仅返回Ansible playbook中的'msg:'。

4

我有一个简单的playbook来查询软件包版本

---
- name: version find
  hosts: all
  become: yes


    tasks:
          - name: query
            shell: *shell command*
            register: info
            no_log: true

           - debug:
                  msg: " {{inventory_hostname}} blah {{info.stdout}}blah"

我得到了一个输出,其中包含大量垃圾内容,如果不进行编辑,解析/使用该输出将变得困难:

   ok: [hostname] => {
    "msg": " *hostname* blah *info* blah"
   }

我正在尝试找到一种解决方案,只返回消息而不带有“ok:”和类JSON的垃圾信息。

我已经尝试在YAML文件和ansible.cfg文件中设置no_log:True,但没有成功。


2
看起来你想了解 playbook callback plugins 是做什么的,并选择你最喜欢的或者编写一个按照你的期望行为运作的插件。ansible-doc -t callback --list可以显示你当前已安装的插件。 - mdaniel
1
仅基于这个例子,你可能会更喜欢临时版本: ansible -i ./whatever -m shell -a "my-thing --version" all,因为它的性质更加保守。 - mdaniel
1个回答

6

使用回调函数community.general.diy(即“自己动手做”)。请参见

shell> ansible-doc -t callback community.general.diy

例如:

shell> cat pb.yml
- hosts: localhost

  tasks:

    - command: uname -o
      register: info

    - debug:
        msg: ""
      vars:
        ansible_callback_diy_runner_on_ok_msg: |
          msg: {{ inventory_hostname }} {{ info.stdout }}

提供

shell> ANSIBLE_STDOUT_CALLBACK=community.general.diy ansible-playbook pb.yml

PLAY [localhost] *****************************************************************************

TASK [command] *******************************************************************************
changed: [localhost]

TASK [debug] *********************************************************************************
msg: localhost GNU/Linux

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接