Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return only the 'msg:' from Ansible playbook

Tags:

yaml

ansible

I have a simple playbook to query a package version

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

I get an output with a lot of junk that makes parsing/using the output difficult without edits:

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

I am trying find a solution to only return the msg without the "ok:", and JSON-esque junk that comes with it.

I have tried setting no_log:True in the YAML file and the ansible.cfg files with no luck.

like image 488
The Mob Avatar asked Oct 29 '25 19:10

The Mob


1 Answers

Use the callback community.general.diy (do it yourself). See

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

For example,

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

gives

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
like image 198
Vladimir Botka Avatar answered Oct 31 '25 11:10

Vladimir Botka