0

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.

1

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

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

main.yml

- include_tasks: step_and_reboot.yml
  with_items:
    - step1
    - step2
    - step3
  loop_control:
    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.