在 Ansible 中组合多个循环的输出 [英] Combining the output of multiple loops in Ansible

查看:28
本文介绍了在 Ansible 中组合多个循环的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ansible,在我的剧本中我正在运行以下任务,其中我有多个循环以便从特定的 xml 中检索不同的标签:

- name: 检索多个xml标签值ixml:xmlstring: "{{ item.string }}"xpath:{{ item.path }}"内容:文字环形:- { 路径:/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-tag",字符串:{{topology.xml}}"}- { 路径:/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-member-interface",字符串:{{topology.xml}}"}注册:tags_value- 调试:msg: "{{ item.matches }}"循环:{{ tags_value.results }}"循环控制:标签:{{ item.matches }}"

所以我得到以下信息:

ok: [sss-sd1-02] =>(item=[{u'vlan-member-interface': u'et-0/0/0.0*'}, {u'vlan-member-interface': u'et-0/0/1.0*'}]) =>{味精":[{vlan-member-interface":et-0/0/0.0*"},{vlan-member-interface":et-0/0/1.0*"}]

}

还有这个

ok: [sss-sd1-02] =>(item=[{u'vlan-tag': u'4071'}, {u'vlan-tag': u'4072'}]) =>{味精":[{VL​​AN标签":4071"},{VL​​AN标签":4072"}]

}

有没有办法将 "vlan-member-interface": "et-0/0/0.0*" 和 "vlan-tag": "4071" 在 1 个结果中分组,无论是在这个任务中还是在另一个任务中?还有没有办法创建一个只有 {4071, 4072} 的列表?因为我现在无法处理它!!!

解决方案

问题可以简化.让我们有数据

 结果:- [A:a1,A:a2]- [B: b1, B: b2]

<小时><块引用>

Q1:将A:a1"和B:b1"分组为 1 个结果"

A:使用 zip 功能.例如

 - set_fact:my_groups: "{{ results.0|zip(results.1)|list }}"- 调试:变量:my_groups

给予

 "my_groups": [[{A":a1"},{B":b1"}],[{A":a2"},{B":b2"}]]}

<小时><块引用>

Q2:创建一个只有 [b1, b2] 的列表"

A:使用 dict2itemsjson_query 函数.例如

 - set_fact:my_values: "{{ results.1|map('dict2items')|list|json_query('[].value') }}"- 调试:变量:my_values

给予

 "my_values": ["b1",b2"]

<小时> 问题 1 是替换:组vlan-member-interface":et-0/0/0.0*"和vlan-tag":1 个结果中的4071"
问题 2 是替换:创建一个仅包含 {4071, 4072}
的列表变量 results 替换了 tags_value.results

I am working with Ansible and in my playbook I am running the following task, in which I have multiple loops so as to retrieve different tags from a specific xml:

- name: Retrieve multiple xml tags valuei
  xml:
    xmlstring: "{{ item.string }}"
    xpath: "{{ item.path }}"
    content: text
  loop:
    - { path: "/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-tag", string: "{{topology.xml}}" }
    - { path: "/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-member-interface", string: "{{topology.xml}}" }
  register: tags_value

- debug:
    msg: "{{ item.matches }}"
  loop: "{{ tags_value.results }}"
  loop_control:
    label: "{{ item.matches }}"

So I am getting the following:

ok: [sss-sd1-02] => (item=[{u'vlan-member-interface': u'et-0/0/0.0*'}, {u'vlan-member-interface': u'et-0/0/1.0*'}]) => {
"msg": [
    {
        "vlan-member-interface": "et-0/0/0.0*"
    }, 
    {
        "vlan-member-interface": "et-0/0/1.0*"
    }
]

}

and this

ok: [sss-sd1-02] => (item=[{u'vlan-tag': u'4071'}, {u'vlan-tag': u'4072'}]) => {
"msg": [
    {
        "vlan-tag": "4071"
    }, 
    {
        "vlan-tag": "4072"
    }
]

}

Is there a way to group "vlan-member-interface": "et-0/0/0.0*" and "vlan-tag": "4071" in 1 result , either in this task or in a different one ? And also is there a way to create a list with only {4071, 4072} ? Because i cannot handle it as it is right now !!!

解决方案

The question can be simplified. Let's have the data

    results:
      - [A: a1, A: a2]
      - [B: b1, B: b2]


Q1: "Group "A:a1" and "B:b1" in 1 result"

A: Use zip function. For example

    - set_fact:
        my_groups: "{{ results.0|zip(results.1)|list }}"
    - debug:
        var: my_groups

gives

    "my_groups": [
        [
            {
                "A": "a1"
            }, 
            {
                "B": "b1"
            }
        ], 
        [
            {
                "A": "a2"
            }, 
            {
                "B": "b2"
            }
        ]
    ]
}


Q2: "Create a list with only [b1, b2]"

A: Use dict2items and json_query functions. For example

   - set_fact:
        my_values: "{{ results.1|map('dict2items')|list|json_query('[].value') }}"
    - debug:
        var: my_values

gives

    "my_values": [
        "b1", 
        "b2"
    ]


Question 1 is substitution of: Group "vlan-member-interface": "et-0/0/0.0*" and "vlan-tag": "4071" in 1 result
Question 2 is substitution of: Create a list with only {4071, 4072}
Variable results is substitution of tags_value.results

这篇关于在 Ansible 中组合多个循环的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