# Code execution depending on Tag Values

**URL:** <https://forum.ansible.com/t/code-execution-depending-on-tag-values/35204>\
**Category:** Ansible Project\
**Created:** [May 20, 2021, 3:42pm UTC](https://forum.ansible.com/t/code-execution-depending-on-tag-values/35204 "2021-05-20T15:42:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ameya\_Agashe](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@Ameya\_Agashe](https://forum.ansible.com/u/Ameya_Agashe)\
**Post date:** [May 20, 2021, 3:42pm UTC](https://forum.ansible.com/t/code-execution-depending-on-tag-values/35204/1 "2021-05-20T15:42:52Z")

</div>

Hello Anyone here can guide me on Ansible ?  
I have created a role for AEM(Adobe Experience Manager) Author and Publisher. They both have exact same instructions however as per role their variables do change. One server per role. One server cannot have two roles.

Variable values are to be dictated by tag. We have two or more VM’s but Ansible code should grab the value of the variable depending on tag. Has anyone done this before? If yes a code snippet or article would be appreciated.

Example code snippet is below:

- name: Create mnt directory structure  
file:  
path: /mnt/{{ item }}  
state: directory  
owner: cq5  
group: cq5  
mode: 775  
with\_items:

- crx

- crx/author

- crx/publish  
tags:

- aem\_author

- aem\_publish

- name: Copy License file on Author  
copy:  
src: license.properties  
dest: “{{ item }}”  
owner: cq5  
group: cq5  
mode: 755  
force: yes  
loop:

- “{{ author\_folder }}”

- “{{ publishe\_folder }}”  
tags:

- aem\_author

- aem\_publish

So as you see in the above snippet, looping variables should be fed as per server. If no tag is given then it should feed in the variable and in fact skip it.

Is this possible at all?

---

<div class="post-metadata">

**Author:** ![rolandm](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rolandm/32/755_2.png) [@rolandm](https://forum.ansible.com/u/rolandm)\
**Post date:** [May 21, 2021, 5:05am UTC](https://forum.ansible.com/t/code-execution-depending-on-tag-values/35204/2 "2021-05-21T05:05:51Z")

</div>

Hello,

not sure whether I understand your goal right. I assume you want to have given group of tasks for each kind of server/host inside the same playbook.

One possibility would be to organize the playbook tasks inside different blocks and have a hosts definition for each of these blocks. A, B can be single hosts or groups from inventory.

---

<div class="post-metadata">

**Author:** ![Andre\_Liebigt](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@Andre\_Liebigt](https://forum.ansible.com/u/Andre_Liebigt)\
**Post date:** [May 21, 2021, 5:45am UTC](https://forum.ansible.com/t/code-execution-depending-on-tag-values/35204/3 "2021-05-21T05:45:57Z")

</div>

Hi,

the way I understand it you want your Playbook to only copy the license.properties file to the location in `author_folder` if the tag `aem_author` is specified und to the location `publishe_folder` when the playbook is invoked with the `aem_publish` tag?

If this is the case then I think the most common way would be to always copy to the location specified in a variable `dest_folder` which is set per host or group in your inventory.  
If for some reason you have to be able to set it via tags, than I would probably would use `set_fact` in combination with tags to specify the variable content accordingly:

- name: Set destination for Author  
set\_fact:  
dest\_folder: “{{ author\_folder }}”  
tags:

- aem\_author

- name: Set destination for Publisher  
set\_fact:  
dest\_folder: “{{ publishe\_folder }}”  
tags:

- aem\_publish

- name: Copy License file on Author  
copy:  
src: license.properties  
dest: “{{ dest\_folder }}”  
owner: cq5  
group: cq5  
mode: 755  
force: yes  
tags:

- aem\_author

- aem\_publish

This would probably work but I feel that this really the wrong way to do it unless you have a really specific need.

Cheers,  
André
