Got an ansible role which consists of many tasks. Therefore these tasks are split up in seperate files for better traceability.
folder roles/myrole/tasks/:
step1.yml
step2.yml
...
step10.yml
main.yml
The main task only consists of included tasks.
roles/myrole/tasks/main.yml:
- include_tasks: step1.yml
- include_tasks: step2.yml
...
- include_tasks: step10.yml
site.yml
- name: Deploy myrole
hosts: rolehosts
remote_user: roleuser
roles:
- myrole
The server has to be rebooted several times during deployment. For now every task file contains tasks like this:
- name: Step 10 - reboot
become: true
shell: sleep 2 && shutdown -r now
async: 1
poll: 0
- name: Step 10 - wait for server to return after reboot
wait_for: >
host={{ ansible_default_ipv4.address }}
port=22
delay=10
timeout=120
delegate_to: localhost
So instead of rewriting these tasks over and over again in every stepX.yml
it seems practical to use a handler
instead. But handlers only get executed at the end of a play which is not practical in this case when the server has to be rebooted for example in the middle of step 5. To avoid this I could use a flush_handlers
task in every step.
I don't see the profit here. Instead of rewriting the reboot tasks in every step I would rewrite the flus_handlers task in every step.
Is there a better way to do this?
The server needs to be reliably rebooted at certain points in every step. The playbook should continue executing when the server is online again.
Something like this?
Keep in mind that this is a kind of pseudo-code, just to point out sequence of events.
step_and_reboot.yml
main.yml