ansible playbook从/etc/resolv.conf文件读取名称服务器(DNS)

ansible playbook从/etc/resolv.conf文件读取名称服务器(DNS),ansible,Ansible,请帮助我编写ansible playbook,从多个服务器的/etc/resolv.conf文件中读取名称服务器(DNS),并将这些名称服务器写入一个文件 我试图将内容放入名为contents的变量中,如下所示: -名称:检查resolv.conf是否存在 斯达: 路径:/etc/resolv.conf 注册:resolv_conf -名称:检查resolv.conf中的名称服务器列表 变量: 内容:“{lookup('file','/etc/resolv.conf')}” when:resol

请帮助我编写ansible playbook,从多个服务器的/etc/resolv.conf文件中读取名称服务器(DNS),并将这些名称服务器写入一个文件

我试图将内容放入名为
contents
的变量中,如下所示:

-名称:检查resolv.conf是否存在
斯达:
路径:/etc/resolv.conf
注册:resolv_conf
-名称:检查resolv.conf中的名称服务器列表
变量:
内容:“{lookup('file','/etc/resolv.conf')}”
when:resolv_conf.stat.exists==True

然后不确定如何继续。

您可以使用带有过滤器的正则表达式来实现这一点,特别是考虑到您可能在其中定义了多个DNS服务器

注意这会给你一个列表

根据剧本:

-hosts:localhost
收集事实:不
任务:
-名称:检查resolv.conf是否存在
斯达:
路径:/etc/resolv.conf
注册:resolv_conf
-名称:检查resolv.conf中的名称服务器列表
调试:
msg:“{{contents}}”
变量:
内容:“{lookup('file','/etc/resolv.conf')| regex_findall('\\s*名称服务器\\s*(*))}”
when:resolv_conf.stat.exists==True
在阿尔卑斯山集装箱上,它提供了以下内容:

PLAY [localhost] ***************************************************************

TASK [check resolv.conf exists] ************************************************
ok: [localhost]

TASK [check nameservers list in resolv.conf] ***********************************
ok: [localhost] => {
    "msg": [
        "192.168.65.1"
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我不确定它是否正是您想要的,但您可以使用lineinfle将内容写入文件,然后使用replace模块替换除名称服务器之外的所有内容。像这样:

          - name: Check if resolv exists
            stat:
                    path: /etc/resolv.conf
            register: resolv_conf

          - set_fact:
                    content: "{{ lookup('file', '/etc/resolv.conf') }}"
            when: resolv_conf.stat.exists


          - name: Write them into a file
            lineinfile:
                    line: "{{ content }}"
                    create: yes
                    state: present
                    path: /tmp/resolv_conf_nameservers

          - name: Remove everything but nameservers
            replace:
                    path: /tmp/resolv_conf_nameservers
                    regexp:  "^(?!nameserver).*$"
                    replace: ""
~
请注意,您应该使用set\u fact将内容加载到变量中

如果要将包含名称服务器的文件放置在您自己的计算机/本地主机中,请在最后两个任务中使用委托给

          - name: Check if resolv exists
            stat:
                    path: /etc/resolv.conf
            register: resolv_conf

          - set_fact:
                    content: "{{ lookup('file', '/etc/resolv.conf') }}"
            when: resolv_conf.stat.exists


          - name: Write them into a file
            lineinfile:
                    line: "{{ content }}"
                    create: yes
                    state: present
                    path: /tmp/resolv_conf_nameservers
            delegate_to: localhost

          - name: Remove everything but nameservers
            replace:
                    path: /tmp/resolv_conf_nameservers
                    regexp:  "^(?!nameserver).*$"
                    replace: ""
            delegate_to: localhost                                           
    

正是我想要的。谢谢Vitor Carvalho。如果我想将多台服务器的输出结果保存到一个文件中,那么最好的方法是什么?然后针对库存中的多台服务器运行本手册。playbook会将内容附加到localhost中的文件中。但是为了做到这一点,不要忘记delegate_to:localhost标记。谢谢您的快速回复。这对我帮助很大。