19

How can I run a playbook only on first host in the group?

I am expecting 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 doesn't work, gives error:

'groups' is undefined

How can I make it work?

techraf
  • 64,883
  • 27
  • 193
  • 198
Jakim
  • 1,713
  • 7
  • 20
  • 44

1 Answers1

37

You can use:

hosts: group_name[0]

Inventory hosts values (specified in the hosts directive) are processed with a custom parser, which does not allow Jinja2 expressions like the regular template engine does.

Read about Patterns.

fabio
  • 247
  • 2
  • 8
techraf
  • 64,883
  • 27
  • 193
  • 198