# How to get IP address from controller's /etc/hosts file?

**URL:** <https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376>\
**Category:** Ansible Project\
**Created:** [March 3, 2020, 8:38pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376 "2020-03-03T20:38:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [March 3, 2020, 8:38pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/1 "2020-03-03T20:38:38Z")

</div>

I’m trying to get the IP address of one of my remote hosts by querying my controller machine’s /etc/hosts file which happens to contain the IP addresses of all the servers I build with Ansible. My Ansible playbook looks like this:

`

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [March 3, 2020, 8:42pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/2 "2020-03-03T20:42:44Z")

</div>

Note that my awk command should say “print $1” to get the IP address. I don’t see a way to edit my original question here to make this change.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 3, 2020, 8:53pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/3 "2020-03-03T20:53:15Z")

</div>

Hi,

Not read everything but first thing is that if you have a pipe in a command call… you should use shell module instead… That should fix your first issue.

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![Juerg\_Ritter](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juerg\_Ritter](https://forum.ansible.com/u/Juerg_Ritter)\
**Post date:** [March 3, 2020, 9:31pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/4 "2020-03-03T21:31:40Z")

</div>

Hi Robert,

Try using the shell module instead of the command module. The command module doesn’t execute a command in a shell, hence you cannot use shell functionality such as a pipes and redirections.

Hope that helps!

–Juerg

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [March 3, 2020, 9:47pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/5 "2020-03-03T21:47:00Z")

</div>

I neglected to mention that I did try the shell and it did not fix the problem. But thanks for the suggestion!

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 3, 2020, 11:41pm UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/6 "2020-03-03T23:41:50Z")

</div>

Hii

If you

1. Properly format your playbook to be a list of plays
2. Use shell
3. Don’t use {{ }} around the var in your debug task

Then it works:

TASK [get file server’s IP address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [villa → localhost]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [villa] =\>  
fs\_ip\_addr:  
changed: true  
cmd: grep prod-fs1 /etc/hosts | awk ‘{ print $0 }’  
delta: ‘0:00:00.008973’  
end: ‘2020-03-04 00:36:05.873228’  
failed: false  
rc: 0  
start: ‘2020-03-04 00:36:05.864255’  
stderr: ‘’  
stderr\_lines:   
stdout: 10.10.10.20 prod-fs1  
stdout\_lines:

- 10.10.10.20 prod-fs1

But I would also recommend:

1. Dropping privilege escalation for local actions
2. Use “print $1” as this will match the actual IP
3. Use ‘stdout’ sa this will be the actual IP

Thus the playbook would be:

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [March 4, 2020, 4:50am UTC](https://forum.ansible.com/t/how-to-get-ip-address-from-controllers-etc-hosts-file/32376/7 "2020-03-04T04:50:01Z")

</div>

Dick,

> 

Thank you very much! Your solution worked. I realized after I posted my question that I should have said “print $1” but I didn’t see a way to edit my question. I had also forgotten that when you do “debug: var=…” that you have to drop the double braces; they’re only used when you do “debug: msg=…”. Removing privilege escalation and using stdout were new to me. Thanks again.
