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
---
# tasks file for my_role

- name: my_role play1 tag
  debug: msg="debug message for tag play1 inside my_role"
  tags: play1

- name: my_role play2 tag
  debug: msg="debug message for tag play2 inside my_role"
  tags: play2



% ansible-playbook -i localhost, foo.yml -t play1

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

TASK [my_role : my_role play1 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play1 inside my_role"
}

TASK [my_role : my_role play2 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play2 inside my_role"
}

TASK [debug in play 1] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 1"
}

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

TASK [my_role : my_role play1 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play1 inside my_role"
}

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0



% ansible-playbook -i localhost, foo.yml -t play2

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

TASK [my_role : my_role play2 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play2 inside my_role"
}

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

TASK [my_role : my_role play1 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play1 inside my_role"
}

TASK [my_role : my_role play2 tag] 
*************************************************************************************
ok: [localhost] => {
    "msg": "debug message for tag play2 inside my_role"
}

TASK [debug in play 2] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 2"
}

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0



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

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

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

PLAY RECAP 
*************************************************************************************************************



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Nov 25, 2022, at 10:38 AM, Robert Grießner 
<[email protected]<mailto:[email protected]>> wrote:

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?

'Rowe, Walter P. (Fed)' via Ansible Project 
<[email protected]<mailto:[email protected]>> 
schrieb am Fr., 25. Nov. 2022, 15:57:
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Nov 25, 2022, at 9:46 AM, 'Rowe, Walter P. (Fed)' via Ansible Project 
<[email protected]<mailto:[email protected]>> 
wrote:

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

---
- name: play 1 tagged play1
  hosts: localhost
  become: false
  gather_facts: false
  tags: play1
  tasks:
    - name: debug in play 1
      debug: msg="debug in play 1"

- name: play 2 tagged play2
  hosts: localhost
  become: false
  gather_facts: false
  tags: play2
  tasks:
    - name: debug in play 2
      debug: msg="debug in play 2"




% ansible-playbook -i localhost, foo.yml

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

TASK [debug in play 1] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 1"
}

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

TASK [debug in play 2] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 2"
}

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0






% ansible-playbook -i localhost, foo.yml -t play1

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

TASK [debug in play 1] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 1"
}

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

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0





% ansible-playbook -i localhost, foo.yml -t play2

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

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

TASK [debug in play 2] 
*************************************************************************************************
ok: [localhost] => {
    "msg": "debug in play 2"
}

PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Nov 25, 2022, at 9:07 AM, Todd Lewis 
<[email protected]<mailto:[email protected]>> wrote:

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://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.ansible.com%2Fansible%2Flatest%2Freference_appendices%2Fplaybooks_keywords.html&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=Xj6vRCDSRXNQu7Z1dr8A3DeEgq9bocC2uBozBawT9GU%3D&reserved=0>
 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.

On 11/25/22 5:22 AM, RG wrote:
Hi,
these are the ansible role config files

 ansible/runsetup.yml
---
- hosts:
    - cmp
  become: true
  tags:
    - nfs_server
  roles:
    - nfs

- hosts:
    - hardware:!cmp
    - vms
  become: true
  tags:
    - nfs_client
  roles:
    - nfs

ansible/nfs/tasks/main.yml
 ---
# tasks file for installing nfs
- name: configure nfs server
  include_tasks: nfs_server.yml
  tags:
    - nfs_server
- name: configure nfs client
  include_tasks: nfs_client.yml
  tags:
    - nfs_client

/ansible/nfs/tasks/ nfs_server.yml
---
- name: Create shared nfs directory
...
- name: Configure /etc/exports
...
- name: set symlink /media/nfs-share -> /Data/nfs-share
...
- name: set symlink to ansible hosts.ini
...


/ansible/nfs/tasks/ nfs_clientym
---
- name: Create sharing nfs directory on hosts/guests
...
- name: Mounting /media/nfs-share
...


