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
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
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
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,