cristian.andrei.stan
cristian.andrei.stan

Reputation: 523

Ansible - how to control the order of the hosts when playbook is ran

Let's say that we have defined two machines in our inventory file:

[db-server-preprod] 172.16.0.1 172.16.0.2

If I run a playbook against this group will run in the same time on both machines if serial is 0 or sequentially if is 1 and the order seems to be the one in which the IPs are defined in the group.

But the question is if can I control the order of the playbook execution on the defined machines?

The same role behaves slightly different for one of the machines (the master) as opposed to all the others but is important that the machine that I want to be the master will be the first one on which the playbook executes.

My ideas so far:

Upvotes: 7

Views: 15718

Answers (2)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

You may place your master host into separate group, then apply common role to all servers at once, then apply master-roles only to master server, and slave-roles to all servers except master using excluding pattern.

Inventory:

[all-servers]
host1
host2
host3
host4

[master-server]
host2

Playbook:

---
- hosts: all-servers
  gather_facts: no
  tasks:
    - debug: msg=role-common

- hosts: master-server
  gather_facts: no
  tasks:
    - debug: msg=role-master

- hosts: all-servers:!master-server
  gather_facts: no
  tasks:
    - debug: msg=role-slave

Upvotes: 10

Asier Gomez
Asier Gomez

Reputation: 6578

You can use in the playbook main.yml "serial = 1" so it executes one by one, and you can ensure that the playbook would execute on the master node and after that it would execute on the others nodes.

- hosts: allnodes
  remote_user: "{{user}}"
  become: True
  serial: 1
  roles:
  - role: your_role

Upvotes: 3

Related Questions