如何在json寄存器的ansible playbook中使用nest循环

时间:2015-06-17 06:32:20

标签: json ansible ansible-playbook

我从rb获得了一个复杂的json,我就像这样注册

- name: get the json
  command: /abc/get_info.rb
  register: JsonInfo

和json是这样的

{"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"]]}

但我能做的就是像这样输出json:

- debug: msg="{{JsonInfo}}"

并像这样循环

- debug: msg="{{item.key}} and the host is{{inventory_hostname}} and value is{{item.value}}"
  with_dict: "{{JsonInfo.stdout}}" 
  when: item.key==inventory_hostname
<顺便说一下,a-b-c.abc.com,c-d-e.abc.com,c-d-m.abc.com是服务器的主机名

但我真正想做的是先在json上运行一个循环,然后得到

的结果
"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"]]

当我得到上述所有内容后,我为abc.abc.com,cde.abc.com,cdm.abc.com的每个值运行另一个循环,然后根据&#34; a&#34 ;,&#34; C&#34; ,在a-b-c.abc.com或c-d-e.abc.com上运行不同的命令

我如何循环这些json?

1 个答案:

答案 0 :(得分:0)

the available Ansible loops无法做到这一点。您可以按creating your own lookup plugin归档此内容。