Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a line in a config file with ansible

Tags:

ansible

I am new to ansible.

Is there a simple way to replace the line starting with option domain-name-servers in /etc/dhcp/interface-br0.conf with more IPs?

  option domain-name-servers 10.116.184.1,10.116.144.1;

I want to add ,10.116.136.1

like image 583
rubo77 Avatar asked Nov 24 '16 14:11

rubo77


4 Answers

You can use the lineinfile Ansible module to achieve that.

  - name: replace line
    lineinfile: 
      path: /etc/dhcp/interface-br0.conf 
      regexp: '^(.*)option domain-name-servers(.*)$' 
      line: 'option domain-name-servers 10.116.184.1,10.116.144.1,10.116.136.1;'
      backrefs: yes

The regexp option tells the module what will be the content to replace.

The line option replaces the previously found content with the new content of your choice.

The backrefs option guarantees that if the regexp does not match, the file will be left unchanged.

like image 84
SegFault Avatar answered Nov 15 '22 19:11

SegFault


You can use Replace Module. Please refer to http://docs.ansible.com/ansible/latest/modules/replace_module.html.

#example
vim httpd-replace-hostname.yml

---
- hosts: <Your ansible host>
  tasks:
  - name: hostname was used instead of path.
    replace:
      path: /etc/hosts
      regexp: '(\s+)old\.name\.com(\s+.*)?$'
      replace: '\new.name.com\2'
      backup: yes

Run

ansible-playbook httpd-replace-hostname.yml

You can check out the result successfully as below.

PLAY [Your hosts] ***************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [hostname.name.com]

TASK [hostname was used instead of path.] ***************************************************************************************************************************************
ok: [hostname.name.com]

TASK [Replace after the expression till the end of the file] ********************************************************************************************************************
changed: [hostname.name.com]

PLAY RECAP **********************************************************************************************************************************************************************
hostname.name.com : ok=3    changed=1    unreachable=0    failed=0 
like image 35
Shirley Avatar answered Nov 15 '22 20:11

Shirley


we can use lineinfile module to replace a line

using ad-hoc command:

ansible <host> -m lineinfile -a "path=/etc/dhcp/interface-br0.conf regexp=''^(.*)option domain-name-servers(.*)$'' line='1option domain-name-servers 10.116.184.1,10.116.144.1,10.116.136.1;' backrefs: yes"

using ansible playbook:

- name: replacing a line in file
  lineinfile: 
    path: /etc/dhcp/interface-br0.conf 
    regexp: '^(.*)option domain-name-servers(.*)$' 
    line: 'option domain-name-servers 10.116.184.1,10.116.144.1,10.116.136.1;'
    backrefs: yes

for more we can check for other options: in lineinfile module

https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html

like image 3
Javeed Shakeel Avatar answered Nov 15 '22 19:11

Javeed Shakeel


I created a role dhcp with the following main.yaml:

---
- name: add all dns servers
  lineinfile:
    dest: /etc/dhcp/interface-br0.conf
    regexp: '^\s*option domain-name-servers.*$'
    line: '  option domain-name-servers 10.116.184.1,10.116.144.1,10.116.136.1;'
    backrefs: yes
  become: true
like image 1
rubo77 Avatar answered Nov 15 '22 20:11

rubo77