Difference between revisions of "Capturing output of shell command in ansible variable"

From Notes_Wiki
(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...")
 
m
 
Line 1: Line 1:
<yambe:breadcrumb>Ansible_tips_and_tricks|Ansible tips and tricks</yambe:breadcrumb>
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[System administration tools]] > [[Ansible|ansible]] > [[Ansible tips and tricks]] > [[Capturing output of shell command in ansible variable]]
=Capturing output of shell command in ansible variable=


To capture output of shell command in ansible variable use:
To capture output of shell command in ansible variable use:
Line 23: Line 22:
</pre>
</pre>
task
task
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[System administration tools]] > [[Ansible|ansible]] > [[Ansible tips and tricks]] > [[Capturing output of shell command in ansible variable]]

Latest revision as of 11:50, 28 July 2022

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