This is the ansible console output -
all tasks included in nfs_server.yml and nfs_client.yml are exectuted for cmp 
(server) and then it starts again with nfs_server.yml for top1...(clients) and 
stops because set symlink cannot be exectuted on clients, which is ok.
in this case tagging in roles doesn't work.


PLAY [cmp] 
***************************************************************************************************************************************************************
TASK [Gathering Facts] 
***************************************************************************************************************************************************
ok: [cmp1]
TASK [configure nfs server] 
**********************************************************************************************************************************************
included: /Data/nfs-share/ansible/nfs/tasks/nfs_server.yml for cmp1
TASK [Create shared nfs directory] 
***************************************************************************************************************************************
ok: [cmp1] =>
TASK [nfs : Configure /etc/exports] 
**************************************************************************************************************************************
ok: [cmp1] =>
TASK [nfs : Commmand exportfs] 
*******************************************************************************************************************************************
skipping: [cmp1] =>
TASK [set symlink /media/nfs-share -> /Data/nfs-share] 
*******************************************************************************************************************
ok: [cmp1] =>
TASK [nfs : set symlink to ansible hosts.ini] 
****************************************************************************************************************************
changed: [cmp1] =>
TASK [configure nfs client] 
**********************************************************************************************************************************************
included: /Data/nfs-share/ansible/nfs/tasks/nfs_client.yml for cmp1
TASK [Create sharing nfs directory on hosts/guests] 
**********************************************************************************************************************
ok: [cmp1] =>
TASK [Mounting /media/nfs-share] 
*****************************************************************************************************************************************
changed: [cmp1] =>

PLAY [hardware:!cmp,vms] 
*************************************************************************************************************************************************
TASK [Gathering Facts] 
***************************************************************************************************************************************************
ok: [top1]
...
TASK [configure nfs server] 
**********************************************************************************************************************************************
included: /Data/nfs-share/ansible/nfs/tasks/nfs_server.yml for top1, top2,...
TASK [Create shared nfs directory] 
***************************************************************************************************************************************
changed: [top1]
...
TASK [nfs : Configure /etc/exports] 
**************************************************************************************************************************************
hanged: [top1] =>
...
TASK [nfs : Commmand exportfs] 
*******************************************************************************************************************************************
skipping: [top1]
...
TASK [set symlink /media/nfs-share -> /Data/nfs-share] 
*******************************************************************************************************************
fatal: [top1]:
....
PLAY RECAP 
***************************************************************************************************************************************************************

Thx,
Robert


[email protected]<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgmail.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=60EKzWflEjN0W7aGaUuwaOnfTbLIin9SKD9gstddG5w%3D&reserved=0>
 schrieb am Donnerstag, 24. November 2022 um 21:34:46 UTC+1:
Show us your playbooks, roles, and output, and we can tell you why.
Without any of those, we'd be guessing just like you.


On 11/24/22 3:23 PM, RG wrote:
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

[email protected]<http://nist.gov/> schrieb am Mittwoch, 23. November 2022 um 
14:31:52 UTC+1:
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
---
- name: configure nfs server
  include_tasks: nfs_server.yml
  tags: nfs_server

- name: configure nfs client
  include_tasks: nfs_client.yml
  tags: nfs_client
...

roles/nfs/tasks/nfs_server.yml
---
- name: tasks to configure nfs servers
  some_tasks:
...


roles/nfs/tasks/nfs_client.yml
---
- name: tasks to configure nfs clients
  some_tasks:
...


This lets you separate the tasks for server and client persona. Executing the 
role with tag nfs_server will configure the inventory machines as nfs servers. 
Executing the role with tag nfs_client will configure the inventory machines as 
nfs clients. Execute the role two times with different inventories and tags as 
you showed in your original message.


my_playbook.yml
---
- hosts: cmp
  become: true
  tags: nfs_server
  roles:
    - nfs

