【问题标题】:Filter Ansible output from lineinfile从 lineinfile 过滤 Ansible 输出
【发布时间】:2019-06-03 17:06:08
【问题描述】:

我正在尝试通过复制到单个主机的文件来审核我的系统。默认输出非常详细。我只想查看 Ansible 日志输出的相关字段;这样,超过 1000 台主机,我可以更快地解决我的问题。 .当我的游戏成功时,我只想看看:

ok: u'/mnt/inventory/hostname999'

我有一个如下所示的剧本:

- hosts: 'localhost'
  name: Playbook for the Audit for our infrastructure.
  gather_facts: False
  become: no
  connection: local
  roles:
    - { role: network, tags: network }

我的网络角色 main.xml 文件如下所示:

---
- name: find matching pattern files
  find:
    paths: "/mnt/inventory"
    patterns: "hostname*"
    file_type: directory
  register: subdirs

- name: check files for net.ipv4.ip_forward = 0
  no_log: False
  lineinfile:
    name: "{{ item.path }}/sysctl.conf"
    line: "net.ipv4.ip_forward = 0"
    state: present
  with_items: "{{ subdirs.files }}"
  register: conf
  check_mode: yes
  failed_when: (conf is changed) or (conf is failed)
- debug:
    msg: "CONF OUTPUT: {{ conf }}"

但我得到这样的日志输出:

ok: [localhost] => (item={u'uid': 0, u'woth': False, u'mtime': 1546922126.0,
u'inode': 773404, u'isgid': False, u'size': 4096, u'roth': True, u'isuid': 
False, u'isreg': False, u'pw_name': u'root', u'gid': 0, u'ischr': False, 
u'wusr': False, u'xoth': True, u'rusr': True, u'nlink': 12, u'issock': 
False, u'rgrp': True, u'gr_name': u'root', u'path': 
u'/mnt/inventory/hostname999', u'xusr': True, u'atime': 1546930801.0, 
u'isdir': True, u'ctime': 1546922126.0, u'wgrp': False, u'xgrp': True, 
u'dev': 51, u'isblk': False, u'isfifo': False, u'mode': u'0555', u'islnk': 
False})

此外,我的 CONF OUTPUT 调试消息从未显示,我不知道为什么不显示。

我查看了 https://github.com/ansible/ansible/issues/5564 和其他文章,但它们似乎只是指诸如 shell 命令之类的项目,这些项目将内容发送到标准输出,而 lineinfile 没有。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    但我得到这样的日志输出:

    那么您可能希望将loop_control:label: "{{ item.path }}" 孩子一起使用:

    - lineinfile:
        # as before
      with_items: "{{ subdirs.files }}"
      loop_control:
         label: "{{ item.path }}"
    

    这会让你更接近你想要的:

    ok: [localhost] => (item=/mnt/inventory/hostname999)
    

    此外,我的 CONF OUTPUT 调试消息从未显示,我不知道为什么不显示。

    我对此的最佳猜测是,可能需要调整详细程度:

    - debug:
        msg: "CONF OUTPUT: {{ conf }}"
        verbosity: 0
    

    但它对我有用,所以也许你的 ansible 设置还有其他特别之处。我想试试verbosity: 看看是否有帮助。考虑到您对 msg: 的实际操作,您可能会更高兴将 conf 直接传递给 debug

    - debug:
        var: conf
    

    因为它会使 渲染得更好,因为 ansible 知道它是一个 dict,而不是仅仅有效地调用 str(conf)(如您在上面看到的)格式不是很好。

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      相关资源
      最近更新 更多