'AnsibleUnderfinedVariable: 'dict object' has no attribute 'ansible_fqdn'

I want to get the fqdns of remote hosts, and I planned to get it by:

{% for host in groups['all'] %}
Hello {{ hostvars[host]['ansible_facts']['ansible_fqdn'] }}
{% endfor %}

but then I got an error saying "AnsibleUnderfinedVariable: 'dict object' has no attribute 'ansible_fqdn'", but I can get the information of a fqdn by this command:

$ ansible all -m setup -a "filter=*fqdn*"
172.25.250.9 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn: "workstation.lab.example.com",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

So my confusion is Why I can get the variable by setup in a command line, but can't do by a playbook? Can you rescue me? Thanks a lot!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source