4

Question

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

ansible.cfg

[defaults]
error_on_undefined_vars = True
mon
  • 18,789
  • 22
  • 112
  • 205

1 Answers1

2

Why Ansible facts such as ansible_distribution are 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.

Community
  • 1
  • 1
techraf
  • 64,883
  • 27
  • 193
  • 198