# ansible\_fqdn only contains the hostname

**URL:** <https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [October 13, 2015, 1:37am UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494 "2015-10-13T01:37:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toby\_Corkindale](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toby\_Corkindale](https://forum.ansible.com/u/Toby_Corkindale)\
**Post date:** [October 13, 2015, 1:37am UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/1 "2015-10-13T01:37:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 13, 2015, 3:05pm UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/2 "2015-10-13T15:05:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Toby\_Corkindale](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toby\_Corkindale](https://forum.ansible.com/u/Toby_Corkindale)\
**Post date:** [October 13, 2015, 11:51pm UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/3 "2015-10-13T23:51:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [October 14, 2015, 8:50am UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/4 "2015-10-14T08:50:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Toby\_Corkindale](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toby\_Corkindale](https://forum.ansible.com/u/Toby_Corkindale)\
**Post date:** [October 14, 2015, 11:24pm UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/5 "2015-10-14T23:24:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![user64](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user64](https://forum.ansible.com/u/user64)\
**Post date:** [October 15, 2015, 5:04am UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/6 "2015-10-15T05:04:28Z")

</div>

ansible\_fqdn = host  
facter\_fqdn = host.domain

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

---

<div class="post-metadata">

**Author:** ![Murad\_Mammed](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Murad\_Mammed](https://forum.ansible.com/u/Murad_Mammed)\
**Post date:** [January 19, 2022, 3:35pm UTC](https://forum.ansible.com/t/ansible-fqdn-only-contains-the-hostname/19494/7 "2022-01-19T15:35:42Z")

</div>

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)
