Ansible: stdout callback plugin
The yaml stdout callback plugin is an ansible plugin to change the output in yaml format.
To install it we need to run the following command:
ansible-galaxy collection install community.general
Let’s try to run simple an ansible playbook before enabling the plugin (The stdout will be in JSON format):
PLAY [Dummy VM checks] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.1.16]
TASK [Checking VM started!] ************************************************************************************************************
ok: [192.168.1.16] => {
"msg": "Staring process...."
}
TASK [Show hostname] *******************************************************************************************************************
changed: [192.168.1.16]
TASK [Classify hosts depending on their OS distribution] *******************************************************************************
changed: [192.168.1.16]
TASK [Operating Systems] ***************************************************************************************************************
ok: [192.168.1.16] => {
"msg": "The operating system is Fedora"
}
TASK [Hostname] ************************************************************************************************************************
ok: [192.168.1.16] => {
"msg": "The operating system is linux-lab"
}
TASK [Facts] ***************************************************************************************************************************
ok: [192.168.1.16] => {
"msg": [
{
"block_available": 2029793,
"block_size": 4096,
"block_total": 3669248,
"block_used": 1639455,
"device": "/dev/sda3",
"fstype": "btrfs",
"inode_available": 0,
"inode_total": 0,
"inode_used": 0,
"mount": "/",
"options": "rw,seclabel,relatime,compress=zstd:1,ssd,space_cache=v2,subvolid=257,subvol=/root,bind",
"size_available": 8314032128,
"size_total": 15029239808,
"uuid": "de0fa6cc-f7f2-41ad-96a3-0215b13371ef"
},
{
"block_available": 2029793,
"block_size": 4096,
"block_total": 3669248,
"block_used": 1639455,
"device": "/dev/sda3",
"fstype": "btrfs",
"inode_available": 0,
"inode_total": 0,
"inode_used": 0,
"mount": "/home",
"options": "rw,seclabel,relatime,compress=zstd:1,ssd,space_cache=v2,subvolid=256,subvol=/home,bind",
"size_available": 8314032128,
"size_total": 15029239808,
"uuid": "de0fa6cc-f7f2-41ad-96a3-0215b13371ef"
},
{
"block_available": 152284,
"block_size": 4096,
"block_total": 249195,
"block_used": 96911,
"device": "/dev/sda2",
"fstype": "ext4",
"inode_available": 65135,
"inode_total": 65536,
"inode_used": 401,
"mount": "/boot",
"options": "rw,seclabel,relatime",
"size_available": 623755264,
"size_total": 1020702720,
"uuid": "0a6142b0-df7e-441c-896d-38133b3e37d1"
}
]
}
TASK [Get uptime information] **********************************************************************************************************
changed: [192.168.1.16]
TASK [Print return information from the previous task] *********************************************************************************
skipping: [192.168.1.16]
TASK [Ansible | Run a simple post task] ************************************************************************************************
ok: [192.168.1.16] => {
"msg": "All task have been performed"
}
PLAY RECAP *****************************************************************************************************************************
192.168.1.16 : ok=9 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
To enable/configure the yaml stdout callback plugin we need to add the following lines at the ansible configuration file (stdout callback plugin):
sudo vim /etc/ansible/ansible.cfg
[defaults]
stdout_callback = community.general.yaml
Then after running again the same ansible playbook we must be able to see the stdout in yaml format:
PLAY [Dummy VM checks] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.1.16]
TASK [Checking VM started!] ************************************************************************************************************
ok: [192.168.1.16] =>
msg: Staring process....
TASK [Show hostname] *******************************************************************************************************************
changed: [192.168.1.16]
TASK [Classify hosts depending on their OS distribution] *******************************************************************************
changed: [192.168.1.16]
TASK [Operating Systems] ***************************************************************************************************************
ok: [192.168.1.16] =>
msg: The operating system is Fedora
TASK [Hostname] ************************************************************************************************************************
ok: [192.168.1.16] =>
msg: The operating system is linux-lab
TASK [Facts] ***************************************************************************************************************************
ok: [192.168.1.16] =>
msg:
- block_available: 2029787
block_size: 4096
block_total: 3669248
block_used: 1639461
device: /dev/sda3
fstype: btrfs
inode_available: 0
inode_total: 0
inode_used: 0
mount: /
options: rw,seclabel,relatime,compress=zstd:1,ssd,space_cache=v2,subvolid=257,subvol=/root,bind
size_available: 8314007552
size_total: 15029239808
uuid: de0fa6cc-f7f2-41ad-96a3-0215b13371ef
- block_available: 2029787
block_size: 4096
block_total: 3669248
block_used: 1639461
device: /dev/sda3
fstype: btrfs
inode_available: 0
inode_total: 0
inode_used: 0
mount: /home
options: rw,seclabel,relatime,compress=zstd:1,ssd,space_cache=v2,subvolid=256,subvol=/home,bind
size_available: 8314007552
size_total: 15029239808
uuid: de0fa6cc-f7f2-41ad-96a3-0215b13371ef
- block_available: 152284
block_size: 4096
block_total: 249195
block_used: 96911
device: /dev/sda2
fstype: ext4
inode_available: 65135
inode_total: 65536
inode_used: 401
mount: /boot
options: rw,seclabel,relatime
size_available: 623755264
size_total: 1020702720
uuid: 0a6142b0-df7e-441c-896d-38133b3e37d1
TASK [Get uptime information] **********************************************************************************************************
changed: [192.168.1.16]
TASK [Print return information from the previous task] *********************************************************************************
skipping: [192.168.1.16]
TASK [Ansible | Run a simple post task] ************************************************************************************************
ok: [192.168.1.16] =>
msg: All task have been performed
PLAY RECAP *****************************************************************************************************************************
192.168.1.16 : ok=9 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0