# AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible\_fqdn'

**URL:** <https://forum.ansible.com/t/ansibleundefinedvariable-ansible-vars-hostvars-hostvarsvars-object-has-no-attribute-ansible-fqdn/32567>\
**Category:** Ansible Project\
**Created:** [March 31, 2020, 2:28am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-ansible-vars-hostvars-hostvarsvars-object-has-no-attribute-ansible-fqdn/32567 "2020-03-31T02:28:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajesh](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@Rajesh](https://forum.ansible.com/u/Rajesh)\
**Post date:** [March 31, 2020, 2:28am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-ansible-vars-hostvars-hostvarsvars-object-has-no-attribute-ansible-fqdn/32567/1 "2020-03-31T02:28:13Z")

</div>

Hi all,

Below is my playbook.

- name: Create server list  
hosts: all  
become: true  
become\_user: root  
gather\_facts: yes  
tasks:
- name: Copy the template  
template:  
src: templates/server\_list.j2  
dest: /etc/server\_list.txt  
owner: automation  
mode: ‘0600’  
setype: net\_conf\_t  
when: ansible\_fqdn in groups[‘proxy’]

[automation@workstation plays]$ cat templates/server\_list.j2  
{% for host in groups[‘all’] %}  
{{ hostvars[host][‘ansible\_fqdn’] }}  
{% endfor %}

When I run the playbook, I get the below error message.

TASK [Copy the template] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [[ansible4.example.com](http://ansible4.example.com)]  
skipping: [[ansible3.example.com](http://ansible3.example.com)]  
fatal: [[ansible2.example.com](http://ansible2.example.com)]: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible\_fqdn’”}

Could someone help if there is anything wrong with my playbook.

Thanks,  
Rajesh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 31, 2020, 6:21am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-ansible-vars-hostvars-hostvarsvars-object-has-no-attribute-ansible-fqdn/32567/2 "2020-03-31T06:21:45Z")

</div>

Your playbook works for me. The problem is missing variable "ansible\_fqdn" at  
some host. You might want to find the host and see what's the problem. Try

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_fqdn

Why do you use "ansible\_fqdn" in the "when" condition instead of  
"inventory\_hostname"? This implicates that you're mixing "ansible\_fqdn" and  
"inventory\_hostname" in the inventory.

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rajesh](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@Rajesh](https://forum.ansible.com/u/Rajesh)\
**Post date:** [March 31, 2020, 9:15am UTC](https://forum.ansible.com/t/ansibleundefinedvariable-ansible-vars-hostvars-hostvarsvars-object-has-no-attribute-ansible-fqdn/32567/3 "2020-03-31T09:15:56Z")

</div>

Thanks for your help. There was an issue in one of the hosts in the inventory. Once I removed that host from the inventory it worked fine.

I created a custom facts in one of the hosts as below. Not sure what the issue is but it is not working.

- name: Create a custom facts file  
hosts: database  
become: true  
become\_user: root  
gather\_facts: true  
tasks:

- name: Create a custom facts directory  
file:  
path: /etc/ansible/facts.d  
state: directory

- name: Create a custom facts  
template:  
src: templates/custom.fact.j2  
dest: /etc/ansible/facts.d/custom.fact

- name: Print ansible\_local  
debug: var=ansible\_local

- name: Print the custom facts value  
debug:  
msg: “{{ ansible\_local.custom.sample\_exam }}”

[automation@workstation plays]$ cat templates/custom.fact.j2  
[sample\_exam]  
server\_role=mysql  
[automation@workstation plays]$

[automation@workstation plays]$ ansible-playbook facts.yml

PLAY [Create a custom facts file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ansible5]: FAILED! =\> {“ansible\_facts”: {}, “changed”: false, “failed\_modules”: {“setup”: {“ansible\_facts”: {“discovered\_interpreter\_python”: “/usr/libexec/platform-python”}, “cmd”: “/etc/ansible/facts.d/custom.fact”, “failed”: true, “invocation”: {“module\_args”: {“fact\_path”: “/etc/ansible/facts.d”, “filter”: “\*”, “gather\_subset”: [“all”], “gather\_timeout”: 10}}, “msg”: “[Errno 8] Exec format error: b’/etc/ansible/facts.d/custom.fact’”, “rc”: 8}}, “msg”: “The following modules failed to execute: setup\n”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ansible5 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

[automation@workstation plays]$

Thanks,  
Rajesh
