【发布时间】:2015-09-09 09:29:45
【问题描述】:
一些 ansible 命令会产生人类几乎无法阅读的 json 输出。当人们需要检查剧本是否正确执行时,它会分散人们的注意力并造成混乱。
示例命令是shell 和replace - 它们会产生很多无用的噪音。我怎样才能防止这种情况?简单ok |改变 |失败就足够了。我不需要整个 JSON。
【问题讨论】:
标签: ansible
一些 ansible 命令会产生人类几乎无法阅读的 json 输出。当人们需要检查剧本是否正确执行时,它会分散人们的注意力并造成混乱。
示例命令是shell 和replace - 它们会产生很多无用的噪音。我怎样才能防止这种情况?简单ok |改变 |失败就足够了。我不需要整个 JSON。
【问题讨论】:
标签: ansible
在您想要抑制所有进一步输出的任务上使用no_log: true。
- shell: whatever
no_log: true
我相信在FAQ 中唯一提及此功能。
示例剧本:
- hosts:
- localhost
gather_facts: no
vars:
test_list:
- a
- b
- c
tasks:
- name: Test with output
shell: echo "{{ item }}"
with_items: test_list
- name: Test w/o output
shell: echo "{{ item }}"
no_log: true
with_items: test_list
示例输出:
TASK: [Test with output] ******************************************************
changed: [localhost] => (item=a)
changed: [localhost] => (item=b)
changed: [localhost] => (item=c)
TASK: [Test w/o output] *******************************************************
changed: [localhost]
changed: [localhost]
changed: [localhost]
【讨论】:
ansible-playbook 2.0.0 和no_log: True 对shell、copy 和replace 命令没有影响。提示可能是我对所有这些命令都使用了with_items。
set_fact ... with_dict 进行了尝试,而不是 JSON,我得到每个键/值对的“=> (item=None)”输出。
ok: [localhost] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}
您可以使用 -o - ansible 命令的一行输出(不能使用 ansible-playbook):
ansible -o -m shell -a 'command' target
它将主机名、命令返回码和命令输出放在同一行:
hostname1 | CHANGED | rc=0 | (stdout) command output
hostname2 | CHANGED | rc=0 | (stdout) command output
hostname3 | CHANGED | rc=0 | (stdout) command output
【讨论】: