Capturing output of shell command in ansible variable

From Notes_Wiki
Revision as of 06:08, 7 August 2015 by Saurabh (talk | contribs) (Created page with "<yambe:breadcrumb>Ansible_tips_and_tricks|Ansible tips and tricks</yambe:breadcrumb> =Capturing output of shell command in ansible variable= To capture output of shell comman...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<yambe:breadcrumb>Ansible_tips_and_tricks|Ansible tips and tricks</yambe:breadcrumb>

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