I am stuck on a task where I am trying to use with_together and loop through multiple lists. Here is the scenario:-
I have two sets of data:
"node_list": [
"10.2.0.1",
"10.2.0.2",
]
"java_process_list": [
[
"8612",
"8622",
"8623",
"8625"
],
[
"8613",
"8627",
"8628",
"8630"
]
]
Now I want my node_list's first item i.e. ( 10.2.0.1 )to iterate all 4 items inside process_list's first list i.e. ( 8612,8622,8623,8625 ) and so on..
What I am doing is:-
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 }}"
When I run the playbook, I get the following output:-
{
"_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
}
Expected Result:-
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