How to list all currently targeted hosts in an Ansible play

You are looking for ‘play_hosts’ variable

---
- hosts: all

  tasks:
    - name: Create a group of all hosts by app_type
      group_by: key={{app_type}}

    - debug: msg="groups={{groups}}"
      run_once: true

- hosts: web:&some_other_group

  tasks:
   - debug: msg="play_hosts={{play_hosts}}"
     run_once: true

would result in

TASK: [Create a group of all hosts by app_type] *******************************
changed: [web1] => {"changed": true, "groups": {"web": ["web1", "web2"], "load_balancer": ["web3"]}}

TASK: [debug msg="play_hosts={{play_hosts}}"] *********************************
ok: [web1] => {
    "msg": "play_hosts=['web1']"
}

inventory:

[proxy]
web1 app_type=web
web2 app_type=web
web3 app_type=load_balancer

[some_other_group]
web1
web3

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)