Ansible playbooks/roles often used to complete the specific task which does not require an output. In some cases, you might need to capture the complex command output as results. The output would help to generate the required reports. In some cases, you might require to store configuration backup of the hosts. In this article, we will walk through to capture the output in a variable and display it.
Environment
- Ansible Server – ansible-server
- Remote hosts – gpfslinapp1
Register Task Output:
1. Create the playbook to execute the “df” command to check the /boot usage. Use “register” to store the output to a variable.
--- - hosts: all become: yes tasks: - name: Execute /boot usage on Hosts command: 'df -h /boot' register: dfboot
2. Run the playbook to see the result. Ensure that “gpfslinapp1” host in the inventory file “lin-servers”.
[linadm@ansible-server playbooks]$ ansible-playbook -i lin-servers df.boot.yaml PLAY [all] **************************************************************** TASK [Gathering Facts] **************************************************** ok: [gpfslinapp1] TASK [Execute /boot usage on Hosts] *************************************** changed: [gpfslinapp1] PLAY RECAP ***************************************************************** gpfslinapp1 : ok=2 changed=1 unreachable=0 failed=0 [linadm@ansible-server playbooks]$
The playbook ran “df -h /boot” command and register the output to variable “dfroot”.
3. Display the registered output using debug module. stdout keyword is used along with the variable name to display the output.
[linadm@ansible-server playbooks]$ cat df.boot.yaml --- - hosts: all become: yes tasks: - name: Execute /boot usage on Hosts command: 'df -h /boot' register: dfboot - debug: var=dfboot.stdout [linadm@ansible-server playbooks]$
4. Repeat the playbook execution to see the difference now.
[linadm@ansible-server playbooks]$ ansible-playbook -i ../lin-servers.1 df.boot.yaml PLAY [all] *********************************************************** TASK [Gathering Facts] *********************************************** ok: [gpfslinapp1] TASK [Execute /boot usage on Hosts] ********************************** changed: [gpfslinapp1] TASK [debug] ********************************************************* ok: [gpfslinapp1] => { "dfroot.stdout": "Filesystem Size Used Avail Use% Mounted on\n/dev/sda1 297M 155M 143M 53% /boot" } PLAY RECAP ********************************************************** gpfslinapp1 : ok=3 changed=1 unreachable=0 failed=0 [linadm@ansible-server playbooks]$
5. If you would like to display the variable output differently, you could replace the “stdout with “stdout_lines”.
[linadm@ansible-server playbooks]$ cat df.boot.yaml --- - hosts: all become: yes tasks: - name: Execute /boot usage on Hosts command: 'df -h /boot' register: dfboot - debug: var=dfboot.stdout_lines [linadm@ansible-server playbooks]$
6. Re-execute the playbook. Results will display the command output in aligned format.
[linadm@ansible-server playbooks]$ ansible-playbook -i ../lin-servers.1 df.boot.yaml PLAY [all] **************************************************************** TASK [Gathering Facts] **************************************************** ok: [gpfslinapp1] TASK [Execute /boot usage on Hosts] *************************************** changed: [gpfslinapp1] TASK [debug] *************************************************************** ok: [gpfslinapp1] => { "dfboot.stdout_lines": [ "Filesystem Size Used Avail Use% Mounted on", "/dev/sda1 297M 155M 143M 53% /boot" ] } PLAY RECAP **************************************************************** gpfslinapp1 : ok=3 changed=1 unreachable=0 failed=0 [linadm@ansible-server playbooks]$
“stdout_lines” just display the command output without any modification in JASON format.
Hope this article is informative to you. Share it! Comment it !! Be Sociable !!!
Dexter Adams says
Hi
I tried that and getting an error:
19 schema: vsphere
20 properties:
21 – name
22 delegate_to: localhost
23 register: dsinfo
ok: [localhost] => {
“dsinfo.stdout_lines”: “VARIABLE IS NOT DEFINED!: ‘dict object’ has no attribute ‘stdout_lines’. ‘dict object’ has no attribute ‘stdout_lines'”
}
John Demand says
Great article – Ansible – How to Store Playbook Result in Variable!! Question, is it possible to precede the output…
Size Used Avail Use% Mounted on\n/dev/sda1 297M 155M 143M 53% /boot
…with the $hostname of the server the df is being executed on?
server1: Size Used Avail Use% Mounted on\n/dev/sda1 297M 155M 143M 53% /boot
server2: Size Used Avail Use% Mounted on\n/dev/sda1 297M 155M 143M 53% /boot
Also, how can this output be written to one file that I can view the results of all the servers I’ve run the playbook against?
Thanks,
John