Reputation: 577
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.
Upvotes: 33
Views: 84385
Reputation: 48753
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
Upvotes: 3
Reputation: 10315
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
Upvotes: 3
Reputation: 91
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
Upvotes: 9
Reputation: 3936
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
Upvotes: 74
Reputation: 11
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)
Upvotes: 1