All,
I’m attempting to use a conditional in a template, without success. Specifically I’m trying to add an iptable rule that allows traffic sourced from eth1 on all hosts:
Below is the relevant portion of the template:
{% if ‘eth1’ in ansible_interfaces %}
{% if hostvars[host][‘ansible_eth1’][‘ipv4’][‘address’] is defined %}
-A INPUT -s {{ hostvars[host][‘ansible_eth1’][‘ipv4’][‘address’] }} -j ACCEPT
{% endif %}
{% endif %}
{% endfor %}
Unfortunately the playbook still errors out:
fatal: [test2] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘dict object’ has no attribute ‘ipv4’”, ‘failed’: True}
This server does indeed have an eth1 interface but said interface does not have an IP address in this particular case.
This “if x is defined” syntax seems to work fine in a “when:” conditional in the playbook, just not in the template itself.
Any thoughts on what I may be doing wrong and how to rectify this error / problem?
Many thanks!