There are my .j2 template and playbook to generate hostname, fqdn and ipv4 for all manage node
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