- hosts:
    - hardware:!cmp
    - vms
  become: true
  tags: nfs_client
  roles:
    - nfs


I am curious to have others also propose ideas to see how others might address 
the problem.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Nov 23, 2022, at 8:08 AM, Robert Grießner 
<[email protected]<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgmail.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=60EKzWflEjN0W7aGaUuwaOnfTbLIin9SKD9gstddG5w%3D&reserved=0>>
 wrote:

Hi,
i'd like to migrate my playbooks into a ansible role. I started but a basic 
question raises.
E.g.
I sould like to implement nfs in network of linux machines. on one machine i've 
to configure the nfs-sharing and on the other machines I've to implemt the link 
to the nfs-sourcing host.

systemA - configure a share for other hosts
systemB,C,D - use the nfs-sharing from system A

I tried to create a role called nfs where i have to configure a few tasks on 
systemA and the other tasks on systemB,C,D
I've splitted the tasks into 2 vaious task files one for systemA and one for 
systemB and both will be imported into main.yml in tasks.
---
# tasks file for installing nfs
- import_tasks: install_cmp.yml
- import_tasks: install_machines.yml

in the run.yml I tried to call the same role twice where I used tags to call 
the plays for the cmp machine from install_cmp.yml and the tags in 
install_machines for running the plays for the other machines. however this 
doesn't work

- hosts: cmp
  become: true
  tags:
    - create_nfs_cmp
    - config_exports_cmp
    - exportfs_cmp
    - symlink_cmp
    - symlink_hostsini_cmp
  roles:
    - nfs

- hosts:
    - hardware:!cmp
    - vms
  become: true
  tags:
    - create_nfs_machines
    - mnt_nfs_machines
  roles:
    - nfs


is my concept wrong, do I have to configure to roles one for setting up nfs on 
cmp and one for implementing nfs on the machines or is it possible to create 
just one role for installing nfs on with vaious tasks on various machines?

Thx


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/56bd92d3-e3df-2eaf-383a-fc9a6a6ca02a%40gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2F56bd92d3-e3df-2eaf-383a-fc9a6a6ca02a%2540gmail.com&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=1D945iCmLZUz8Tpo%2B87ESh9FSqL9SLVmM4jMNNKWNSw%3D&reserved=0>.


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5EE865C6-8EE5-4E0E-ADF9-46224D525982%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2F5EE865C6-8EE5-4E0E-ADF9-46224D525982%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=LIKEjghPs%2BY3tpkh1ifRZz9aLCP93HdmuxC7OsMtMIg%3D&reserved=0>.


--
You received this message because you are subscribed to a topic in the Google 
Groups "Ansible Project" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/ansible-project/f1liGR-TVCs/unsubscribe<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Ftopic%2Fansible-project%2Ff1liGR-TVCs%2Funsubscribe&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=syXLO4siDcBUEW3Pq0w9zdANqhVcj3lIstk1k3CgcF0%3D&reserved=0>.
To unsubscribe from this group and all its topics, send an email to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/AA831EF0-F19F-4812-8C4B-7FB67CF18346%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FAA831EF0-F19F-4812-8C4B-7FB67CF18346%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=GTeDIRHYQ4kX3nPcTWRJpTy8syQn413YucVDJieuQGY%3D&reserved=0>.

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2BSddAy0wEBNkbuPCwB4t421ChHFivjVr8%2BTHLLd6uOjhr2-%3DA%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCA%252BSddAy0wEBNkbuPCwB4t421ChHFivjVr8%252BTHLLd6uOjhr2-%253DA%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc490be1e6ddb4a06019908dacefb530d%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638049876083107739%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=wek4Cq%2Fu%2B4z9rimo8vCFGyE5OQaY5sXRh%2B9HMeOuhLc%3D&reserved=0>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6BDCA021-5014-447B-AE3B-54B9E5D043D3%40nist.gov.

Reply via email to