美文网首页
lineinfile模块使用记录

lineinfile模块使用记录

作者: wowshiwoniu | 来源:发表于2020-08-07 18:23 被阅读0次

lineinfile模块使用记录

在日常操作中,对于文件的操作,是比较频繁的。lineinfile就是针对文本操作的一个强大的模块。

通过ansible的lineinfile模块处理系统dns问题

最近集群内新增了一批ubuntu 18.04 的机器,默认dns使用的是机器内部的,需要调整为内部的dns。默认是systemd-resolved管理的,/etc/resolv.conf文件是一个软链,单独修改无效,这里需要进行处理。同时修改 /etc/nsswitch.conf文件,否则dns无法生效。这里使用了ansible来进行批量操作。

  • 停掉systemd-resolved,删除软连接,将dns地址写入 /etc/resolv.conf
  • 更新/etc/nsswitch.conf文件,通过lineinfile删除匹配的关键字
---
- hosts: all
  become_user: root
  become: yes
  gather_facts: no

  tasks:

   - name: echo resolve
     shell: |
       systemctl stop systemd-resolved
       systemctl disable systemd-resolved
       rm /etc/resolv.conf
       echo "nameserver 114.114.114.114" > /etc/resolv.conf

   - name: update /etc/nsswitch.conf
     lineinfile:
       path: /etc/nsswitch.conf
       regexp: "^hosts(.*)$"
       line: "hosts:          files dns"
       state: present

   - name: install ifconfig
     shell: |
       apt install net-tools -y

上面的修改在机器重启之前,一直很正常,但是有一天机器重启后,返现dns被置为系统默认的了,查看/etc/resolv.conf文件,默认被NetworkManager重置了。

  • 通过lineinfile,在匹配的关键字下一行添加记录。若添加内容存在,则不会进行更改。
---
- hosts: all
  become_user: root
  become: yes
  gather_facts: no

  tasks:

   - name: lineinfile change
     lineinfile:
       dest: /etc/NetworkManager/NetworkManager.conf
       insertafter: '^plugins'
       line: "dns=none"

参考链接:https://www.cnblogs.com/breezey/p/9297252.html

相关文章

网友评论

      本文标题:lineinfile模块使用记录

      本文链接:https://www.haomeiwen.com/subject/fihqdktx.html