Tuesday, August 29, 2023

Format Output in Ansible

 

 Ansible

To properly format the ouput of the ansible playbook output

Create a ansible.cfg as follows and add the following content

/etc/ansible/ansible.cfg

[defaults]
# Use the YMAL callback plugin
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands
bin_ansible_callbacks = True
interpreter_python = auto_silent

Execute the playbook and you will see the formatted output as follows

ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook shell.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Execute shell command] ******************************************************************************************************************************************************************************
changed: [localhost] => changed=true
  cmd: |-
    pwd
    ls
    df -h
  delta: '0:00:00.007618'
  end: '2021-04-23 20:52:56.904928'
  rc: 0
  start: '2021-04-23 20:52:56.897310'
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    /home/ansible/Desktop
    docker.yml
    pip.yml
    shell.sh
    shell.yml
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           199M  1.4M  198M   1% /run
    /dev/sda3        31G  9.7G   20G  34% /
    tmpfs           992M  128K  992M   1% /dev/shm
    tmpfs           5.0M  4.0K  5.0M   1% /run/lock
    tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
    /dev/sda2       512M  7.9M  505M   2% /boot/efi
    tmpfs           199M  104K  199M   1% /run/user/1000
    /dev/sr0         59M   59M     0 100% /media/ansible/VBox_GAs_6.1.18
  stdout_lines: <omitted>

TASK [Execute shell script] *******************************************************************************************************************************************************************************
changed: [localhost] => changed=true
  cmd: /home/ansible/Desktop/shell.sh
  delta: '0:00:00.008033'
  end: '2021-04-23 20:52:57.244745'
  rc: 0
  start: '2021-04-23 20:52:57.236712'
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    Running a Ansible Playbook
    Friday 23 April 2021 08:52:57 PM IST
    7
  stdout_lines: <omitted>

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

ansible@ansible-VirtualBox:~/Desktop$