如果我有这两项任务
- name: Replace ServerIP in config_file on OTHER NODES
set_fact:
variable: "{{hostvars.localhost.new_ips.results}}"
- name: Display variable
debug: var=variable
结果是:
TASK: [Display variable] *********************************************************
ok: [vm2] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
ok: [vm1] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
然后我怎样才能访问变量的stdout部分。请注意,我只需要这个变量的标准输出部分,即11.11.4.74和11.11.4.138(最好是循环)
答案 0 :(得分:3)
您可以单独访问
{{ variable[0].stdout }}
和 {{variable [1] .stdout}}
或使用循环
- debug: var=item.stdout
with_items: variable