# Use a specific variable within a playbook base on the tag defined

**URL:** <https://forum.ansible.com/t/use-a-specific-variable-within-a-playbook-base-on-the-tag-defined/34545>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [January 30, 2021, 3:06am UTC](https://forum.ansible.com/t/use-a-specific-variable-within-a-playbook-base-on-the-tag-defined/34545 "2021-01-30T03:06:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 30, 2021, 3:06am UTC](https://forum.ansible.com/t/use-a-specific-variable-within-a-playbook-base-on-the-tag-defined/34545/1 "2021-01-30T03:06:53Z")

</div>

> Hi All,
> 
> Hope anyone could help me!
> 
> Basically i have a playbook that configures sudoers commands for a particular user. In this playbook is a command that has to use a {{ variable }}. This variable differs dependent on the environment i.e. dev/prod.
> 
> Content of files:  
> #In my root directory main.yml is this:
> 
> - hosts: all
> 
> become: True  
> roles:
> 
> - configure\_sudoers  
> tags:
> - environment: dev
> 
> # in my role playbook is this:
> 
> - block:
> 
> tags:
> 
> - dev
> 
> - prod
> 
> - name: Create sudoers commands for user  
> blockinfile:

This is only one line so why use blockinfile?

> path: “/etc/sudoers.d/user”  
> create: yes  
> block: |  
> user ALL=(ALL) /bin/systemctl status omsagent-{{ workspace\_id }}.service

You should definitely use validate to avoid expensive mistakes

> # In my vars files is this:
> 
> workspace\_id: this\_id\_for\_dev  
> workspace\_id: this\_id\_for\_prod

This can’t be in the same file.

> Example Scenario:  
> So if i run the main.yml playbook with tag=dev, I want the role to configure sudoers command and only pass in the workspace\_id for dev
> 
> The same would be if i ran the main.yml playbook with tag=prod

It’s not clear if this different option are on the same target machine? Or are there multiple targets that each have a different sudoers file?
