find ansible_fqdn and default_ipv4.address with ansible hostvars

148 views
Skip to first unread message

Gustave Pablo

unread,
Jun 25, 2021, 2:14:04 PM6/25/21
to Ansible Project
There are my .j2 template and playbook to generate hostname, fqdn and ipv4 for all manage node
  --> vi hosts.j2
{% for i in groups['all'] %}

{{ hostvars[i].inventory_hostname }}
{{hostvars[i].ansible_fqdn }}
{{hostvars[i].ansible_default_ipv4.address }}
{% endfor %}

--> vi hosts.yml
---
- hosts: all
  become: yes
  gather_facts: yes
  tasks:
  - name: user template to generate myhosts file
    template:
      src: hosts.j2
      dest: /home/hostvarsfile

When i run the playbook here what i get

 ansible-playbook hosts.yml

PLAY [all] **************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [192.168.0.17]
fatal: [node12]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host node12 port 22: No route to host", "unreachable": true}
ok: [node14]
ok: [node16]
ok: [node15]

TASK [user template to generate myhosts file] ***************************************************************************************************************************
fatal: [192.168.0.17]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'"}
fatal: [node14]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'"}
fatal: [node16]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'"}
fatal: [node15]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'"}

PLAY RECAP **************************************************************************************************************************************************************
192.168.0.17               : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
node12                     : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
node14                     : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
node15                     : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
node16                     : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Can somebody help please


Reply all
Reply to author
Forward
0 new messages