有时候我们可能需要循环数组中的内容,也有时候需要从dict中取出指定的信息, 下边这个例子,首先根据a-b-c.abc.com取出和当前主机名匹配的数组,设置到另外一个变量中,然后,通过 with_dict来循环相关变量的值
如下例子可以直接copy复制粘贴后执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
--- - hosts: localhost remote_user: root gather_facts: no vars: myJson: {"a-b-c.abc.com":[["000000001","a"],["000000002","a"],["000000003","c"]],"c-d-e.abc.com":[["000000010","c"],["000000012","b"]],"c-d-m.abc.com":[["000000022","c"],["000000033","b"],["000000044","c"]]} tasks: - name: get the a-b-c.abc.com info set_fact: myjson='{{item.value}}' with_dict: '{{myJson}}' when: item.key=='a-b-c.abc.com' - name: oup put the myjson of first debug: var="{{myjson}}" - name: Output the inner with_nested: "{{ [myjson] }}" debug: msg="{{item[0]}} is {{item[1]}}" |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021