Capturing output of shell command in ansible variable

From Notes_Wiki
Revision as of 11:50, 28 July 2022 by Saurabh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible tips and tricks > Capturing output of shell command in ansible variable

To capture output of shell command in ansible variable use:

    - shell: ifconfig | grep mtu | grep [ep][n0-9]p[0-9] | sed 's/:.*$//g'
      register: ifconfig_output

Here register is a variable name which will capture exit status, output, etc. For accessing the output use:

    - name: Disable management of p2p1 by network-manager
      shell: "echo NM_CONTROLLED=no >> /etc/sysconfig/network-scripts/ifcfg-{{ifconfig_output.stdout}}"

The output can also be used in a template such as:

dhclient -v {{ifconfig_output.stdout}}

Thus, ifconfig_output is a dictionary and output is stored in stdout. Other things stored in ifconfig_output register can be found using

- debug msg="{{ifconfig_output}}"

task



Home > CentOS > CentOS 6.x > System administration tools > ansible > Ansible tips and tricks > Capturing output of shell command in ansible variable