How to make Ansible playbook on the first host in a group?

How to start the player only on the first host in the group?

I expect something like this:

---
- name: playbook that only run on first host in the group
  hosts: "{{ groups[group_name] | first }}"

  tasks:
   - debug:
       msg: "on {{ inventory_hostname }}"

But this does not work, gives an error:

'groups' undefined

How can I make it work?

+4
source share
1 answer

You can use:

hosts: group_name[0]

Inventory host values ​​(specified in the directive hosts) are processed using a custom analyzer that does not allow Jinja2 expressions, such as the regular template engine.

Read about Patterns .

+6
source

Source: https://habr.com/ru/post/1672885/


All Articles