Why Ansible facts such as ansible_distribution are not available in Roles section?
---
- name: Test
hosts: all
tasks:
- name: debug
debug:
msg: "{{ ansible_distribution }}" <----- Works
roles:
- "{{ ansible_distribution }}/somerole" <---- ERROR! 'ansible_distribution' is undefined
[defaults]
error_on_undefined_vars = True
Why Ansible facts such as
ansible_distributionare not available in Roles section?
Because roles dictionary in a play is parsed before executing the setup task which gathers facts.
That said, according to common sense, you should be able to use include_role module in your tasks, but using facts for role names is not possible there either (similarly to this problem, but you can use a workaround I suggested there, i.e. include a role with a static name and use when conditional to control which one).
Generally, referring to your example, roles were designed to provide an abstraction level and are supposed to handle architectural differences inside them, not outside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With