用ansible替换配置文件中的一行

用ansible替换配置文件中的一行,ansible,Ansible,我不熟悉ansible 是否有一种简单的方法可以将/etc/dhcp/interface-br0.conf中以选项域名服务器开始的行替换为更多IP option domain-name-servers 10.116.184.1,10.116.144.1; 我想添加,10.116.136.1我用以下main.yaml创建了一个角色dhcp: --- - name: add all dns servers lineinfile: dest: /etc/dhcp/interface-

我不熟悉ansible

是否有一种简单的方法可以将
/etc/dhcp/interface-br0.conf
中以
选项域名服务器开始的行替换为更多IP

  option domain-name-servers 10.116.184.1,10.116.144.1;

我想添加
,10.116.136.1

我用以下
main.yaml
创建了一个角色
dhcp

---
- 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
您可以使用来实现这一点

  - 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
regexp
选项告诉模块要替换的内容

选项将以前找到的内容替换为您选择的新内容


backrefs
选项保证如果regexp不匹配,文件将保持不变。

您可以使用Replace模块。请参阅

您可以按以下方式成功签出结果

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 

我们可以使用lineinfle模块替换一条线

使用特设命令:

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"
- 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
有关更多信息,我们可以查看其他选项:在lineinfle模块中


。使用
regexp
backrefs
参数,从包含的示例中学习。如果找不到,我们可以替换或添加新行吗?backrefs在本示例中的作用是什么?应该是path:而不是dest:。
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"
- 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