16

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?

4

1 回答 1

33

您可以使用:

hosts: group_name[0]

Inventory hosts 值(在hosts指令中指定)使用自定义解析器进行处理,该解析器不允许像常规模板引擎那样的 Jinja2 表达式。

阅读模式

于 2017-03-22T05:01:58.400 回答