Ansible:通过迭代注册和显示结果
我的剧本是Ansible:通过迭代注册和显示结果,ansible,Ansible,我的剧本是 --- - name: Running Checks on ACTIVATION VMs of ECM hosts: activationvms tasks: - name: Checking File System status shell: df -PTh register: filesystem_check - name: Display File System Statistics debug: msg: "{{
---
- name: Running Checks on ACTIVATION VMs of ECM
hosts: activationvms
tasks:
- name: Checking File System status
shell: df -PTh
register: filesystem_check
- name: Display File System Statistics
debug:
msg: "{{ filesystem_check.stdout_lines }}"
- name: Check the status of list of services
shell: systemctl list-units|grep "{{ item }}"|awk '{ print $1,$2,$3,$4}'
with_items:
- zookeeper.service
- cassandra.service
register: service_status
- name: Display services status
debug:
msg: "{{ item.stdout }}"
with_items: "{{ service_status.results }}"
我想把结果缩小到只有msg行
ok: [10.142.6.79] => (item={u'_ansible_parsed': True, u'changed': True, u'stdout': u'cassandra.service loaded active running', u'_ansible_no_log': False, u'stdout_lines': [u'cassandra.service loaded active running'], u'warnings': [], u'_ansible_item_result': True, u'start': u'2019-11-15 07:50:39.047456', u'delta': u'0:00:00.011821', u'cmd': u'systemctl list-units|grep "cassandra.service"|awk \'{ print $1,$2,$3,$4}\'', u'item': u'cassandra.service', u'rc': 0, u'invocation': {u'module_name': u'command', u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'systemctl list-units|grep "cassandra.service"|awk \'{ print $1,$2,$3,$4}\'', u'removes': None, u'creates': None, u'chdir': None}}, u'end': u'2019-11-15 07:50:39.059277', u'stderr': u''}) => {
"item": {
"changed": true,
"cmd": "systemctl list-units|grep \"cassandra.service\"|awk '{ print $1,$2,$3,$4}'",
"delta": "0:00:00.011821",
"end": "2019-11-15 07:50:39.059277",
"invocation": {
"module_args": {
"_raw_params": "systemctl list-units|grep \"cassandra.service\"|awk '{ print $1,$2,$3,$4}'",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "cassandra.service",
"rc": 0,
"start": "2019-11-15 07:50:39.047456",
"stderr": "",
"stdout": "cassandra.service loaded active running",
"stdout_lines": [
"cassandra.service loaded active running"
],
"warnings": []
},
"msg": "cassandra.service loaded active running"
}
ok: [10.142.6.72] => (item={u'_ansible_parsed': True, u'changed': True, u'stdout': u'cassandra.service loaded active running', u'_ansible_no_log': False, u'stdout_lines': [u'cassandra.service loaded active running'], u'warnings': [], u'_ansible_item_result': True, u'start': u'2019-11-15 07:50:39.429296', u'delta': u'0:00:00.011853', u'cmd': u'systemctl list-units|grep "cassandra.service"|awk \'{ print $1,$2,$3,$4}\'', u'item': u'cassandra.service', u'rc': 0, u'invocation': {u'module_name': u'command', u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'systemctl list-units|grep "cassandra.service"|awk \'{ print $1,$2,$3,$4}\'', u'removes': None, u'creates': None, u'chdir': None}}, u'end': u'2019-11-15 07:50:39.441149', u'stderr': u''}) => {
"item": {
"changed": true,
"cmd": "systemctl list-units|grep \"cassandra.service\"|awk '{ print $1,$2,$3,$4}'",
"delta": "0:00:00.011853",
"end": "2019-11-15 07:50:39.441149",
"invocation": {
"module_args": {
"_raw_params": "systemctl list-units|grep \"cassandra.service\"|awk '{ print $1,$2,$3,$4}'",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "cassandra.service",
"rc": 0,
"start": "2019-11-15 07:50:39.429296",
"stderr": "",
"stdout": "cassandra.service loaded active running",
"stdout_lines": [
"cassandra.service loaded active running"
],
"warnings": []
},
"msg": "cassandra.service loaded active running"
您需要使用循环控制工具。像这样重写你的上一部剧本
- name: Display services status
debug:
msg: "{{ item.stdout }}"
with_items: "{{ service_status.results }}"
loop_control:
label: "{{ item.item }}"
这会将输出限制为仅迭代到先前命令中的内容,以便您可以知道当前处于循环的哪一部分。您遇到了什么样的错误?我只是在本地运行了它,它起了作用,所以共享错误可能会帮助我了解问题所在。这个循环控制:{item.item}和标签{{item.item}}只在Ansible的更高版本上起作用吗??我使用2.1.1.0。我没有得到任何错误,但结果是相同的。有问题的行似乎是:loop_control:label:“{{item.item}}”^这里我们可能是错的,但这一行看起来可能是缺少引号的问题。当模板表达式括号开始一个值时,始终引用它们。例如:with_items:-{{foo}}应该写为:with_items:-“{{foo}”我认为您在版本问题上是正确的。这是在2.7版本上测试的。我对2.1没有任何经验,所以我很难说出这两个版本之间的区别。