AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'

3,928 views
Skip to first unread message

Rajesh

unread,
Mar 30, 2020, 10:28:13 PM3/30/20
to Ansible Project
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]
skipping: [ansible3.example.com]
fatal: [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

Vladimir Botka

unread,
Mar 31, 2020, 2:22:07 AM3/31/20
to Rajesh, ansible...@googlegroups.com
On Mon, 30 Mar 2020 19:28:13 -0700 (PDT)
Rajesh <rajeshbr...@gmail.com> wrote:

> - hosts: all
> [...]
> 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 %}
> [...]
> fatal: [ansible2.example.com]: FAILED! => {"changed": false, "msg":
> "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has
> no attribute 'ansible_fqdn'"}

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

- hosts: all
tasks:
- debug:
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,

-vlado

Rajesh

unread,
Mar 31, 2020, 5:15:56 AM3/31/20
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages