Ansible与_一起循环迭代

Ansible与_一起循环迭代,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,我被困在一个任务中,我试图与_一起使用,并在多个列表中循环。以下是场景:- 我有两组数据: "node_list": [ "10.2.0.1", "10.2.0.2", ] "java_process_list": [ [ "8612", "8622", "8623", "8625" ], [ "8613", "8627", "8628", "8630" ] ] 现在,我希望我的节点列表的第一个项(10.2.0.1)迭代

我被困在一个任务中,我试图与_一起使用,并在多个列表中循环。以下是场景:-

我有两组数据:

"node_list": [
"10.2.0.1", 
"10.2.0.2",
]

"java_process_list": [ 
  [
   "8612",
   "8622",
   "8623",
   "8625"
  ], 
  [
   "8613",
   "8627",
   "8628",
   "8630"
  ] 
]
现在,我希望我的节点列表的第一个项(10.2.0.1)迭代进程列表的第一个项(8612862286238625)中的所有4个项,以此类推

我正在做的是:-

task1.yml:-

- name: Execute Script to grep IP address of dynamic nodes
  command: sh grep_nodes.sh
  args:
   chdir: "{{ somedir }}"
  register: result

- name: set fact
  set_fact: dynamic_nodes="{{ item }}"
  with_items: "{{ result.stdout_lines}}"
  register: items

- name: make a list
  set_fact: node_list="{{ items.results | map(attribute='ansible_facts.dynamic_nodes') | list }}"

- debug: var=node_list

- name: Get running java process of dynamic machines
  shell: ssh -i /tmp/key.pem {{item}} ps -ef | grep -v grep | grep -w java | grep GSC | awk '{print$2}'
  with_items: "{{node_list}}"
  register: process

- name: set fact
  set_fact: java_process="{{ item.stdout_lines }}"
  with_items: "{{process.results}}"
  register: items

- name: make a list
  set_fact: java_process_list="{{ items.results | map(attribute='ansible_facts.java_process') | list }}"

- debug: var=java_process_list

- name: Print items
  shell: 'echo {{item.0}}, echo {{item.1}}'
  args:
   chdir: "{{maindir}}"
  with_together:
    - "{{ dynamic_ip_list }}"
    - "{{ java_process_list }}"
当我运行playbook时,我得到以下输出:-

{
"_ansible_parsed": true,
"stderr_lines": [],
"cmd": "echo 10.2.0.1, echo 8612",
"stderr": "",
"stdout": "10.2.0.1, echo 8612",
"_ansible_item_result": true,
"attempts": 1,
"delta": "0:00:00.013837",
"stdout_lines": [
    "10.2.0.1, echo 8612"
],
"_ansible_no_log": false,
"end": "2020-03-16 12:23:58.704174",
"_ansible_item_label": [
    "10.2.0.1",
    "8612",
    "8622",
    "8623",
    "8625"
],
"start": "2020-03-16 12:23:58.690337",
"changed": true,
"item": [
    "10.2.0.1",
    "8612",
    "8622",
    "8623",
    "8625"
],
"rc": 0,
"invocation": {
    "module_args": {
        "creates": null,
        "executable": null,
        "_uses_shell": true,
        "_raw_params": "echo 10.2.0.1, echo 8612",
        "removes": null,
        "argv": null,
        "warn": true,
        "chdir": "/tmp",
        "stdin": null
    }
},
"_ansible_ignore_errors": null
}



{
"_ansible_parsed": true,
"stderr_lines": [],
"cmd": "echo 10.2.0.2, echo 8613",
"stderr": "",
"stdout": "10.2.0.2, echo 8613",
"_ansible_item_result": true,
"attempts": 1,
"delta": "0:00:00.015971",
"stdout_lines": [
    "10.2.0.2, echo 8613"
],
"_ansible_no_log": false,
"end": "2020-03-16 12:23:58.921053",
"_ansible_item_label": [
    "10.2.0.2",
    "8613",
    "8627",
    "8628",
    "8630"
],
"start": "2020-03-16 12:23:58.905082",
"changed": true,
"item": [
    "10.2.0.2",
    "8613",
    "8627",
    "8628",
    "8630"
],
"rc": 0,
"invocation": {
    "module_args": {
        "creates": null,
        "executable": null,
        "_uses_shell": true,
        "_raw_params": "echo 10.2.0.2, echo 8613",
        "removes": null,
        "argv": null,
        "warn": true,
        "chdir": "/tmp",
        "stdin": null
    }
},
"_ansible_ignore_errors": null
}
预期结果:-

echo 10.2.0.1, echo 8612
echo 10.2.0.1, echo 8622
echo 10.2.0.1, echo 8623
echo 10.2.0.1, echo 8625

echo 10.2.0.2, echo 8613
echo 10.2.0.2, echo 8627
echo 10.2.0.2, echo 8628
echo 10.2.0.2, echo 8630 

下面的任务完成了这项工作

    - debug:
        msg: "{{ item.0.key }}, {{ item.1 }}"
      with_subelements:
        - "{{ dict(nodes_list|zip(process_list))|dict2items }}"
        - value
给予


工作起来很有魅力!谢谢你,先生。
    "msg": "10.2.0.2, 8613"
    "msg": "10.2.0.2, 8627"
    "msg": "10.2.0.2, 8628"
    "msg": "10.2.0.2, 8630"
    "msg": "10.2.0.1, 8612"
    "msg": "10.2.0.1, 8622"
    "msg": "10.2.0.1, 8623"
    "msg": "10.2.0.1, 8625"