寻求帮助以了解为什么我从下面的调试行获得额外的输出:
- name: Check kernel diff
become: true
shell: "sdiff {{ item }}/pre-kernel.out {{ item }}/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs"
register: "kernel"
with_items: "{{work_dir}}"
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
when: "{{ item.changed == true }}"
with_items: "{{ kernel.results}}"
Run Code Online (Sandbox Code Playgroud)
msg 的输出是正确的,但如何停止/隐藏它之前出现的所有详细信息:
TASK [patching : debug] ************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item.changed == true }}
ok: [test02] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-05-26 16:23:37.068718', u'stderr': u'', u'stdout': u'4.14.35-1902.302.2.el7uek.x86_64', u'changed': True, 'failed': False, u'delta': u'0:00:00.008894', u'cmd': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", 'item': u'/var/tmp/patching_2020-05-26', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'4.14.35-1902.302.2.el7uek.x86_64'], u'start': u'2020-05-26 16:23:37.059824'}) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
Run Code Online (Sandbox Code Playgroud)
所以它看起来更像是:
TASK [patching : debug] ************************
ok: [test02] => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
Run Code Online (Sandbox Code Playgroud)
var_file.yml以供参考:
---
work_dir:
- /var/tmp/patching_{{ansible_date_time.date}}
Run Code Online (Sandbox Code Playgroud)
我一直用这个作为指导。
根据您的具体示例,这应该是您的debug任务:
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop: "{{ kernel.results }}"
when: item.changed
loop_control:
label: 'kernel'
Run Code Online (Sandbox Code Playgroud)
哪个会输出
TASK [debug] *******************************************************************
ok: [local] => (item=kernel) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
Run Code Online (Sandbox Code Playgroud)
您无法按照自己的意愿完全减少它,但您确实可以通过label在loop_controlattribute中定义 a 来使循环变得不那么烦人的冗长。
在这里,label您可以选择当 Ansible 循环时显示字典的属性之一,而不是完整的字典。
TASK [debug] *******************************************************************
ok: [local] => (item=kernel) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
Run Code Online (Sandbox Code Playgroud)
这将给出一个像这样的回顾
TASK [debug] *******************************************************************
ok: [local] => (item=bar) => {
"msg": "The system is now running Kernel Version bar"
}
Run Code Online (Sandbox Code Playgroud)
与没有的杂乱的相比loop_control:
TASK [debug] *******************************************************************
ok: [local] => (item={'some': 'foo', 'dict': 'foo', 'with': 'foo', 'an': 'foo', 'annoyingly': 'foo', 'long': 'foo', 'list': 'foo', 'of': 'foo', 'attributes': 'foo', 'name': 'bar', 'stdout': 'bar'}) => {
"msg": "The system is now running Kernel Version bar"
}
Run Code Online (Sandbox Code Playgroud)
另外,为了修复您的警告,您只需要删除 Jinja 大括号,就像when总是假设您将传递 Jinja 表达式一样。
测试某些东西== true是否也是不必要的额外冗长when: something-that-evaluates-to-true就足够了。
所以你应该使用
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop:
- some: foo
dict: foo
with: foo
an: foo
annoyingly: foo
long: foo
list: foo
of: foo
attributes: foo
name: bar
stdout: bar
loop_control:
label: "{{ item.name }}"
Run Code Online (Sandbox Code Playgroud)
或者,使用有目的的 Ansible 测试:
TASK [debug] *******************************************************************
ok: [local] => (item=bar) => {
"msg": "The system is now running Kernel Version bar"
}
Run Code Online (Sandbox Code Playgroud)
而不是你实际的
TASK [debug] *******************************************************************
ok: [local] => (item={'some': 'foo', 'dict': 'foo', 'with': 'foo', 'an': 'foo', 'annoyingly': 'foo', 'long': 'foo', 'list': 'foo', 'of': 'foo', 'attributes': 'foo', 'name': 'bar', 'stdout': 'bar'}) => {
"msg": "The system is now running Kernel Version bar"
}
Run Code Online (Sandbox Code Playgroud)
PS:因为 Ansible 2.5loop应该优先于with_items.