【问题标题】:Ansible debug loop output as a single list?Ansible 调试循环输出为单个列表?
【发布时间】:2021-06-07 21:18:27
【问题描述】:

我有以下调试任务,其中列出了 {{image_names}} 的图像和版本属性。

---
- name: "List Images"
  debug:
    msg: "{{ item.image }}:{{ item.version }}"
  loop: "{{ image_names }}"

循环变量看起来像这样

  image_names:
  - { service_name: "xxxxxxx",             registry: "xxxxxxx", image: "xxxxxxx",             version: "xxxxxxx" }
  - { service_name: "xxxxxxx",             registry: "xxxxxxx", image: "xxxxxxx",             version: "xxxxxxx" }
...

我在 ansible 塔上收到的输出是

ok: [host] => (item={u'service_name': u'xxxxxxx', u'image': u'xxxxxxx', u'version': u'xxxxxxx', u'registry': u'xxxxxxx'}) => {
    "msg": "xxxxxxx:xxxxxxx"
ok: [host] => (item={u'service_name': u'xxxxxxx', u'image': u'xxxxxxx', u'version': u'xxxxxxx', u'registry': u'xxxxxxx'}) => {
    "msg": "xxxxxxx:xxxxxxx"
ok: [host] => (item={u'service_name': u'xxxxxxx', u'image': u'xxxxxxx', u'version': u'xxxxxxx', u'registry': u'xxxxxxx'}) => {
    "msg": "xxxxxxx:xxxxxxx"

有没有办法将所有“msg:”输出加入一个列表,这样看起来像

msg: 
 - "xxxxxxx:xxxxxxx"
 - "xxxxxxx:xxxxxxx"
 - "xxxxxxx:xxxxxxx"
...

【问题讨论】:

    标签: debugging module ansible


    【解决方案1】:

    例如

        - debug:
            msg: "{{ image_names|
                     json_query('[].[image, version]')|
                     map('join', ':') }}"
          vars:
            image_names:
              - {service_name: x1, registry: x2, image: x3, version: x4}
              - {service_name: y1, registry: y2, image: y3, version: y4}
              - {service_name: z1, registry: z2, image: z3, version: z4}
    

    给予

      msg:
      - x3:x4
      - y3:y4
      - z3:z4
    

    【讨论】:

      【解决方案2】:

      Ansible 2.5 的另一个可能选项是使用 yaml 进行回调,而不是默认的 skippy。在您的 ansible.cfg 中更改以下两个选项:

      # Use the YAML callback plugin.
      stdout_callback = yaml
      # Use the stdout_callback when running ad-hoc commands.
      bin_ansible_callbacks = True 
      

      这会从一个巨大的单行更改调试消息的标准输出:

      fatal: [vulnmanager]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 99, "stderr": "Shared connection to <FQDN>.\r\n", "stderr_lines": ["Shared connection to <FQDN> closed."], "stdout": "\r\nRHSA-2021:1989              Important/Sec. bind-export-libs-32:9.11.26-4.el8_4.x86_64\r\nRHSA-2021:1989              Important/Sec. bind-libs-32:9.11.26-4.el8_4.x86_64\r\nRHSA-2021:1989              Important/Sec. bind-libs-lite-32:9.11.26-4.el8_4.x86_64\r\nRHSA-2021:1989              Important/Sec. bind-license-32:9.11.26-4.el8_4.noarch\r\nRHSA-2021:1989              Important/Sec. bind-utils-32:9.11.26-4.el8_4.x86_64\r\nRHSA-2021:2168              Important/Sec. bpftool-4.18.0-305.3.1.el8_4.x86_64\r\nRHSA-2021:1578     There are 44 available for installation on <FQDN>
      

      到基于 yaml 的、更易读的版本:

      fatal: [vulnmanager]: FAILED! => changed=true 
        msg: non-zero return code
        rc: 99
        stderr: |-
          Shared connection to <FQDN> closed.
        stderr_lines: <omitted>
        stdout: |2-
        
          RHSA-2021:1989              Important/Sec. bind-export-libs-32:9.11.26-4.el8_4.x86_64
          RHSA-2021:1989              Important/Sec. bind-libs-32:9.11.26-4.el8_4.x86_64
          RHSA-2021:1989              Important/Sec. bind-libs-lite-32:9.11.26-4.el8_4.x86_64
          RHSA-2021:1989              Important/Sec. bind-license-32:9.11.26-4.el8_4.noarch
          RHSA-2021:1989              Important/Sec. bind-utils-32:9.11.26-4.el8_4.x86_64
          RHSA-2021:2168              Important/Sec. bpftool-4.18.0-305.3.1.el8_4.x86_64
          RHSA-2021:1578              Important/Sec. bpftool-4.18.0-305.el8.x86_64
      There are 44 available for installation on <FQDN>
      

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 1970-01-01
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-29
        • 1970-01-01
        • 2021-06-11
        相关资源
        最近更新 更多