29

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Kimmel
  • 485
  • 1
  • 4
  • 12

5 Answers5

64

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
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • Thank you. Since I'm presently stuck on 2.4.2 are there any filters to achieve a similar capability? – Kimmel Apr 25 '18 at 18:07
  • 7
    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](https://github.com/ansible/ansible/issues/27078) – imjoseangel Apr 25 '18 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 Oct 06 '20 at 09:18
  • 1
    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 Jan 24 '21 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 – imjoseangel Jan 25 '21 at 13:30
6

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

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: https://docs.ansible.com/ansible/2.9/plugins/callback.html

bjorndown
  • 61
  • 1
  • 2
3

Here is a bug report:

https://github.com/ansible/ansible/issues/27078 - why is ansible's default output not more human readable... stilll?

The possible answers:

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

or:

ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook ....
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
2

Another option:

https://blog.alexgittings.com/improving-the-ansible-output-with-anstomlog/

just store it inside ansible/ansible.cfg

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

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

DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121
0

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)