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
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).
I’m a little confused about the use case here, but it seems like you could just get the IP address from the fact.
@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.
Ditto.
Brian, thanks for the suggestion on pulling the IP from the
environment on the target machine. Not ideal, but certainly adequate.
==ml
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
I'll add to my list, previous attempt was hacked at the same level as
inventory_hostname, but facts seems like a better placing.
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.