# template IP address for ansible\_ssh\_host?

**URL:** <https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364>\
**Category:** Ansible Project\
**Created:** [August 26, 2013, 6:07pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364 "2013-08-26T18:07:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Lucas](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@Michael\_Lucas](https://forum.ansible.com/u/Michael_Lucas)\
**Post date:** [August 26, 2013, 6:07pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/1 "2013-08-26T18:07:57Z")

</div>

Hi,

I want to use the IP address of the hostname in ansible\_ssh\_host in a  
template. Is there an easy way to get this?

Details:

A bunch of my hosts have multiple IP addresses and/or interfaces. I  
want to attach sshd to a specific address, and manage sshd\_config via  
ansible.

While OpenSSH accepts a hostname as a ListenAddress parameter, I'm not  
comfortable with doing so. Even with DNSSec.

I could define ansible\_ssh\_host as an IP address for every host, or  
add a host variable mgmt\_addr or somesuch, but that requires extra  
maintenance.

Any assistance appreciated!

==ml

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 26, 2013, 6:16pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/2 "2013-08-26T18:16:07Z")

</div>

SSH\_CONNECTION env variable should have ip you ssh’ed into host with, should be easy to retrieve with a shell command and register.

also you have ansible\_default\_ipv4.address, which gives you the default address for the host (in my case it always matches hostname address).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 26, 2013, 7:56pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/3 "2013-08-26T19:56:04Z")

</div>

I’m a little confused about the use case here, but it seems like you could just get the IP address from the fact.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 26, 2013, 8:08pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/4 "2013-08-26T20:08:37Z")

</div>

@Micheal, for the same reason I once tried to add 'inventory\_ip' as a magic  
variable, sometimes you cannot clearly get from facts which IP corresponds  
to the 'management' one you use to access the host. It can be temporary,  
dependent on client dns zone, transitory, etc.

---

<div class="post-metadata">

**Author:** ![Michael\_Lucas](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@Michael\_Lucas](https://forum.ansible.com/u/Michael_Lucas)\
**Post date:** [August 26, 2013, 8:16pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/5 "2013-08-26T20:16:35Z")

</div>

Ditto.

Brian, thanks for the suggestion on pulling the IP from the  
environment on the target machine. Not ideal, but certainly adequate.

==ml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 26, 2013, 9:33pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/6 "2013-08-26T21:33:56Z")

</div>

Brian,

I’m fine for something like ssh\_ip\_address to get set by the facts (setup) module if you can get it reliably.

Vaguely recall that one but it’s been a while so it doesn’t matter 🙂

–Michael

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 26, 2013, 9:44pm UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/7 "2013-08-26T21:44:02Z")

</div>

I'll add to my list, previous attempt was hacked at the same level as  
inventory\_hostname, but facts seems like a better placing.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 27, 2013, 1:39am UTC](https://forum.ansible.com/t/template-ip-address-for-ansible-ssh-host/13364/8 "2013-08-27T01:39:56Z")

</div>

Yeah that’s probably why it didn’t get through last time, I was unwilling to add the extra lookups.

If we can figure it out at fact level via the environment it would be practical zero cost.
