Capturing output of shell command in ansible variable
From Notes_Wiki
<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