smo*_*hir 2 ansible ansible-2.x ansible-facts
我陷入了一项任务,我试图使用 with_together 并循环访问多个列表。这是场景:-
我有两组数据:
"node_list": [
"10.2.0.1",
"10.2.0.2",
]
"java_process_list": [
[
"8612",
"8622",
"8623",
"8625"
],
[
"8613",
"8627",
"8628",
"8630"
]
]
Run Code Online (Sandbox Code Playgroud)
现在我希望我的node_list的第一个项目即(10.2.0.1)迭代process_list的第一个列表中的所有4个项目即(8612,8622,8623,8625)等等。
我正在做的是:-
任务1.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 }}"
Run Code Online (Sandbox Code Playgroud)
当我运行剧本时,我得到以下输出:-
{
"_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
}
Run Code Online (Sandbox Code Playgroud)
预期结果:-
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
Run Code Online (Sandbox Code Playgroud)
下面的任务可以完成这项工作
- debug:
msg: "{{ item.0.key }}, {{ item.1 }}"
with_subelements:
- "{{ dict(nodes_list|zip(process_list))|dict2items }}"
- value
Run Code Online (Sandbox Code Playgroud)
给出
"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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4236 次 |
| 最近记录: |