用ansible替换配置文件中的一行 [英] Replace a line in a config file with ansible

查看:60
本文介绍了用ansible替换配置文件中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ansible 的新手.

I am new to ansible.

是否有一种简单的方法可以将 /etc/dhcp/interface-br0.conf 中以 option domain-name-servers 开头的行替换为更多 IP?

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;

我要添加,10.116.136.1

推荐答案

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

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

regexp 选项告诉模块要替换的内容是什么.

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

line 选项用您选择的新内容替换之前找到的内容.

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

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

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

这篇关于用ansible替换配置文件中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