How to end a whole play while using serial (Ansible 2.9)

Viewed 143

Using Ansible 2.9, how do you end a playbook while also using serial?

When I run the following code that is a rolling upgrade, it executes the prompt for each target system. When using serial, run_once only seems to work for the current target. I only want it to execute once.

- hosts: all
  become: yes
  serial: 1

  handlers:
    - include: handlers/main.yml

  pre_tasks:

  - name: Populate service facts
    service_facts:
   
  - name: Prompt
    pause:
      prompt: "NOTE: You are running a dangerous playbook. Do you want to continue? (yes/no)"
    register: confirm_input
    run_once: True
 
  - name: end play if user didn't enter yes
    meta: end_play
    when: confirm_input.user_input | default ("yes") =="no"
    run_once: True

  tasks:
  (other stuff)
  
1 Answers

run_once will be executed at each serial execution in the play. That means, if you choose serial = 1, it will be asked to confirm as many times as the quantity of targets on the play.

Check Ansible docs: https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html#running-on-a-single-machine-with-run-once

When used together with serial, tasks marked as run_once will be run on one host in each serial batch. If the task must run only once regardless of serial mode, use when: inventory_hostname == ansible_play_hosts_all[0] construct.

to avoid default ansible behaviour and do what you want, follow the doc and use when: inventory_hostname == ansible_play_hosts_all[0] on the prompt task.

Related