有没有办法隐藏/停止调试生成的一些输出



寻求帮助以了解为什么我有下面调试行的其他输出:

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

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

所以它看起来更像是:

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

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

我一直在以此为指导。

TL;博士;

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

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

你不能完全减少它,但你确实可以通过在loop_control属性中定义一个label来使循环不那么烦人

。在此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

或者,使用专用的 Ansible 测试:

when: item is changed

而不是你的实际

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

PS:由于Ansible 2.5loop应该优先于with_items

不带 "loop_control" ansible 代码

- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout  }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10

以下是使用loop_control之前的执行结果

TASK [Print message if CPU utilization become abnormal] ******************************************************************************************
ok: [ansiblenode] => (item={u'stderr_lines': [], u'cmd': u"mpstat -u 1 3|tail -1| awk '{print $NF}'n", u'end': u'2021-04-03 03:37:38.417169', u'stdout': u'0.00', u'changed': True, u'failed': False, u'delta': u'0:00:03.259453', u'stderr': u'', u'rc': 0, u'stdout_lines': [u'0.00'], u'start': u'2021-04-03 03:37:35.157716'}) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------

使用"loop_control"的 ansible 代码

注意:">loop_control"有助于避免在执行循环时出现调试消息

- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout  }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10
loop_control:
label: "{{ item.stdout }}"

以下是使用loop_control后的执行结果

TASK [Print message if CPU utilization become abnormal] *****************************************************
ok: [ansiblenode] => (item=0.00) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------'

最新更新