• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Ansible – How to Store Playbook Result in Variable ?

August 25, 2018 By Cloud_Devops 2 Comments

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 !!!

Filed Under: Ansible engine, Configuration Management, DevOps Tagged With: Ansible, DevOps

Reader Interactions

Comments

  1. Dexter Adams says

    April 27, 2023 at 5:48 am

    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'”
    }

    Reply
  2. John Demand says

    January 13, 2020 at 7:16 pm

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version