Include vars in role-based tasks

In the role, I am trying to load some variables from another role. (If this role was included in the current game, the variables would be available, but that's not how they are.)

So, I tried this:

- include_vars: ../../another_role/defaults/main.yml 

But this does not work, there is no error, but the variables are still undefined. Therefore, I tried to be smart and symbolically refer to the vars/another_role_defaults.yml file in the role where I want to use vars, and then include it as follows:

 - include_vars: another_role_defaults.yml 

The same result, no errors (why doesn't it throw an error if the file cannot be found?), But the variables are still undefined. I also tried this, for good measure, but still not cigars.

 - include_vars: ../vars/another_role_defaults.yml 

What am I doing wrong?

+6
source share
1 answer

It was my own mistake at the end ... I tested this using the debug module and tags as follows:

 - include_vars: ../../another_role/defaults/main.yml - debug: msg={{ variable }} tags: foo 

and then play this book as follows:

  ansible-playbook -vvvv playbook.yml --tags foo 

As soon as I miss the tags, it works (of course). The problem was that I have to add tags to the include_vars command just like this:

 - include_vars: ../../another_role/defaults/main.yml tags: foo - debug: msg={{ variable }} tags: foo 
+8
source

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


All Articles