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

问题描述 投票:0回答:4

我是 ansible 新手。

有没有简单的方法可以用更多的IP替换

option domain-name-servers
中以
/etc/dhcp/interface-br0.conf
开头的行?

  option domain-name-servers 10.116.184.1,10.116.144.1;

我想补充

,10.116.136.1

ansible
4个回答
53
投票

您可以使用 lineinfile Ansible 模块 来实现这一点。

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

line
选项会将之前找到的内容替换为您选择的新内容。

backrefs
选项保证如果正则表达式不匹配,文件将保持不变。


9
投票

您可以使用更换模块。请参阅 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.
    ansible.builtin.replace:
      path: /etc/hosts
      regexp: '(\s+)old\.name\.com(\s+.*)?$'
      replace: '\1new.name.com\2'
      backup: yes

奔跑

ansible-playbook httpd-replace-hostname.yml

如下图即可查看成功结果。

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 

4
投票

我们可以使用lineinfile模块来替换一行

使用临时命令

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"

使用ansible剧本

- 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

有关更多信息,我们可以检查其他选项:在 lineinfile 模块中

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


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
© www.soinside.com 2019 - 2024. All rights reserved.