# ansible\_fqdn should use platform.node() instead of socket.getfqdn()

**URL:** <https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894>\
**Category:** Ansible Project\
**Created:** [November 12, 2013, 8:51pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894 "2013-11-12T20:51:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [November 12, 2013, 8:51pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/1 "2013-11-12T20:51:02Z")

</div>

Hi Ansible devs,

I’ve just discovered, to my surprise and annoyance, that the ansible\_fqdn fact is obtained by calling socket.getfqdn(). This means the socket module does a reverse DNS lookup on the host’s IP address (not sure which one). If anyone changes the reverse DNS, or if it is not set properly, it results in unexpected behaviour when using this fact. My expectation was that this fact would be looked up from the system’s own hostname, instead of DNS.

Would you consider fixing this by calling platform.node() instead? This looks up the hostname properly, and is not dependent on reverse DNS. Its result is therefore predictable and reliable.

Regards,

Anand

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 12, 2013, 8:54pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/2 "2013-11-12T20:54:27Z")

</div>

​Hi Anand,​

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [November 12, 2013, 9:00pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/3 "2013-11-12T21:00:36Z")

</div>

Goeden avond Serge,

> ​There is a separate ansible-devel mailing list for discussing code​.

Thanks. I didn’t know about the -devel mailing list. However, I’m mostly just a user, and hardly do development. I guess my post was more of a request to the developers, as well as an alert to other users about the reliability of the ansible\_fqdn fact. I only cited code to help pinpoint the issue.

Regards,

Anand

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [November 12, 2013, 9:19pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/4 "2013-11-12T21:19:31Z")

</div>

The python documentation says this about platform.node()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the computer�s network name (may not be fully qualified!). An  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;empty string is returned if the value cannot be determined.

So there's no guarantee that the system's hostname is a FQDN, not any more than using socket.getfqdn().

I guess it's up to you to set the hostname correctly in /etc/hosts if you don't want to depend on DNS. That's a best practice anyway.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [November 13, 2013, 3:52pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/5 "2013-11-13T15:52:27Z")

</div>

Excerpts from Dag Wieers's message of 2013-11-12 16:19:31 -0500:

> The python documentation says this about platform.node()
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the computer’s network name (may not be fully qualified!). An  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;empty string is returned if the value cannot be determined.
> 
> So there's no guarantee that the system's hostname is a FQDN, not any more  
> than using socket.getfqdn().

I would further add that concept of a FQDN is pretty meaningless without  
DNS (or, as you note, /etc/hosts). The only thing the kernel itself  
even \*knows\* about as a hostname is the dumb string set by  
`sethostname()`. Conventionally, one sets that to the "short" hostname.

Even the `hostname` command resorts to the resolver subsystem when asked  
for the FQDN.

You'll note that the 'ansible\_hostname' fact \*is\* resolved via  
`platform.node()`, correctly: platform.node() eventually gets its  
information from the `uname()` system call, which on Linux (and \_not\_  
necessarily other Unices) is the same at the string set by  
`sethostname()`.

I think it could be argued that 'ansible\_hostname' should be set to the  
entirety of the output of `platform.node()`, however. Currently that  
output is split on a period and only the first segment is returned.  
Technically, there is no reason the system's nodename couldn't be  
'foo.bar', for example. On the other hand, most users will expect  
'ansible\_hostname' to be the "short" hostname. It could be confusing to  
users who \_do\_ set the nodename/hostname to a string with a period in  
it, however, to only get the first bit of it back.

Perhaps there should be an 'ansible\_nodename' that returns the contents  
of `platform.node()` unedited? I could see in some cases having that  
information be useful.

Also related, is that in the following circumstance there is no way to  
retrieve the "short" version of the FQDN:

&nbsp;&nbsp;&nbsp;&nbsp;nodename = 'foo.bar'  
&nbsp;&nbsp;&nbsp;&nbsp;FQDN\_via\_DNS = 'hey.example.org'

In this case, 'ansible\_hostname' will give 'foo', while 'ansible\_fqdn'  
will give 'hey.example.org'. To get the "short" version of the FQDN,  
you'll need to do "ansible\_fqdn.split('.')[0]", which is really not all  
that bad considering you're already doing something insane.

Not sure if this is something that should be noted in the docs, given  
that we all, occasionally, do things that are insane. Perhaps a section  
explaining the facts returned by setup and where they come from, for  
"advanced" users, would be useful.

---

<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:** [November 13, 2013, 4:56pm UTC](https://forum.ansible.com/t/ansible-fqdn-should-use-platform-node-instead-of-socket-getfqdn/13894/6 "2013-11-13T16:56:59Z")

</div>

There is totally a way -- split it in Jinja

-- Michael
