Ansible、循环、寄存器和标准输出
我有一本这样的剧本:Ansible、循环、寄存器和标准输出,ansible,Ansible,我有一本这样的剧本: - hosts: host1 gather_facts: false tasks: - name: "Loop" command: "echo {{ item }}" with_items: [ 0, 2, 4, 6, 8, 10 ] register: hello - debug: "msg={{ hello.results }}" 一切都正常工作,输出被返回,但是有成吨的输出。事实证明: - debug: "msg={{ h
- hosts: host1
gather_facts: false
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: [ 0, 2, 4, 6, 8, 10 ]
register: hello
- debug: "msg={{ hello.results }}"
一切都正常工作,输出被返回,但是有成吨的输出。事实证明:
- debug: "msg={{ hello.results.1.stdout }}"
做我想做的事情——从命令中获取stdout——但这只是循环中六次中的一次
我真正想要/需要做的是:
- debug: "msg={{ hello.results.*.stdout }}"
它进入hello
结构,访问results
条目,找到该数组的每个成员,并提取stdout
值
这可能吗
更新 与我最初的示例一样冗长
TASK [debug] *******************************************************************
ok: [host1] => (item={'_ansible_parsed': True, 'stderr_lines': [], u'cmd': [
u'echo', u'0'], u'end': u'2018-01-02 20:53:08.916774', '_ansible_no_log': False
, u'stdout': u'0', '_ansible_item_result': True, u'changed': True, 'item': 0,
u'delta': u'0:00:00.002137', u'stderr': u'', u'rc': 0, u'invocation': {u'module_
args': {u'warn': True, u'executable': None, u'_uses_shell': False, u'_raw_params
': u'echo 0', u'removes': None, u'creates': None, u'chdir': None, u'stdin': Non
e}}, 'stdout_lines': [u'0'], u'start': u'2018-01-02 20:53:08.914637', 'failed':
False}) => {
"item": {
"changed": true,
"cmd": [
"echo",
"0"
],
"delta": "0:00:00.002137",
"end": "2018-01-02 20:53:08.916774",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo 0",
"_uses_shell": false,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": 0,
"rc": 0,
"start": "2018-01-02 20:53:08.914637",
"stderr": "",
"stderr_lines": [],
"stdout": "0",
"stdout_lines": [
"0"
]
},
"msg": "0"
}
我得到了6份上述结构的副本
感觉上我已经很接近了,但我还是做错了什么。我在底部看到
“msg”:“0”
,这就是我想要的。我只是不想要剩下的部分。当然。ansible网站上有文档说明。您只需迭代hello.results
数组,如下所示:
- debug:
msg: "{{item.stdout}}"
with_items: "{{hello.results}}"
当然。ansible网站上有文档说明。您只需迭代
hello.results
数组,如下所示:
- debug:
msg: "{{item.stdout}}"
with_items: "{{hello.results}}"
那么:
- debug: "msg={{ item.stdout }}"
with_items: "{{ hello.results }}"
那么:
- debug: "msg={{ item.stdout }}"
with_items: "{{ hello.results }}"
解决方案:
- debug: "msg={{ hello.results | map(attribute='stdout') | join('\n') }}"
备注:
- debug: "msg={{ hello.results | map(attribute='stdout') | join('\n') }}"
默认情况下,Ansible将打印可见的\n
两个字符序列,而不是换行,因此可以使用回调插件进行人类可读的输出()或使用以下方法验证该方法:
- copy:
content: "{{ hello.results | map(attribute='stdout') | join('\n') }}"
dest: ./result.txt
并检查
result.txt
解决方案的内容:
- debug: "msg={{ hello.results | map(attribute='stdout') | join('\n') }}"
备注:
- debug: "msg={{ hello.results | map(attribute='stdout') | join('\n') }}"
默认情况下,Ansible将打印可见的\n
两个字符序列,而不是换行,因此可以使用回调插件进行人类可读的输出()或使用以下方法验证该方法:
- copy:
content: "{{ hello.results | map(attribute='stdout') | join('\n') }}"
dest: ./result.txt
并检查
result.txt
的内容。我认为这个结构可以很好地满足我的需要
- hosts: localhost
gather_facts: false
vars:
stuff: [ 0,2,4,6,8,10 ]
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: "{{ stuff }}"
register: hello
- debug: "var=hello.results.{{item}}.stdout"
with_sequence: "0-{{stuff|length - 1}}"
我认为这个结构对我的需要来说已经足够好了
- hosts: localhost
gather_facts: false
vars:
stuff: [ 0,2,4,6,8,10 ]
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: "{{ stuff }}"
register: hello
- debug: "var=hello.results.{{item}}.stdout"
with_sequence: "0-{{stuff|length - 1}}"
我使用关键字循环从上一个循环的所有迭代中获取标准输出:
loop: "{{ hello | json_query('results[*].stdout') }}"
我发现json_查询在这种寄存器循环情况下最容易使用。可以在这里找到官方文档==>我使用关键字循环从上一个循环的所有迭代中获取标准输出:
loop: "{{ hello | json_query('results[*].stdout') }}"
我发现json_查询在这种寄存器循环情况下最容易使用。可以在此处找到正式文档==>
任务[debug]致命:[localhost]:失败!=>{“msg”:“模板化字符串时出现模板错误:没有名为“map”的筛选器。字符串:{{hello.results | map(attribute='stdout')| join('\n')}}}}
可能我缺少库或外接程序或其他内容。。。或者我的ansible版本太旧了。请修复您的ansible/Jinja2安装<代码>映射是在Jinja2 2.7(in!)中添加的,因此您的安装要么陈旧,要么已损坏。任务[debug]致命:[localhost]:失败!=>{“msg”:“模板化字符串时出现模板错误:没有名为“map”的筛选器。字符串:{{hello.results | map(attribute='stdout')| join('\n')}}}}
可能我缺少库或外接程序或其他内容。。。或者我的ansible版本太旧了。请修复您的ansible/Jinja2安装map
是在Jinja2 2.7(in!)中添加的,因此您的安装要么古老,要么已损坏。如果查看结果中的msg
键,您将看到它正确地迭代了结果列表。如果你想减少冗长的输出,除了使用debug
任务之外,还可以做些别的事情。我对ansible非常陌生。你能给我一些其他的选择吗<代码>调试是我能找到的唯一生成命令标准输出的东西。您想对输出做什么?Ansible并不是一个真正的工具。它是做事的工具。是否要将输出写入文件?将它发送到另一个命令?等等。我只想将命令输出记录到一个文件中,或者(理想情况下)记录到一个可以用script
捕获的会话中。如果该工具要做重要的事情,我需要一个详细的日志,记录它做了什么以及命令的结果是什么,以便将来进行故障排除/记录。如果查看结果中的msg
键,您将看到它正确地迭代了结果列表。如果你想减少冗长的输出,除了使用debug
任务之外,还可以做些别的事情。我对ansible非常陌生。你能给我一些其他的选择吗<代码>调试是我能找到的唯一生成命令标准输出的东西。您想对输出做什么?Ansible并不是一个真正的工具。它是做事的工具。是否要将输出写入文件?将它发送到另一个命令?等等。我只想将命令输出记录到一个文件中,或者(理想情况下)记录到一个可以用script
捕获的会话中。如果这个工具要做一些重要的事情,我需要一个详细的日志来记录它做了什么以及命令的结果,以便将来进行故障排除/记录保存。