Ansible Bugs affect template-generated configurations for Nagios
I wanted to point out some Ansible bugs affect the way the jinja2 .conf.j2 templates are generated when using {{ hostvars[host].ansible_default_ipv4.address }} not being able to be ascertained without first gathering facts due to:
https://github.com/ansible/ansible/issues/9260
Code in reference: https://github.com/ansible/ansible-examples/blob/master/lamp_haproxy/roles/nagios/templates/dbservers.cfg.j2#L13
The workaround stated for adding this to playbooks in https://github.com/ansible/ansible/issues/9260 does not work:
- hosts: all
tasks: []
Due to https://github.com/ansible/ansible/issues/15525
I have similar issue not sure. I am not using ipv4 variable. I want to access ansible_host variable. #ansible --version ansible 2.0.2.0
#python --version Python 2.7.11+
It works when I do debug:
Code:
- debug: var=hostvars[item]['ansible_host'] with_items: groups['all'] Jinja2 template: {% for item in groups['all'] %} {{ hostvars[item]['ansible_host'] }} {% endfor %}
Output:
ok: [server-01] => (item=demo-xdadmin-01) => { "hostvars[item]['ansible_host']": "10.120.8.136", "item": "demo-xdadmin-01" }
TASK [hostname : Copy the hostfile template] *********************************** fatal: [server-01]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_host'"}
As an update, I was able to get the workaround listed for bug ansible/ansible#9260 to work but I was missing the remote_user directive. Here's what I am using now that gets me past this and gathers facts needed to generate ansible_default_ipv4.address
As mentioned in the workaround for this but, I place this at the top of my playbooks for hosts that need this value generated so it can be referenced.
- hosts: all
remote_user: root
tasks: []