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

岁酱吖の 提交于 2020-08-03 12:18:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!