How to make Ansible playbook run on first host in the group?
Asked Answered
P

1

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?

Pimiento answered 22/3, 2017 at 4:40 Comment(0)
I
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.

Italianize answered 22/3, 2017 at 5:1 Comment(2)
When I use this, I get a warning saying "[WARNING]: Could not match supplied host pattern, ignoring: group_name"Formalize
… because I'm stupid. I thought you meant the literal string group_name[0] but you clearly mean my_group_name[0] like in the docs.Formalize

© 2022 - 2024 — McMap. All rights reserved.