pause ansible playbook for user confirmation, whether to run rest tasks
I need to reboot all hosts but I can't risk letting a user to do so by just pressing 'Enter'.
For a true yes/no confirmation in the middle of a playbook, I searched quite a while before finding this post. How to do this is not so well described in Ansible documentation, but here is the code:
- name: Confirm important file deletion
pause:
prompt: "Are you sure you want to delete backup.db? (yes/no)"
register: confirm_delete
- name: Delete backup file
file:
path: /home/admin/backup.db
state: absent
when: confirm_delete.user_input | bool
A good way to achieve prompt for each task without modifying the playbook itself is to use the --step
option of ansible-playbook
command.
This will allow you to confirm each step before it is run. You have options here to select (N)o/(y)es/(c)ontinue
. N
skips this step, y
runs the step and c
continues the rest of the playbook without further prompting (useful when you're debugging and are past the troublesome place.)
Note that also works fine with the --check
option.
Official Ansible documentation is here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_startnstep.html#step
The pause
module actually does exactly that. But it does not give you an option to answer yes
or no
. Instead it expects the user to press Ctrl+C and then a for abort. To continue the user simply needs to press Enter.
Since this is not perfectly obvious to the user you can describe it in the prompt
parameter.
- name: Exterminate mankind
pause:
prompt: Please confirm you want to exterminate mankind! Press return to continue. Press Ctrl+c and then "a" to abort