# Ansible facts help (ansible\_fqdn)

**URL:** <https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834>\
**Category:** Ansible Project\
**Created:** [November 20, 2014, 4:18pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834 "2014-11-20T16:18:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 4:18pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/1 "2014-11-20T16:18:58Z")

</div>

Hi,

I"m trying to use Ansible magic var inventory\_hostname along with ansible\_fqdn fact. I have a playbook that will update the hostname of the system based on the entry in the inventory file and later it will use ansible\_fqdn value to configure a file.

Unfortunately the ansible\_fqdn keeps the initial value when I start running the plabook even if later on I manually gather facts again. The correct value is displayed if I run the same playbook again after the first run. It seems to me that for some reason the manual facts gathering (setup:) is not working :

“”"

- hosts: all  
tasks:

- name: Gathering Ansible facts  
setup:

- name: update the hostname  
hostname: name=“{{ inventory\_hostname }}”

- name: boot persist the hostname  
lineinfile: dest=/etc/sysconfig/network  
regexp=^HOSTNAME=  
line=HOSTNAME={{ inventory\_hostname }}  
state=present

- name: add the hostname to the hosts file  
lineinfile: dest=/etc/hosts  
regexp=“^127.0.0.1(.\*)”  
line=“127.0.0.1{{‘\t’}}{{ inventory\_hostname }} localhost.localdomain localhost”  
state=present  
backrefs=yes

- name: Print ansible\_fqdn  
debug: msg=“ansible\_fqdn is {{ ansible\_fqdn }}”

“”"

Any ideas ??

Regards,  
N.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 4:22pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/2 "2014-11-20T16:22:02Z")

</div>

Sorry , the correct playbook is the one bellow, the setup module executed before the debug action at the end:

“”"

- hosts: all  
tasks:

- name: update the hostname  
hostname: name=“{{ inventory\_hostname }}”

- name: boot persist the hostname  
lineinfile: dest=/etc/sysconfig/network  
regexp=^HOSTNAME=  
line=HOSTNAME={{ inventory\_hostname }}  
state=present

- name: add the hostname to the hosts file  
lineinfile: dest=/etc/hosts  
regexp=“^127.0.0.1(.\*)”  
line=“127.0.0.1{{‘\t’}}{{ inventory\_hostname }} localhost.localdomain localhost”  
state=present  
backrefs=yes

- name: Gathering Ansible facts  
setup:

- name: Print ansible\_fqdn  
debug: msg=“ansible\_fqdn is {{ ansible\_fqdn }}”

“”"

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [November 20, 2014, 4:22pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/3 "2014-11-20T16:22:12Z")

</div>

Hi Nicholas, in your example above, the manual use of the setup module would have to come after the hostname entry is changed in the file. Alternatively, you could simply use a call to set\_fact to override that value, so that you don’t have to regather all of the facts.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [November 20, 2014, 4:26pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/4 "2014-11-20T16:26:39Z")

</div>

Which connection type are you using? If you’re using the default (“smart”), which will use SSH on most recent versions of Linux (Fedora, Ubuntu) and Mac OSX, the ControlPersist option will leave the SSH connection open to the target host, so the current hostname of the session will continue to be used. As I said previously, I would recommend using set\_fact so you don’t have to re-gather facts in this situation.

Thanks!

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 4:53pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/5 "2014-11-20T16:53:39Z")

</div>

Thanks a lot James, I guess I’m using the default connection type.

Didn’t know I could use set\_fact to override discovered values, that will be quite handy .

Regards,  
N.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 5:47pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/6 "2014-11-20T17:47:32Z")

</div>

I’m trying to set ansible\_fqdn using set\_fact but the task doesn’t seem to be executed :

- name: set ansible\_fqdn fact  
set\_fact: ansible\_fqdn={{ inventory\_hostname }}

# output :

TASK: [common | set ansible\_fqdn fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[server1.example.net](http://server1.example.net)] =\> {“ansible\_facts”: {“ansible\_fqdn”: “[server1.example.net](http://server1.example.net)”}}

Any clue ?

---

<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:** [November 20, 2014, 5:55pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/7 "2014-11-20T17:55:43Z")

</div>

task seems to be executed, what result were you expecting?

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 6:05pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/8 "2014-11-20T18:05:13Z")

</div>

I’m expecting ansible\_fqdn to have the same value as inventory\_hostname , see my test playbook and output bellow :

“”"

- name: show inventory\_hostname  
debug: msg=“inventory\_hostname is {{ inventory\_hostname }}”

- name: show ansible\_fqdn  
debug: msg=“ansible\_fqdn is {{ ansible\_fqdn }}”

- name: set ansible\_fqdn fact  
set\_fact: ansible\_fqdn={{ inventory\_hostname }}

- name: show ansible\_fqdn  
debug: msg=“ansible\_fqdn is {{ ansible\_fqdn }}”

---

<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:** [November 20, 2014, 6:15pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/9 "2014-11-20T18:15:48Z")

</div>

this 2nd output shows ansible\_facts not returning anything, unlike  
your previous output.

but ran a test with my own system copying and pasting your last  
playbook and I get this:

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [show inventory\_hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "inventory\_hostname is localhost"  
}

TASK: [show ansible\_fqdn] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "ansible\_fqdn is workstation.briancoca.local"  
}

TASK: [set ansible\_fqdn fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [show ansible\_fqdn] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "ansible\_fqdn is localhost"  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=5 changed=0 unreachable=0 failed=0

what version of ansible are you using?

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 9:15pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/10 "2014-11-20T21:15:49Z")

</div>

I think I found the most weird bug that was driving me crazy, it seems for some strange reason when you have in your playbook vars\_files with the name based on a variable the ansible\_fqdn keeps the old value…

Here are all the steps to reproduce it in ansible 1.7.1 :

create the bellow 3 files exactly as they are (var\_test.yml , inv\_test, test.yml) :

1. **var\_test.yml** :

TEST: “HELLO”

1. **inv\_test** :

[all:vars]  
VAR\_FILE=var\_test

[test]  
test

1. **test.yml** :

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [November 20, 2014, 10:13pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/11 "2014-11-20T22:13:33Z")

</div>

Ahh yes, this was a bug in 1.7.x which has been fixed in devel and will be included in 1.8.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [November 20, 2014, 10:22pm UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/12 "2014-11-20T22:22:03Z")

</div>

What’s the ETA for 1.8 ?

---

<div class="post-metadata">

**Author:** ![Drew\_Michel](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@Drew\_Michel](https://forum.ansible.com/u/Drew_Michel)\
**Post date:** [November 25, 2014, 4:47am UTC](https://forum.ansible.com/t/ansible-facts-help-ansible-fqdn/16834/13 "2014-11-25T04:47:36Z")

</div>

I’m also curious when 1.8 is planning to be released
