# Ansible roles - setup concept

**URL:** <https://forum.ansible.com/t/ansible-roles-setup-concept/37381>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [November 23, 2022, 1:31pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381 "2022-11-23T13:31:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 23, 2022, 1:31pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/1 "2022-11-23T13:31:38Z")

</div>

It sounds like you want one NFS role that can do both NFS server and NFS client tasks. Were I designing such a role I think I would use a variable or tag to identify which “persona” you want the role to configure, and use that in the tasks/main.yml to source two different task files. One task file would do the work for an NFS server persona. One task file would do the work for an NFS client persona.

roles/nfs/tasks/main.yml

---

<div class="post-metadata">

**Author:** ![RG1](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@RG1](https://forum.ansible.com/u/RG1)\
**Post date:** [November 23, 2022, 2:52pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/2 "2022-11-23T14:52:43Z")

</div>

hi,

I adapted the configuration to your proposal, however it doesn’t work  
First all tasks were execetuted on host cmp an then all tasks were again exectuted on the the other machine except cmp.

Regarding to your solution would be necessary to tag both tasks

roles/nfs/tasks/nfs\_server.yml

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [November 23, 2022, 4:27pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/3 "2022-11-23T16:27:21Z")

</div>

I do something similar, but then with rsyslog.

I have this set in the role:

defaults/main.yml:

rsyslog\_type: client

and then in tasks/main.yml:

- name: Ensure the appropriate tasks file is imported  
include\_tasks: “{{ rsyslogd\_type }}.yml”

This then includes either tasks/client.yml or tasks/server.yml.

The playbook uses the role like this:

- name: Include rsyslogd role  
include\_role:  
name: ansible\_role\_rsyslogd  
args:  
apply:  
tags: rsyslogd  
tags: always

So by default (i.e. for all hosts in the play) the rsyslog client is configured.  
The one or two rsyslog servers have set this host\_var:

rsyslog\_type: server

It’s more or less the same, except that I use a variable to denote the type of deployment, rather than tags, as I find tags more cumbersome to work with.

---

<div class="post-metadata">

**Author:** ![RG1](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@RG1](https://forum.ansible.com/u/RG1)\
**Post date:** [November 24, 2022, 8:23pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/4 "2022-11-24T20:23:22Z")

</div>

Hi,

i once again exactly followed your instruction by copying your published config into the files, because your methon seems comprehensive to me.

However the problem is that the tags are not used which means that both tasks are executed completely twice - first for cmp machine and then for the other machines.  
Do you’ve an idea why this happens.

Thx

---

<div class="post-metadata">

**Author:** ![RG1](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@RG1](https://forum.ansible.com/u/RG1)\
**Post date:** [November 24, 2022, 8:29pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/5 "2022-11-24T20:29:47Z")

</div>

Hi,  
I don’t exactly understand your method although it nearly seems to be similar than the version from Walter. Could you probably post more excerpts from the various scripts. Maybe this could help to better get an idea about the context.  
Thanks

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 24, 2022, 8:34pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/6 "2022-11-24T20:34:32Z")

</div>

Show us your playbooks, roles, and output, and we can tell you why.  
Without any of those, we’d be guessing just like you.

---

<div class="post-metadata">

**Author:** ![RG1](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@RG1](https://forum.ansible.com/u/RG1)\
**Post date:** [November 25, 2022, 10:22am UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/7 "2022-11-25T10:22:44Z")

</div>

Hi,  
these are the ansible role config files

ansible/runsetup.yml

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 11:50am UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/8 "2022-11-25T11:50:39Z")

</div>

How do you execute runsetup.yml ? You need to provide the appropriate tag on the command line to tell it which persona to assume.

$ ansible-playbook runsetup.yml -i inventory -t nfs\_server

$ ansible-playbook runsetup.yml -i inventory -t nfs\_client

Walter

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 25, 2022, 2:07pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/9 "2022-11-25T14:07:20Z")

</div>

Te be honest, I’ve never seen the tags keyword applied to at the play level, although [https://docs.ansible.com/ansible/latest/reference\_appendices/playbooks\_keywords.html](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html) clearly says it can be. I suspect the functionality it provides is not what you’re after though, as you seem to be using it in a way that would normally be done with the tags command line parameter. The above mentioned docs says this about tags on a play:

> Tags applied to the task or included tasks, this allows selecting subsets of tasks from the command line.

The way I read that is, it’s as if you had put these tags on each task, or had used include\_tasks with apply tags […]. You still have to use the command line tags parameter to do any task selection based on tags.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 2:46pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/10 "2022-11-25T14:46:17Z")

</div>

I ran a simple test on tagging plays and it does seem to work as one might assume. I have two simple plays. Each prints a debug message stating what play they are executing. When no tags both plays execute. With a tag only the tagged play executes.

**foo.yml**

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 2:56pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/11 "2022-11-25T14:56:48Z")

</div>

As one more sanity check I ran the sample playbook with a tag not used in the playbook and none of the plays executed. Note the empty Play Recap output

**% ansible-playbook -i localhost, foo.yml -t play3**

