# Get role names for multiple plays in the same playbook file

**URL:** <https://forum.ansible.com/t/get-role-names-for-multiple-plays-in-the-same-playbook-file/38837>\
**Category:** Ansible Project\
**Created:** [July 30, 2024, 2:53am UTC](https://forum.ansible.com/t/get-role-names-for-multiple-plays-in-the-same-playbook-file/38837 "2024-07-30T02:53:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardobarbosams](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@ricardobarbosams](https://forum.ansible.com/u/ricardobarbosams)\
**Post date:** [July 30, 2024, 2:53am UTC](https://forum.ansible.com/t/get-role-names-for-multiple-plays-in-the-same-playbook-file/38837/1 "2024-07-30T02:53:40Z")

</div>

Hello everyone

I have a playbook called “play.yml” with three plays and I would like to get the names of the roles that are applied to a certain host. Let’s say I have a host called host01 and this host is in the two groups: GROUP01 and GROUP03

----------play.yml----------------

- name: play 1  
hosts:

- GROUP01  
roles:

- nginx

- php

- name: play 2  
hosts:

- GROUP02  
roles:

- mysql

- name: play 3  
hosts:

- GROUP03  
roles:

- linux  
tasks:

- debug:  
msg: “{{ ansible\_role\_names }}”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 30, 2024, 5:35am UTC](https://forum.ansible.com/t/get-role-names-for-multiple-plays-in-the-same-playbook-file/38837/2 "2024-07-30T05:35:49Z")

</div>

> I have a host host01 in the two groups: GROUP01 and  
> GROUP03
> 
> ----------play.yml----------------  
> - name: play 1  
> &nbsp;&nbsp;hosts:  
> &nbsp;&nbsp;&nbsp;&nbsp;- GROUP01  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- nginx  
> &nbsp;&nbsp;&nbsp;&nbsp;- php
> 
> - name: play 2  
> &nbsp;&nbsp;hosts:  
> &nbsp;&nbsp;&nbsp;&nbsp;- GROUP02  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- mysql
> 
> - name: play 3  
> &nbsp;&nbsp;hosts:  
> &nbsp;&nbsp;&nbsp;&nbsp;- GROUP03  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- linux  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_role\_names }}"  
> --------------------------------------------  
> ansible-playbook play.yml --limit host01  
> ----------------------------------
> 
> So it will run on play 1 and play 3, so I wanted the "ansible\_role\_names"  
> variable to contain the values "nginx", "PHP" and "linux"

Create the audit on your own. For example, create the file

> cat audit.yml

- name: Audit  
&nbsp;&nbsp;when: audit | d(false) | bool  
&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;block:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts\_roles: "{{ hosts\_roles | d() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ansible\_play\_hosts\_all |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([ansible\_role\_names]) |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;community.general.dict] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- meta: end\_play

and import it in each play

> cat play.yml

- name: Play 1  
&nbsp;&nbsp;hosts: GROUP01  
&nbsp;&nbsp;pre\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- import\_tasks: audit.yml  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- nginx  
&nbsp;&nbsp;&nbsp;&nbsp;- php

- name: Play 2  
&nbsp;&nbsp;hosts: GROUP02  
&nbsp;&nbsp;pre\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- import\_tasks: audit.yml  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- mysql

- name: Play 3  
&nbsp;&nbsp;hosts: GROUP03  
&nbsp;&nbsp;pre\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- import\_tasks: audit.yml  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- linux

To display the audit results add one more play to the  
playbook

- name: Audit  
&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Results  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: audit | d(false) | bool  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: hosts\_roles | to\_yaml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: hosts\_roles |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine(list\_merge='append\_rp') | to\_yaml

The below command

> ansible-playbook play.yml -e audit=true --limit host01

gives what you want

&nbsp;&nbsp;hosts\_roles | to\_yaml: |-  
&nbsp;&nbsp;&nbsp;&nbsp;- host01: [nginx, php]  
&nbsp;&nbsp;&nbsp;&nbsp;- host01: [linux]

&nbsp;&nbsp;hosts\_roles | combine(list\_merge='append\_rp') | to\_yaml:  
&nbsp;&nbsp;&nbsp;&nbsp;- host01: [nginx, php, linux]

HTH,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 31, 2024, 9:19pm UTC](https://forum.ansible.com/t/get-role-names-for-multiple-plays-in-the-same-playbook-file/38837/3 "2024-07-31T21:19:36Z")

</div>

If it is just to display at the end of play, you can also use  
'set\_stats' [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set\_stats\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_stats_module.html)  
And enable stat display in the default callback (others might/might  
not display them).
