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 }}”
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.
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.
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) :