2016-11-24 71 views
0

我是新來的ansible。替換一個配置文件中的一個行與安理

是否有一種簡單的方法來取代/etc/dhcp/interface-br0.conf中以option domain-name-servers開頭的行,並使用更多的IP?

option domain-name-servers 10.116.184.1,10.116.144.1; 

我想補充,10.116.136.1

+1

['lineinfile'](https://docs.ansible.com/ansible/lineinfile_module.html)。使用'regexp'和'backrefs'參數,從包含的例子中學習。 – techraf

回答

3

可以使用lineinfile Ansible module來實現這一目標。

- name: replace line 
    lineinfile: 
     dest: /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 

regexp選項告訴模塊會是怎樣的內容替換。

line選項會用您選擇的新內容替換先前找到的內容。

+0

thx的提示,我改變了我的解決方案更多的行 – rubo77

1

我創建了一個角色dhcp具有以下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 
相關問題