Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pause ansible playbook for user confirmation, whether to run rest tasks

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.

like image 502
Ajeet Khan Avatar asked Dec 15 '15 13:12

Ajeet Khan


People also ask

How do you pause a playbook in Ansible?

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 .

How do you handle long running tasks in Ansible?

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.

What is async and poll in Ansible?

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).

Which command tells Ansible to run the playbook on all the hosts except host1?

Folder Which command tells ansible to run the playbook on all the hosts except host1? ansible-playbook playbooks/PLAYBOOK_NAME.


2 Answers

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 
like image 126
udondan Avatar answered Sep 20 '22 15:09

udondan


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 
like image 28
ahuetWL Avatar answered Sep 18 '22 15:09

ahuetWL