Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - task serial 1 reverse order

Tags:

ansible

I'd like to create two playbooks, one to stop an environment, another to start it.

Part of the environment is a RabbitMQ cluster, for which stop/start order is quite important, specifically the last node stopped needs to be the first node started.

I was wondering if there is a way to specify a reverse order for running a task against a group. That way I could apply the stop with serial 1, and the start with serial 1 and reverse group order.

I haven't found a way to do that but to define the rabbitmq host group twice (under different names), in inverted order, which seems a bit distasteful.

Also attempted following:

- hosts: "{ myhostsgroup | sort(reverse=False) }"
  serial: 1

And

- hosts: "{ myhostsgroup | reverse }"
  serial: 1

But result stays the same, whichever case and its variation (reverse=True, reverse|list) is attempted

Any help would be greatly appreciated.

like image 272
Olivier Avatar asked Jul 25 '17 10:07

Olivier


People also ask

What is the ansible playbook execution order?

Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.

What is Pre_tasks in Ansible?

What is Ansible pre_tasks? Ansible pretask is a conditional execution block that runs before running the play. It can be a task with some prerequisites check (or) validation.

What does Serial do in Ansible?

You can do rolling updates in Ansible using the serial keyword. This gives you the ability to specify the number of hosts you want to execute against at a time on a per-playbook basis. This also allows you to slowly ramp up your deployment in batches by providing an increasing list.

Does Ansible run tasks in parallel?

Running parallel tasks in Ansible is not a problem. Ansible or in fact any kind of configuration management tool was meant for this. They are suppossed to be provisioning multiple host systems simultaneously and hence parallel execution of jobs is one of their strengths.


1 Answers

You can create dynamic groups in runtime:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - add_host:
        name: "{{ item }}"
        group: forward
      with_items: "{{ groups['mygroup'] }}"

    - add_host:
        name: "{{ item }}"
        group: backward
      with_items: "{{ groups['mygroup'] | reverse | list }}"

- hosts: forward
  gather_facts: no
  serial: 1
  tasks:
    - debug:

- hosts: backward
  gather_facts: no
  serial: 1
  tasks:
    - debug:
like image 75
Konstantin Suvorov Avatar answered Sep 26 '22 15:09

Konstantin Suvorov