Sunday, 29 August 2021

Ansible Template Error

 Ansible Template Error

You wrote a template along the lines of 

{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}

and a deploy script

---
- name: deploy template
  hosts: ansible01
  tasks:
  - name: send template
    template:
      src: test.j2
      dest: /tmp/hosts.txt

and was given the feedback of....


[ansible@control01 ~]$ ansible-playbook deploy-template.yml

PLAY [deploy template] **********************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [ansible01]

TASK [send template] ************************************************************************************************
fatal: [ansible01]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'"}

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

[ansible@control01 ~]$


Spot the obvious mistake! Or perhaps it's not too obvious: the issue is that the template is set to work on all hosts, but your script is set to run on anything but all hosts.





No comments:

Post a Comment