Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Ansible facts not available in Roles section?

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
like image 212
mon Avatar asked Sep 13 '25 22:09

mon


1 Answers

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.

like image 63
techraf Avatar answered Sep 15 '25 12:09

techraf