I am running an ansible-playbook
which have many tasks listed. All of them use to get run one by one, but I want to pause
the playbook after a particular tasks to asks the user if he wants to continue running the rest of the tasks or exit. I have seen the pause module
of ansible but couldn't see any example which asks users for yes or no which in turn continue or exit the ansible-playbook accordingly.
The default behavior is to pause with a prompt. To pause/wait/sleep per host, use the wait_for module. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early press ctrl+c and then c .
By default, for Ansible tasks, the connections stay open until the task is done on each node, but this may not be the desired behavior for some functions as sometimes the task can take more time than SSH timeouts. You can run such long running tasks in the background and check their status later.
If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).
Folder Which command tells ansible to run the playbook on all the hosts except host1? ansible-playbook playbooks/PLAYBOOK_NAME.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With