首页 > 解决方案 > Is there a way to hide/stop some output generated by debug

问题描述

Looking for help to understand why I have the additional output from the debug line below:

- 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}}"

The output from msg is correct, but how can I stop/hide all the detail that appears before it:

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"
}

So it looks more like just:

TASK [patching : debug] ************************
ok: [test02] => {
    "msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}

var_file.yml for reference:

---
work_dir:
  - /var/tmp/patching_{{ansible_date_time.date}}

I've been using this as guidance

标签: ansible

解决方案


TL;博士;

根据您的确切示例,这应该是您的调试任务:

- debug: 
    msg: "The system is now running Kernel Version {{ item.stdout }}"
  loop: "{{ kernel.results }}"
  when: item.changed
  loop_control:
    label: 'kernel'

哪个会输出

TASK [debug] *******************************************************************
ok: [local] => (item=kernel) => {
    "msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}

你不能完全减少它,但你确实可以通过labelloop_control属性中定义 a 来使循环不那么冗长。

在此label,您可以选择在 Ansible 循环而不是完整字典时显示字典的属性之一。

- 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 }}"

这将使回顾看起来像这样

TASK [debug] *******************************************************************
ok: [local] => (item=bar) => {
    "msg": "The system is now running Kernel Version bar"
}   

与没有的杂乱无章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"
}

此外,为了修复您的警告,您只需要删除 Jinja 花括号,就像when始终假设您要传递 Jinja 表达式一样。
并且测试某些东西== true是否也是不必要的额外冗长,when: something-that-evaluates-to-true就足够了。

所以你应该使用

  when: item.changed

而不是你的实际

  when: "{{ item.changed == true }}"

loopPS:因为应该首选Ansible 2.5而不是with_items.


推荐阅读