【问题标题】:Print json output in Ansible and store in a list?在 Ansible 中打印 json 输出并存储在列表中?
【发布时间】:2020-06-17 07:50:19
【问题描述】:

Ansible 剧本:

---
- task:-
  - code goes here

  - name: Trying to get instance Private IP from ASG
    ec2_instance_facts:
      instance_ids: 
         - "{{ item }}"
    with_items: "{{ INSTANCE_IDS_FROM_ASG }}"    
    register: instance_ids_result

  - set_fact:
      msg: "{{ instance_ids_result | json_query('results[*].instances[*].network_interfaces[*].private_ip_address') }} "

  - debug: var=msg

我的输出如下:

ok: [localhost] => {
    "msg": [
        [
            "172.31.144.74"
        ],
        [
            "172.31.147.69"
        ]
    ]
}

但是,我需要列表中的输出为["172.31.144.74", "172.31.147.69"]"172.31.147.69" "172.31.147.69"
以这种方式打印它的最佳方法是什么?

【问题讨论】:

    标签: linux automation ansible


    【解决方案1】:

    您可以使用过滤器来展平您的列表

      - set_fact:
      msg: "{{ instance_ids_result | json_query('results[*].instances[*].network_interfaces[*].private_ip_address') | flatten }} "
    

    【讨论】:

    • 另外,我在一位同事的帮助下找到了这个。 msg: "{{ instance_ids_result | json_query('results[] | [*].instances[] | [*].network_interfaces[] | [*].private_ip_address') | join(',') }}
    猜你喜欢
    • 2021-05-15
    • 2020-03-25
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2023-03-08
    • 2022-11-12
    相关资源
    最近更新 更多