ansible_fqdn only contains the hostname

I’ve been having trouble with this for a while…
On Ubuntu 14.04 (current LTS), the ansible_fqdn variable often only contains the hostname, not the hostname+domainname.

I’ve tried to fix this by having ansible insert a line like this in /etc/hosts, but it didn’t help.
127.0.1.1 {{ inventory_hostname }} {{ ansible_hostname }}
(Which expands to 127.0.1.1 hostname.domain.name hostname)

Does anyone have other suggestions?

so the fqdn gets set first by trying to do reverse dns, which would
not use that /etc/hosts entry, if that fails it falls back to using
the gethostname function which does not guarantee a fully qualified
domain name.

run 'hostname' on the command line, you should see the same output.

Well I guess the question is: how do I fix it?
It sounds like I need to fix the reverse DNS lookups.

I do note that running “hostname -f” from the command line DOES return the FQDN, by the way, even though “hostname” on its own does not.

-Toby

Picking through the code it seems to be whatever

socket.getfqdn()

returns in Python.

side suggestion: why are you needing the hostname? could you just use the inventory name instead in whatever template etc. you’re having issues with?

I was able to use the inventory_hostname as a workaround in templates, but I’m not the only one here, and other people in the company are likely to continue to use the ansible_fqdn as it’s the widely-documented way to get the FQDN - and does work in some places.

I eventually tracked the problem with the reverse DNS lookups down, and it seems to have fixed the problem.

ansible_fqdn = host
facter_fqdn = host.domain

I use this last two years, it’s comfortable for me.

First it seems like it is dns issue- rDNS.
For troubleshooting get the ip address of host manually,
and try to run host ip, it should return something (fqdn, if it doesn’t go fix DNS entry)