首页 > 解决方案 > Ansible - 从特定角色开始

问题描述

我有一本只调用角色的剧本。这是它的样子:(里面大约有20个角色)

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

有时,一个角色失败了,我不想重新开始,因为每个角色都很大,我只想从那个点或下一个开始。

对于任务,我知道--start-at-task="task-name". 我可以对角色做类似的事情吗?

我目前的解决方案是注释掉我不需要的所有行并再次运行它..

先谢谢了!~~

标签: ansible

解决方案


快速 n 肮脏的解决方案。下面的roleimport.yml剧本

# 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 }}"

给出:

$ 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   

推荐阅读