Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a playbook on multiple host groups one at a time

I want to run a playbook containing some roles on multiple host groups I create dynamically with the group_by module.

I'm able to do it like the example below (ping replacing my actual role).

I was wondering if there is a way to run each group separately in a loop instead of listing all instance ids. I don't want to create a duplicate line with every instance id.

The purpose here is to deploy to one instance in every data center at a time instead of running all with a low serial which takes a long time.

There might be a different way of doing it, I don't want to create static groups in the inventory for each instance_id as well.

---
- hosts: tag_type_edgenode
  tasks:
    - group_by: key=instance_id_{{instance_id}}
      register: dyn_groups

- hosts: instance_id_1
  tasks:
    - ping:
- hosts: instance_id_2
  tasks:
    - ping:
- hosts: instance_id_3
  tasks:
    - ping:
- hosts: instance_id_4
  tasks:
    - ping:
like image 508
Shay Rybak Avatar asked Jun 20 '17 11:06

Shay Rybak


People also ask

Can a playbook have multiple plays?

A Playbook can have multiple Plays and a Play can have one or multiple Tasks. In a Task a Module is called, like the Modules in the previous chapter. The goal of a Play is to map a group of hosts to a list of Tasks. The goal of a Task is to implement Modules against those hosts.

Can I run multiple Ansible playbooks in parallel?

Ansible is not designed to run multiple playbooks at the same time in one process - for example, because the tasks differ from playbook to playbook and there is no step "taskA" in playbook1 and playbook2 at the same time. You need to run every playbook in one separate process (like with ansible-playbook ... & ).

How do I control Ansible playbook only on specific hosts?

Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.


1 Answers

There is a simpler method using the size (length) of each group. However, this runs on all members of a group, flowing through the groups sequentially. I think OP was asking for how to act on the first member of each group, which I am also working on figuring out.

- name: "Restore selected devices in model_dc"
  hosts: group_1, group_2, group_3, group_4, group_5, group_6, group_7, !excluded
  any_errors_fatal: true    # Stop entire play if one host fails
  gather_facts: no
  order: inventory
  serial: 
    - "{{ groups['group_1'] | length }}" # The number of hosts for first batch
    - "{{ groups['group_2'] | length }}" # The number of hosts for second batch
    - "{{ groups['group_3'] | length }}"
    - "{{ groups['group_4'] | length }}"
    - "{{ groups['group_5'] | length }}"
    - "{{ groups['group_6'] | length }}"
    - "{{ groups['group_7'] | length }}" # The number of hosts for every batch until the end.
like image 138
Joanna Delaporte Avatar answered Oct 21 '22 10:10

Joanna Delaporte