Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 421
Jakim Avatar asked Mar 22 '17 04:03

Jakim


People also ask

Do Ansible playbooks run in order?

Within each play, tasks also run in order from top to bottom. Playbooks with multiple 'plays' can orchestrate multi-machine deployments, running one play on your webservers, then another play on your database servers, then a third play on your network infrastructure, and so on.

How do I Group hosts in Ansible?

Ansible uses a combination of a hosts file and a group_vars directory to pull variables per host group and run Ansible plays/tasks against hosts. group_vars/all is used to set variables that will be used for every host that Ansible is ran against.


1 Answers

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.

like image 126
techraf Avatar answered Oct 21 '22 10:10

techraf