问题
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.
回答1:
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
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/34290525/pause-ansible-playbook-for-user-confirmation-whether-to-run-rest-tasks