jessefournier
jessefournier

Reputation: 193

Ansible - Start at specific role

I have a playbook that only calls roles. This is what it looks like: (there are about 20 roles in it)

---
- hosts: prod1234
  roles:
  - role1
  - role2
  - role3

Sometimes, a role fails, and I don't want to start over as each role is huge and I would just like to start at that point or the next one.

With tasks, I know there's a flag for --start-at-task="task-name". Is there something similar I can do with roles?

My current solution is to comment out all the lines I don't need and run it again..

Thanks ahead!~~

Upvotes: 0

Views: 855

Answers (1)

Zeitounator
Zeitounator

Reputation: 44808

Quick n dirty solution. The following roleimport.yml playbook

# Note you will have to implement error management (e.g. you give a role that does not exist).
- name: quickNdirty roles start demo
  hosts: localhost
  gather_facts: false

  vars:
    full_role_list:
      - myfirstrole
      - mysecondrole
      - thirdone
      - next
      - last

    # We calculate a start index for the above list. By default it will be 0.
    # Unless we pass `start_role` var in extra_vars: the index will be set
    # to that element
    start_index: "{{ full_role_list.index(start_role|default('myfirstrole')) }}"

    # We slice the list from start_index to end
    current_role_list: "{{ full_role_list[start_index|int:] }}"

  tasks:
    # Real task commented out for this example
    # - name: Import selected roles in order
    #   import_role:
    #     name: "{{ item }}"
    #   loop: "{{ current_role_list }}"

    - name: Debug roles that would be used in above commented task
      debug:
        msg: "I would import role {{ item }}"
      loop: "{{ current_role_list }}"

gives:

$ ansible-playbook roleimport.yml 

PLAY [quickNdirty roles start demo] *******************************************************************************************************************************************************************************************

TASK [Debug roles that would be used in above commented task] *****************************************************************************************************************************************************************
ok: [localhost] => (item=myfirstrole) => {
    "msg": "I would import role myfirstrole"
}
ok: [localhost] => (item=mysecondrole) => {
    "msg": "I would import role mysecondrole"
}
ok: [localhost] => (item=thirdone) => {
    "msg": "I would import role thirdone"
}
ok: [localhost] => (item=next) => {
    "msg": "I would import role next"
}
ok: [localhost] => (item=last) => {
    "msg": "I would import role last"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


$ ansible-playbook roleimport.yml -e start_role=thirdone

PLAY [quickNdirty roles start demo] *******************************************************************************************************************************************************************************************

TASK [Debug roles that would be used in above commented task] *****************************************************************************************************************************************************************
ok: [localhost] => (item=thirdone) => {
    "msg": "I would import role thirdone"
}
ok: [localhost] => (item=next) => {
    "msg": "I would import role next"
}
ok: [localhost] => (item=last) => {
    "msg": "I would import role last"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



$ ansible-playbook roleimport.yml -e start_role=last

PLAY [quickNdirty roles start demo] *******************************************************************************************************************************************************************************************

TASK [Debug roles that would be used in above commented task] *****************************************************************************************************************************************************************
ok: [localhost] => (item=last) => {
    "msg": "I would import role last"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


# Implement error management yourself if you need it.
$ ansible-playbook roleimport.yml -e start_role=thisIsAnError

PLAY [quickNdirty roles start demo] *******************************************************************************************************************************************************************************************

TASK [Debug roles that would be used in above commented task] *****************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ full_role_list[start_index|int:] }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ full_role_list.index(start_role|default('myfirstrole')) }}'. Error was a <class 'ValueError'>, original message: 'thisIsAnError' is not in list"}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Upvotes: 1

Related Questions