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/:


The main task only consists of included tasks.


- include_tasks: step1.yml
- include_tasks: step2.yml
- include_tasks: step10.yml


- name: Deploy myrole
  hosts: rolehosts
  remote_user: roleuser

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


- import_tasks: "{{ step_name }}.yml"
- shell: shutdown -r now
- local_action: wait_for


- include_tasks: step_and_reboot.yml
    - step1
    - step2
    - step3
    loop_var: step_name
  • Looks pretty neat, will try it! It just feels wrong to write the same task over and over again or is this a common practice? Ansible is new to me. – duenni Nov 16 '17 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.