Get role names for multiple plays in the same playbook file

36 views
Skip to first unread message

ricardo barbosa

unread,
Jul 29, 2024, 10:54:15 PM7/29/24
to Ansible Project

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 }}"
--------------------------------------------

I tried using the "role_names" magic variable but it didn't work as needed. I run the playbook as follows

----------------------------------
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", but in my tests it only shows the "linux" role. I thought about creating a variable and adding the role names to the hostvars variable of host01 like "hostvars['host01'][roles]" or something similar, but I wonder if there isn't something ready already.

Thank you in advance.

Vladimir Botka

unread,
Jul 30, 2024, 1:36:20 AM7/30/24
to ricardo barbosa, ansible...@googlegroups.com
On Mon, 29 Jul 2024 22:53:40 -0400
ricardo barbosa <ricardoba...@gmail.com> wrote:

> I have a host host01 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 }}"
> --------------------------------------------
> 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

shell> cat audit.yml
- name: Audit
when: audit | d(false) | bool
run_once: true
block:
- set_fact:
hosts_roles: "{{ hosts_roles | d([]) +
[ansible_play_hosts_all |
product([ansible_role_names]) |
community.general.dict] }}"
- meta: end_play

and import it in each play

shell> cat play.yml
- name: Play 1
hosts: GROUP01
pre_tasks:
- import_tasks: audit.yml
roles:
- nginx
- php

- name: Play 2
hosts: GROUP02
pre_tasks:
- import_tasks: audit.yml
roles:
- mysql

- name: Play 3
hosts: GROUP03
pre_tasks:
- import_tasks: audit.yml
roles:
- linux

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

- name: Audit
hosts: all
tasks:
- name: Results
when: audit | d(false) | bool
run_once: true
block:
- debug:
var: hosts_roles | to_yaml
- debug:
var: hosts_roles |
combine(list_merge='append_rp') | to_yaml


The below command

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

gives what you want

hosts_roles | to_yaml: |-
- host01: [nginx, php]
- host01: [linux]

hosts_roles | combine(list_merge='append_rp') | to_yaml:
- host01: [nginx, php, linux]


HTH,

--
Vladimir Botka

Brian Coca

unread,
Jul 31, 2024, 5:20:05 PM7/31/24
to ansible...@googlegroups.com, ricardo barbosa
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
And enable stat display in the default callback (others might/might
not display them).


--
----------
Brian Coca (he/him/they/them)

Reply all
Reply to author
Forward
0 new messages