PLAY [play 1 tagged play1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

PLAY [play 2 tagged play2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Walter

---

<div class="post-metadata">

**Author:** ![Robert\_Griessner](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Robert\_Griessner](https://forum.ansible.com/u/Robert_Griessner)\
**Post date:** [November 25, 2022, 3:38pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/12 "2022-11-25T15:38:00Z")

</div>

hi, my intention is to migrate various playbooks to absible roles, setting up a system consisting of various machines. i would like to start the new ansible role playbook once doing the whole stuff. one item will be configuring nfs on server and clients. is it a good idea to use one role for this nfs-task and how could i realize it? until now i don’t or whether it works with tags or any other solution or is it in general usual and easier in this case to build two roles one for server and one for client?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 4:19pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/13 "2022-11-25T16:19:51Z")

</div>

It works with roles. I will provide a following message where there is behavior one might NOT expect.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 4:27pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/14 "2022-11-25T16:27:26Z")

</div>

There is a unique behavior with plays and roles and tags to be aware of. If you tag a play that calls a role, all tasks in that role are executed if that tag is provided even if there are tasks inside that role that have an alternate tag.

AFTER running the tests below, I edited my sample role to include two debug statements. Each had play1 or play2 as the tag. When I executed the foo.yml playbook with -t play1 then both debug messages are display in play1, but only the play1 debug message is display for play2. It seems to parse the role for play2 to determine if any tasks have the play1 tag (including the roles) and will run them if they exist. It is easier to show than to describe.

I don’t know if this is the expected behavior with ansible. I was surprised by this. I expected the entire play to be skipped if the tag for that play was not specified.

**% cat roles/my\_role/tasks/main.yml**

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 4:29pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/15 "2022-11-25T16:29:12Z")

</div>

> AFTER running the tests IN THE PRIOR MESSAGE, I edited my sample role to include two debug statements. Each had play1 or play2 as the tag. When I executed the foo.yml playbook with -t play1 then both debug messages are display in play1, but only the play1 debug message is display for play2. It seems to parse the role for play2 to determine if any tasks have the play1 tag (including the roles) and will run them if they exist. It is easier to show than to describe.

Walter

---

<div class="post-metadata">

**Author:** ![Robert\_Griessner](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Robert\_Griessner](https://forum.ansible.com/u/Robert_Griessner)\
**Post date:** [November 25, 2022, 4:48pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/16 "2022-11-25T16:48:28Z")

</div>

hi,  
and where/how does your solution differ to mine? why did you place the tasks in the playbook and not in the role-tasks? how does your my\_role task look like?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 25, 2022, 5:08pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/17 "2022-11-25T17:08:52Z")

</div>

I was using your original playbook as my starting point. It had tags in the plays. If you remove the tags in the plays and leave the tags in the role tasks it behaves as expected. Each play runs the role, and only the tasks with the named tag execute.

Walter

---

<div class="post-metadata">

**Author:** ![RG1](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@RG1](https://forum.ansible.com/u/RG1)\
**Post date:** [November 25, 2022, 6:01pm UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/18 "2022-11-25T18:01:51Z")

</div>

hi,  
would it be possible to post the relevant content of all these necessary files (excerpt in one of my previous posts) for installing nfs on server and client using one ansible role. That allows me to understand and reproduce it.

Thx a lot

- ansible/runsetup.yml

- ansible/nfs/tasks/main.yml

- ansible/nfs/tasks/ nfs\_server.yml

- ansible/nfs/tasks/ nfs\_clientym

- command to run the ansible role playbook

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 26, 2022, 12:13am UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/19 "2022-11-26T00:13:00Z")

</div>

I think it makes sense to put both the client and server tasks in the same role, especially if you have some hosts which are both nfs clients and nfs servers.

However, I would recommend that you not use the tags keyword at the play level. It’s a short cut way to add a tag or tags to every task involved in the play. While there must be some problem for which that is a solution, you don’t have such a problem, at least not in my opinion.

A more straightforward approach would be to use _host groups_. Let’s say you have host groups rg\_nfs\_clients and rg\_nfs\_servers. Then in your nfs/tasks/main.yml you have something like

```auto
---
# ansible/nfs/tasks/main.yml

- name: Configure NFS Servers
  ansible.builtin.include_tasks: nfs_server.yml
  when: inventory_hostname in groups['rg_nfs_servers']
  tags:
    - always

- name: Configure NFS Clients
  ansible.builtin.include_tasks: nfs_client.yml
  when: inventory_hostname in groups['rg_nfs_clients']
  tags:
    - always

```

Then your playbook becomes

```auto
---
# my_playbook.yml
- name: Configure NFS
  hosts: all # <-- or any subset of "all"
  become: true
  roles:
    - nfs

```

Whatever hosts you throw at it, it’s only going to put nfs server configs on hosts in your rg\_nfs\_servers group, and likewise it will only configure your rg\_nfs\_clients members as nfs clients. For hosts that are in neither group, it will do nothing.

The “tags: always” on those ansible.builtin.includes are so that, if you ever do put tags on the included task files tasks, you can still make use of those tags from the command line. It ensures the include tasks happen even when faced with otherwise task-limiting command line tags.

---

<div class="post-metadata">

**Author:** ![Robert\_Griessner](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Robert\_Griessner](https://forum.ansible.com/u/Robert_Griessner)\
**Post date:** [November 26, 2022, 10:16am UTC](https://forum.ansible.com/t/ansible-roles-setup-concept/37381/20 "2022-11-26T10:16:59Z")

</div>

Hi,  
great, it works now. This method is easy to understand and implement.  
Thx to all, a really great ansible community 🙂
