2

Below is my playbook that works fine.

---
- hosts: "PROD_SERVERS"
  user: "{{ USER }}"

  tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: "Server is UNREACHABLE."
         when: "hostvars[item].ansible_facts|list|length == 0"
         with_items: "{{ groups.PROD_SERVERS }}"

However, can you help me with the syntax when the host is presented as wildcard i.e {{ ENV }}_*?

---
- hosts: "{{ ENV }}_*"
  user: "{{ USER }}"

  tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: "Server is UNREACHABLE."
         when: "hostvars[item].ansible_facts|list|length == 0"
         with_items: "{{ groups.{{ ENV }}_* }}" # <------- Need help with this part of the code
U880D
  • 8,601
  • 6
  • 24
  • 40
Ashar
  • 2,942
  • 10
  • 58
  • 122

1 Answers1

3

There are special variables also called as "magic variables" to identify the hosts in the current play. They are:

  • ansible_play_hosts
  • ansible_play_batch

You can use one of these variables to loop with instead of the inventory group name.

Example with ansible_play_hosts:

    - fail:
        msg: "Server is UNREACHABLE."
      when: hostvars[item].ansible_facts|list|length == 0
      with_items: "{{ ansible_play_hosts }}"

Update:

Changed the example for Ansible version "2.4.x", the loop should be performed with with_items rather than loop. Quoting from official documentation

We added loop in Ansible 2.5. It is not yet a full replacement for with_<lookup>, but we recommend it for most use cases.

Note: For Ansible "2.9.16" and "2.10.2" loop works as well.

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • i'm getting the following error trying your suggestion: `TASK [Check if all hosts are reachable] **************************************** fatal: [usdfw33as75v.mrshmc.com]: FAILED! => {"msg": "Unexpected failure in finding the lookup named '{{ ansible_play_hosts }}' in the available lookup plugins"` – Ashar Jan 28 '21 at 17:01
  • i tried `gather_facts: true` as well as removing `gather_facts` all-together along with `{{ ansible_play_hosts_all }}` and `{{ ansible_play_batch }}` but i keep getting `Unexpected failure in finding the lookup named` – Ashar Jan 28 '21 at 17:13
  • There is nothing wrong with the answer. This is working for me as per the documentation, I have tried with Ansible version "2.10.2" and "2.9.16". Try looking at the list of plugins `ansible-doc -t lookup -l`. – seshadri_c Jan 28 '21 at 17:27
  • which plugin should i look for this to work ? My ansible version is `2.4.2.0` – Ashar Jan 28 '21 at 17:35
  • Not sure which plugin it is for sure but `inventory_hostnames` seems related. Could be due to Ansible version too. Does `debug` of this `var` give any output? – seshadri_c Jan 28 '21 at 18:04
  • `TASK [debug] ***************************************************************************************** ok: [localhost] => { "msg": [ "localhost" ] } ` Seems it is not a list to loop with. – Ashar Jan 28 '21 at 18:50
  • You are running it on localhost, and it is a list. Note `[]`.. From the question I thought it looks like you want to run on a set of servers matching a pattern like `hosts: "prod-*"`. Also use `loop` or `with_items`, and not `lookup`. – seshadri_c Jan 28 '21 at 18:52
  • I updated the answer. For Ansible 2.4.x `with_items` should be used. – seshadri_c Jan 28 '21 at 19:07