【问题标题】:How to disable json output from specific ansible commands?如何禁用特定 ansible 命令的 json 输出?
【发布时间】:2015-09-09 09:29:45
【问题描述】:

一些 ansible 命令会产生人类几乎无法阅读的 json 输出。当人们需要检查剧本是否正确执行时,它会分散人们的注意力并造成混乱。

示例命令是shellreplace - 它们会产生很多无用的噪音。我怎样才能防止这种情况?简单ok |改变 |失败就足够了。我不需要整个 JSON。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    在您想要抑制所有进一步输出的任务上使用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.0no_log: Trueshellcopyreplace 命令没有影响。提示可能是我对所有这些命令都使用了with_items
    • 嗯,2.0.0 是一个 alpha 版本。我在上面添加了一个示例,它适用于 Ansible 1.9.2
    • 看来 v 2.0.0-0.3.beta1 在消息输出方面表现得更加人性化。
    • 在 Ansible 2.6.1 中使用 set_fact ... with_dict 进行了尝试,而不是 JSON,我得到每个键/值对的“=> (item=None)”输出。
    • Ansible 2.8 中的输出明显不同。现在显示如下ok: [localhost] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}
    【解决方案2】:

    您可以使用 -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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多