我是新來的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
我是新來的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
可以使用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
選項會用您選擇的新內容替換先前找到的內容。
thx的提示,我改變了我的解決方案更多的行 – rubo77
我創建了一個角色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
['lineinfile'](https://docs.ansible.com/ansible/lineinfile_module.html)。使用'regexp'和'backrefs'參數,從包含的例子中學習。 – techraf