On Mon, 29 Jul 2024 22:53:40 -0400
ricardo barbosa <ricardobarbosam...@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

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20240730073549.6d9f42df%40gmail.com.

Attachment: pgpLq9Javyz1c.pgp
Description: OpenPGP digital signature

Reply via email to