当我调试从命令模块注册的变量时,我无法访问 ansible 返回的结果数组中的某些值。
我正在使用 with_items 运行多个命令并将结果注册到“registeredVar”中。我尝试访问这样的结果,registeredVar.results.stdout但我得到“变量未定义!” 错误。
我还尝试在调试任务中循环遍历结果数组,但出现“对象”没有属性“stdout”错误
以下是我正在运行的任务
- name: check configuration
shell: "{{ item }}"
register: falcon_config
with_items:
- /opt/CrowdStrike/falconctl -g --aph
- /opt/CrowdStrike/falconctl -g --cid
- /opt/CrowdStrike/falconctl -g --app
Run Code Online (Sandbox Code Playgroud)
下面是输出变量的调试任务
- debug:
var: falcon_config['results'].stdout
Run Code Online (Sandbox Code Playgroud)
替代方案:
- debug:
var: '{{ item.stdout }}'
with_items: falcon_config['results']
Run Code Online (Sandbox Code Playgroud)
这是运行时结果数组的调试输出falcon_config['results']
"falcon_config['results']": [
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/CrowdStrike/falconctl -g --aph",
"delta": "0:00:00.006724",
"end": "2019-07-29 10:36:05.481672",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/opt/CrowdStrike/falconctl -g --aph",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "/opt/CrowdStrike/falconctl -g --aph",
"rc": 0,
"start": "2019-07-29 10:36:05.474948",
"stderr": "",
"stderr_lines": [],
"stdout": "aph=crowdstrike.domain.",
"stdout_lines": [
"aph=crowdstrike.intra.absaafrica."
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/CrowdStrike/falconctl -g --cid",
"delta": "0:00:00.006716",
"end": "2019-07-29 10:36:05.662173",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/opt/CrowdStrike/falconctl -g --cid",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "/opt/CrowdStrike/falconctl -g --cid",
"rc": 0,
"start": "2019-07-29 10:36:05.655457",
"stderr": "",
"stderr_lines": [],
"stdout": "cid=\"185d26e78791sksdasd9d1033sa4\".",
"stdout_lines": [
"cid=\"185d26e78791sksdasd9d1033sa4\"."
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/CrowdStrike/falconctl -g --app",
"delta": "0:00:00.006617",
"end": "2019-07-29 10:36:05.840573",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/opt/CrowdStrike/falconctl -g --app",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "/opt/CrowdStrike/falconctl -g --app",
"rc": 0,
"start": "2019-07-29 10:36:05.833956",
"stderr": "",
"stderr_lines": [],
"stdout": "app=8080.",
"stdout_lines": [
"app=8080."
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我无法获取每个对象的标准输出值。
您可以使用json_query过滤器:
- debug:
var: falcon_config | json_query('results[*].stdout')
Run Code Online (Sandbox Code Playgroud)
这也可以作为循环的输入
| 归档时间: |
|
| 查看次数: |
11971 次 |
| 最近记录: |