Optional variable overrides default value in another role

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?

+5
source share
2 answers

If you want to associate a role with another role, you can override the internal variable of the role by specifically passing variables from the external role.

According to your example, you will have an internal role:

 - name: Some Task when: do_some_task 

This internal role would have a variable like this:

 do_some_task: true 

And then you can redefine the inner role when you call your outer role in a similar play:

 - name: outer-role hosts: outer-role-nodes roles: - role: outer-role - { role: inner role, do_some_task: "{{ do_some_task }}" } 

This should take the do_some_task variable from the external role and override the internal do_some_task role.

0
source

If you use Ansible> = 2.2, you might be able to use include_roles :

  - name: Pass variables to role include_role: name: role1 vars: do_some_task: "{{ role2_do_some_task }}" 

You can also use role dependencies for Anslbie> = 1.3. Write role2/meta/main.yml as:

  --- dependencies: - { role: role1, do_some_task: yes } 

If you do not want one or the other, given that you do not want to specify the variable definition in the playbook, the only reasonable place to define them is in the inventory.

You can define those that should overwrite the default role1 value in the same group:

  [overwrite_do_some_task] host1 host3 [overwrite_do_some_task:vars] do_some_task=yes 

If you did not specify a variable value before running role1, then the expected behavior is to use the default value that OP observes.

Note. This is more readable for prefixing role variables. those. use role1_do_some_task instead of do_some_task . By doing so, you are less likely to confuse yourself. See Key Recommendations: Key Principles .

0
source

Source: https://habr.com/ru/post/1238387/


All Articles