I’m trying to get in a configuration file the active interface from facts … but i’m still wrong somewhere in the jinja code or in facts accessing …
My tryout looks like this:
# Network interface to monitor (default eth0)
{% set active_eth_list = [] %}
{% for iface in ansible_interfaces if iface != 'lo' %}
{% set fact_eth_name = "ansible_" ~ iface %}
{% if ansible_facts[fact_eth_name]['active'] %}
{{ active_eth_list.append(iface) }}
{% endif %}
{% endfor %}
EOS_FST_NETWORK_INTERFACE="{{ active_eth_list[0] }}"
And the error message is
The full traceback is:
Traceback (most recent call last):
File "/home/adrian/.local/lib/python3.9/site-packages/ansible/template/__init__.py", line 1015, in do_template
res = myenv.concat(rf)
File "/home/adrian/.local/lib/python3.9/site-packages/ansible/template/native_helpers.py", line 83, in ansible_concat
return ''.join([to_text(v) for v in nodes])
File "/home/adrian/.local/lib/python3.9/site-packages/ansible/template/native_helpers.py", line 83, in <listcomp>
return ''.join([to_text(v) for v in nodes])
File "<template>", line 33, in root
File "/home/adrian/.local/lib/python3.9/site-packages/jinja2/runtime.py", line 857, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'ansible_eno1'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/adrian/.local/lib/python3.9/site-packages/ansible/plugins/action/template.py", line 152, in run
resultant = templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False, overrides=overrides)
File "/home/adrian/.local/lib/python3.9/site-packages/ansible/template/__init__.py", line 1049, in do_template
raise AnsibleUndefinedVariable(e, orig_exc=e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_eno1'. 'dict object' has no attribute 'ansible_eno1'
fatal: [fst16.spacescience.ro]: FAILED! => {
"changed": false
}
MSG:
AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_eno1'. 'dict object' has no attribute 'ansible_eno1'
So, the name of the fact key is correctly constructed and i can confirm looking in the redis store:
redis-cli get fst16.spacescience.ro | jq | grep ansible_eno1
"ansible_eno1": {
but somehow the ansible_facts does not have it …
I do have gather_facts: False
as the facts are already cached in redis and my expectation is that the cache facts are available to playbook as if they were freshly gathered…
So, does anyone have an idea where i’m wrong?
Thanks a lot