6

I have a playbook defined as below:

- name: install percona rpms
  hosts: imdp
  roles:
    - role1
    - role2
    - role3
    - role4

I just want the tasks defined in role 3 to be executed serially. If I define serial: 1 in the role3 tasks, it doesn't work. All tasks are executed in parallel. But if I defined serial: 1 in the main yaml (the above yaml) then all the roles are executed serially, which is also not needed.

How can I get just role3 to be executed serially?

2 Answers 2

3

"serial" is available in a play only. See Playbook Keywords. The solution is to split the roles among more plays. For example

- name: Play 1. install percona rpms
  hosts: imdp
  roles:
    - role1
    - role2

- name: Play 2. install percona rpms
  hosts: imdp
  serial: 1
  roles:
    - role3

- name: Play 3. install percona rpms
  hosts: imdp
  roles:
    - role4
3

As example (emulation)

my-tasks.yml:

---
- include_tasks: custom-tasks.yml
  when: inventory_hostname == item
  with_items: "{{ ansible_play_hosts }}"

custom-tasks.yml:

---
- debug:
    var: inventory_hostname
3
  • This solution has the added benefit of being able to use it from within a role.
    – fbicknel
    Commented May 11, 2023 at 18:46
  • this is exactly what i was looking for, and exactly same reason, to change a global variable, or to update variables on other hosts, but to run serial upgrades on them and to increment each time a host has been upgraded to keep a counter. thanks! hope i can get it to work beacuse this is some deep ansible coding what i have going on now!
    – blamb
    Commented Dec 7, 2023 at 18:01
  • i wasnt able to get a global variable fact set, using this, and the link included which shows to use a flat file instead. i dont think you need to emulate just to use the flatfile for memory. what would be nice is a true way to increment a fact, even if on the localhost only, each serial iteration.
    – blamb
    Commented Dec 7, 2023 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.