Warm tip: This article is reproduced from serverfault.com, please click

其他-如何使Ansible仅打印具有stdout的任务

(其他 - How to make Ansible print only the task that has stdout)

发布于 2020-11-30 06:41:13

我有一本包含多个任务的剧本。我需要Ansible仅打印包含STDOUT / debug任务的任务

- hosts: "{{ v_host }}"
gather_facts: no
tasks:

- name: "Create /tmp in target in all VM's "
  file:
    path: /tmp
    state: directory
    owner: oracle
    group: oinstall
    mode: 0775

- copy:
    src: /xxx
    dest: /yyy
    owner: oracle
    group: oinstall
    force: yes
    mode: 0755

- name:  "Performing {{ patch_action }} of CPU patch on {{ v_host }} "
  shell: |
    cd /tmp
    sudo -u oracle ./patch.sh
  register: out
  
- debug: var=out.stdout_lines

我执行不带-v选项的剧本以获得更清晰的输出。输出如下所示。

ansible-playbook test.yml --extra-vars v_host=xyz.com.
    
TASK [Create /tmp in target in all VM's] ********************************************************************
ok: [ixyz.com]

TASK [copy] ********************************************************************
ok: [xyz.com]

TASK [Performing  of CPU patch on xyz.com] ***
changed: [xyz.com]

TASK [debug] *******************************************************************
ok: [xyz.com] => {
    "out.stdout_lines": [
        "Patch  123456     : applied on Tue Sep 29 10:11:58 GMT 2020", 
        "Patch description:  \"one-off\"", 
        "Patch  987654     : applied on Tue Sep 29 10:08:39 GMT 2020", 
        "Patch description:  \"One-off\"", 

    ]
}

我执行剧本时是否有选择,它只为stdout / debug提供输出。我唯一需要的输出是

TASK [debug] *******************************************************************
ok: [xyz.com] => {
    "out.stdout_lines": [
        "Patch  123456     : applied on Tue Sep 29 10:11:58 GMT 2020", 
        "Patch description:  \"one-off\"", 
        "Patch  987654     : applied on Tue Sep 29 10:08:39 GMT 2020", 
        "Patch description:  \"One-off\"", 

    ]
}
Questioner
Meenakshisundaram Ramanathan
Viewed
0
Vladimir Botka 2020-11-30 21:36:51

通过使用actionable回调,可以仅选择已更改的任务请参阅“ ansible-doc -t可操作的回调”

“当你不关心“确定”或“跳过”时,请使用此回调。此回调可抑制任何非“失败”或“已更改”状态。

例如,下面的剧本

shell> cat pb.yml
- hosts: localhost
  tasks:
    - stat:
        path: /etc/passwd
      register: result
    - debug:
        var: result.stat.mode
    - debug:
        msg: test message
      changed_when: actionable|default(false)|bool

shell> ansible-playbook pb.yml

PLAY [localhost] ****

TASK [stat] ****
ok: [localhost]

TASK [debug] ****
ok: [localhost] => {
    "result.stat.mode": "0644"
}

TASK [debug] ****
ok: [localhost] => {
    "msg": "test message"
}

PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

可操作的回调将抑制输出。例如

shell> ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook pb.yml

PLAY [localhost] ****

PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

你可以通过控制其更改的状态来启用所选任务的输出例如

shell> ANSIBLE_STDOUT_CALLBACK=actionable ansible-playbook pb.yml -e "actionable=true"

PLAY [localhost] ****

TASK [debug] ****
changed: [localhost] => {
    "msg": "test message"
}

PLAY RECAP ****
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

问:如何使用“默认”而不是“可操作”执行我的剧本?

答:下面的命令给出相同的结果

shell> ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_DISPLAY_SKIPPED_HOSTS=false ANSIBLE_DISPLAY_OK_HOSTS=false  ansible-playbook pb.yml -e "actionable=true"