# How to load variables for a tagged role?

**URL:** <https://forum.ansible.com/t/how-to-load-variables-for-a-tagged-role/27213>\
**Category:** Ansible Project\
**Created:** [April 7, 2018, 6:07am UTC](https://forum.ansible.com/t/how-to-load-variables-for-a-tagged-role/27213 "2018-04-07T06:07:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 7, 2018, 6:07am UTC](https://forum.ansible.com/t/how-to-load-variables-for-a-tagged-role/27213/1 "2018-04-07T06:07:20Z")

</div>

I have a large role, which I will break down into separate roles eventually.

For the moment, I am trying to use tags to selectively run bits of it, tagged e.g. “server”, “portal” etc.

The role is called from this playbook:

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 7, 2018, 12:27pm UTC](https://forum.ansible.com/t/how-to-load-variables-for-a-tagged-role/27213/2 "2018-04-07T12:27:58Z")

</div>

Possibly bad form to reply to one’s own query, but I think I worked it out.

Rather than tag every task inside the role, I split the big role up into four. I removed ALL the tagging from inside the roles, and set up this little playbook:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 8, 2018, 8:09pm UTC](https://forum.ansible.com/t/how-to-load-variables-for-a-tagged-role/27213/3 "2018-04-08T20:09:22Z")

</div>

> Possibly bad form to reply to one's own query, but I think I worked it out.

It's not, it's encouraged when you have an answer.  
People don't waste time answering and other might have the same question also get an answer, so it's a win win.

> Rather than tag every task inside the role, I split the big role up into  
> four. I removed ALL the tagging from inside the roles, and set up this  
> little playbook:
> 
> ---
> 
> - hosts: localhost  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;gather\_facts: false
> 
> &nbsp;&nbsp;pre\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_vars: blah\_envs/{{env}}.yaml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: always  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_vars: blah\_envs/common.yaml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: always
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: blah\_server, tags: [server] }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: blah\_portal, tags: [portal] }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: blah\_alb, tags: [alb] }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { role: blah\_asg, tags: [asg] }
> 
> ... and now it all works as desired. The real breakthrough was figuring out  
> that include\_vars is a task 🙂

You can use vars\_files insted for include\_vars, then you don't need to tag them since they are always read.

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- blah\_envs/{{env}}.yaml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- blah\_envs/common.yaml
