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

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

问题描述

我是新来的ansible.

I am new to ansible.

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

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模块来实现.

  - 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选项可确保如果regexp不匹配,则文件将保持不变.

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

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

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