# ansible shell variable

**URL:** <https://forum.ansible.com/t/ansible-shell-variable/33019>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 24, 2020, 11:55pm UTC](https://forum.ansible.com/t/ansible-shell-variable/33019 "2020-05-24T23:55:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 24, 2020, 11:55pm UTC](https://forum.ansible.com/t/ansible-shell-variable/33019/1 "2020-05-24T23:55:52Z")

</div>

Hello All,

I have an ansible script that I am writing to use meta data from an aws ec2 instance. I want to create and image of a list og servers from my inventory file them launch each one, then perform a function then terminate the instances.I am currently stuck on trying to get the subnet portion of the meta data as I need to perform two commands.

1. get the mac address
2. get the subnet

But I am not able to successfully get the subnet id

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 25, 2020, 12:40am UTC](https://forum.ansible.com/t/ansible-shell-variable/33019/2 "2020-05-25T00:40:17Z")

</div>

> 1) get the mac address  
> 2) get the subnet
> 
> ---  
> - hosts: dev  
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;#connection: local  
> &nbsp;&nbsp;gather\_facts: true

Run the command below a see what "facts" are available when "gather\_facts:  
true" is enabled.

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 25, 2020, 3:31am UTC](https://forum.ansible.com/t/ansible-shell-variable/33019/3 "2020-05-25T03:31:12Z")

</div>

This is the output:

`

fatal: [deVPN]: FAILED! =\> changed=true  
cmd: |-  
ansible dev -m setup  
delta: ‘0:00:00.033604’  
end: ‘2020-05-24 23:29:51.883830’  
msg: non-zero return code  
rc: 127  
start: ‘2020-05-24 23:29:51.850226’  
stderr: ‘/bin/bash: ansible: command not found’  
stderr\_lines:   
stdout: ‘’  
stdout\_lines:

`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 25, 2020, 5:16am UTC](https://forum.ansible.com/t/ansible-shell-variable/33019/4 "2020-05-25T05:16:28Z")

</div>

> \> \> 1) get the mac address  
> \> \> 2) get the subnet  
> \> \>  
> \> \> ---  
> \> \> - hosts: dev  
> \> \> become: true  
> \> \> #connection: local  
> \> \> gather\_facts: true

> \> Run the command below a see what "facts" are available when "gather\_facts:  
> \> true" is enabled.  
> \> \> ansible dev -m setup

> This is the output:
> 
> fatal: [deVPN]: FAILED! =\> changed=true  
> &nbsp;&nbsp;cmd: |-  
> &nbsp;&nbsp;&nbsp;&nbsp;ansible dev -m setup  
> &nbsp;&nbsp;delta: '0:00:00.033604'  
> &nbsp;&nbsp;end: '2020-05-24 23:29:51.883830'  
> &nbsp;&nbsp;msg: non-zero return code  
> &nbsp;&nbsp;rc: 127  
> &nbsp;&nbsp;start: '2020-05-24 23:29:51.850226'  
> &nbsp;&nbsp;stderr: '/bin/bash: ansible: command not found'  
> &nbsp;&nbsp;stderr\_lines: \<omitted\>  
> &nbsp;&nbsp;stdout: ''  
> &nbsp;&nbsp;stdout\_lines: \<omitted\>

This is a misunderstanding. Do not run the command with ansible-playbook as  
"shell" task on the remote host!

The "command" shall be run from the command-line on controller. The output  
will show all facts collected by the module "setup" on the remote host. The  
same facts will be collected automatically by a playbook when not disabled by  
"gather\_facts: false". See  
[https://docs.ansible.com/ansible/latest/modules/setup\_module.html#examples](https://docs.ansible.com/ansible/latest/modules/setup_module.html#examples)

&nbsp;&nbsp;\> ansible dev -m setup

Find the variables that fit your purpose. For example

&nbsp;&nbsp;\> cat playbook.yml  
&nbsp;&nbsp;- hosts: dev  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_default\_ipv4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "MAC: {{ ansible\_default\_ipv4.macaddress }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Network: {{ ansible\_default\_ipv4.network }}"

&nbsp;&nbsp;\> ansible-playbook playbook.yml

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 25, 2020, 11:57pm UTC](https://forum.ansible.com/t/ansible-shell-variable/33019/5 "2020-05-25T23:57:19Z")

</div>

Thank you very much for the help
