33

Assuming the below tasks:

- shell: "some_script.sh"
  register: "some_script_result"

- debug:
    msg: "Output: {{ some_script_result.stdout_lines }}

I receive the below output:

"msg": "Output: [u'some_value',u'some_value2,u'some_value3]"

How do I get the output to print as?

"msg":
Output:
  - some_value
  - some_value2
  - some_value3

Ansible version is 2.4.2.

0

5 Answers 5

73

Try this option. You’ll love it.

There's a new YAML callback plugin introduced with Ansible 2.5 — meaning any machine running Ansible 2.5.0 or later can automatically start using this format without installing custom plugins.

To use it, edit your ansible.cfg file (either globally, in /etc/ansible/ansible.cfg, or locally in your playbook/project), and add the following lines under the [defaults] section:

# Use the YAML callback plugin.
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True

Now I can easily read through your output message

If you get the following error:

ERROR! Invalid callback for stdout specified: yaml

run

ansible-galaxy collection install community.general
5
  • Thank you. Since I'm presently stuck on 2.4.2 are there any filters to achieve a similar capability?
    – Kimmel
    Commented Apr 25, 2018 at 18:07
  • 9
    Ansible 2.4+ has built-in support for human-readable results: Temporarily by setting ANSIBLE_STDOUT_CALLBACK=debug in the environment export ANSIBLE_STDOUT_CALLBACK=debug or permanently by setting stdout_callback=debug in the [default] section of ansible.cfg [default] # human-readable stdout/stderr results display stdout_callback = debug From Github Commented Apr 25, 2018 at 18:11
  • 2
    I couldn't see ansible.cfg file in/etc/ansible location. But there is a different file named ansible.cfg.rpmsave. Editing this file also fix the issue. Just for anyone confused about not having ansible.cfg file.
    – Natsu
    Commented Oct 6, 2020 at 9:18
  • 2
    I generally have a copy of ansible.cfg in the same directory as where I'm executing ansible or ansible-playbook. I know someone somewhere says that isn't a good idea, but I do it anyways.
    – FilBot3
    Commented Jan 24, 2021 at 23:16
  • Having an ansible.cfg is a perfect option to me. Do you know why couldn't it be a good idea? :D Commented Jan 25, 2021 at 13:30
9

What I found to work best so far for getting CLI-like output in Ansible, and which should work out of the box (at least for me on Fedora 34, Ansible 2.9), is setting the unixy callback for condensed Ansible output

stdout_callback = unixy
bin_ansible_callbacks = True

in your ansible.cfg. Given the tasks

  tasks:
    - name: uptime
      shell: uptime
    - name: volumes
      shell: "df -h"

the output in the terminal will look like

- all on hosts: all -
uptime...
  host1 done | stdout:  08:20:09 up 33 min,  1 user,  load average: 0.55, 0.27, 0.26
  host2 done | stdout:  08:20:09 up 1 day,  1:39,  1 user,  load average: 0.18, 0.17, 0.17

volumes...
  host1 done | stdout: Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  1.5G  5.4G  21% /overlay/pivot
devtmpfs        212M     0  212M   0% /dev
none            217M     0  217M   0% /overlay/pivot/overlay
none            217M  137M   80M  64% /overlay/rwdata
overlay         217M  137M   80M  64% /
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M   25M  192M  12% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           217M     0  217M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   53M  200M  21% /boot
tmpfs            44M     0   44M   0% /run/user/1000
  host2 done | stdout: Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  1.5G  5.4G  22% /overlay/pivot
devtmpfs        212M     0  212M   0% /dev
none            217M     0  217M   0% /overlay/pivot/overlay
none            217M  103M  114M  48% /overlay/rwdata
overlay         217M  103M  114M  48% /
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M  5.8M  211M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           217M     0  217M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   53M  200M  21% /boot
tmpfs            44M     0   44M   0% /run/user/1000


- Play recap -
  host1           : ok=1    changed=1    unreachable=0    failed=0    rescued=0    ignored=0   
  host2           : ok=1    changed=1    unreachable=0    failed=0    rescued=0    ignored=0   

You should be able to list all available callback plugins using ansible-doc -t callback -l and their respective documentation using ansible-doc -t callback <plugin name>

Source Documentation

3

Another Option

Another option of Improving the Ansible output is downloading the callbacks\anstomlog.py from Ansible STDOUT Compact Logger, store it under

tree ansible     
ansible
├── ansible.cfg
├── callbacks
│   ├── anstomlog.py
└── playbooks
    └── nginx.yaml

and configure inside ansible/ansible.cfg.

ANSIBLE_CONFIG=ansible/ansible.cfg ansible-playbook -u centos --private-key .ssh/key -i `terraform output bastion_ip`, ansible/playbooks/nginx.yaml

enter image description here

3

The possible answers is using an other Callback Plugin

[defaults]
nocows = True
# minimal, debug, yaml
stdout_callback = yaml

or

ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook ....

Reference / Bug Report

1

I was trying do the following changes, still i did not got the output format of yaml or debug options output properly. Later i found the ansible user configuration pick up it from user's config file. So i changed callbacks at user specific location and it worked.

[automation@ansibleserver ~]$ ansible --version
   ansible 2.9.9
   config file = /home/automation/ansible.cfg


# Use the YAML callback plugin.
   stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True

So ensure, even though you changed at /etc/ansible/ansible.conf , try at user's config file too to get the good result. instead of yaml , debug also gives good helpful format ( for linux users)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.