I'm not sure how to redefine variables between roles in Ansible.
To simplify the setup a bit, I have two roles applied to the same host. The first role defines a variable in its default/main.yml :
do_some_task: yes
And it searches for this variable in its tasks:
- name: Some Task when: do_some_task
The second role overrides that in its vars/main.yml , which should take precedence over default values:
do_some_task: no
However, the task is still in progress, indicating that the variable has not been redefined. The redefinition seems to be related to the tasks of the second role. I tested this by adding a debug task in both roles:
- name: Test some task debug: "msg='do_some_task = {{ do_some_task }}'"
This confirms that the first role sees a different value for the variable than the second.
TASK: [role1 | Test some task] ok: [myhost] => { "msg": "do_some_task = True" } ... TASK: [role2 | Test some task] ok: [myhost] => { "msg": "do_some_task = False" }
The general answer to this question is to set variables in inventory or in hosts. However, this is not particularly DRY: if you have many hosts in different inventories, you will need to set the same variables in many places.
So, is there a way to override a variable from another role?
source share