Ansible – register variable

Often we require to store the result of our command outputs in any variables, so we can use them further as inputs or just display them on our output screen.

Ansible does offer us to store or print the output results, this can be done using the Ansible register variable.

We have covered Ansible Variables, and the Ansible Data Types in our earlier post,

Syntax:

---
 - name: Lets learn Ansible registry
   hosts: localhost

   tasks:
   - name: lets get the bash version
     shell: "bash --version"
     register: bash_version

   - name: lets print the raw output  
     debug:
      msg:
      - "The shell module raw output "
      - "{{bash_version}}"

In the above ansible playbook, we are running the command bash –version. Ansible will run the command but will not display the output.

The register will store the output value of the bash –version command and we can further display the output using the debug module.

Ansible registry

Now let’s play around with the output and get the specific output we want, in our case we need the bash version from the above dictionary/map.

We have covered the Ansible Map data type,

   - name: lets print specific key from output value
     debug:
      msg: # Print array/dictonary/map values 
      - "We can print specific key in three different ways"
      - "Method 1 : Output stdout key from raw output -  " 
      - "{{bash_version.stdout}}"
      - "Method 2 : Output stdout key from raw output -  "
      - "{{bash_version['stdout']}}"
      - "Method 3 : Output stdout key from raw output -  "
      - "{{bash_version.get('stdout')}}"

   

In our case, the output is in Map/Dictionary format, use the key name to fetch specific values from the Map.

ansible output

Now, let’s split the string using Ansible Split function, as we can see in the below code snippet, we can go on extracting the pattern from the string as per our requirements.

- name: lets split string in the output value 
     debug:
      msg:
      - "Lets Split the output value string - "
      - "{{bash_version.stdout.split('\n')}}"
      - "lets get the first string value - "
      - "{{bash_version.stdout.split('\n')[0]}}"
      - "Now, lets get further stplit it with space - "
      - "{{bash_version.stdout.split('\n')[0].split()}}"
      - "Lets print the version - "
      - "{{bash_version.stdout.split('\n')[0].split()[3]}}"
ansible split string

You can find the full ansible playbook code below,

---
 - name: Lets learn Ansible registry
   hosts: localhost

   tasks:
   - name: lets get the bash version
     shell: "bash --version"
     register: bash_version

   - name: lets print the raw output  
     debug:
      msg:
      - "The shell module raw output "
      - "{{bash_version}}"

   - name: lets print specific key from output value
     debug:
      msg: # Print array/dictonary/map values 
      - "We can print specific key in three different ways"
      - "Method 1 : Output stdout key from raw output -  " 
      - "{{bash_version.stdout}}"
      - "Method 2 : Output stdout key from raw output -  "
      - "{{bash_version['stdout']}}"
      - "Method 3 : Output stdout key from raw output -  "
      - "{{bash_version.get('stdout')}}"

   - name: lets split string in the output value 
     debug:
      msg:
      - "Lets Split the output value string - "
      - "{{bash_version.stdout.split('\n')}}"
      - "lets get the first string value - "
      - "{{bash_version.stdout.split('\n')[0]}}"
      - "Now, lets get further stplit it with space - "
      - "{{bash_version.stdout.split('\n')[0].split()}}"
      - "Lets print the version - "
      - "{{bash_version.stdout.split('\n')[0].split()[3]}}"

Summary

  • Ansible registers can be used to output the module into the variables.
  • These variables can be used further to print the output and used as input variables in the playbook.