ansible - Ansible - 从特定角色开始
问题描述
我有一本只调用角色的剧本。这是它的样子:(里面大约有20个角色)
---
- hosts: prod1234
roles:
- role1
- role2
- role3
有时,一个角色失败了,我不想重新开始,因为每个角色都很大,我只想从那个点或下一个开始。
对于任务,我知道--start-at-task="task-name"
. 我可以对角色做类似的事情吗?
我目前的解决方案是注释掉我不需要的所有行并再次运行它..
先谢谢了!~~
解决方案
快速 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
推荐阅读
- python - opencv - IndexError:索引 26 超出轴 0 的范围,大小为 17
- r - 使用日期范围匹配
- ubuntu - Nginx 显示错误的时间/时区
- yocto - 将 net-snmp 包添加到 poky yocto 失败
- multithreading - 即使 server.tomcat.max-threads=5,Springboot 也会增加线程
- php - 使用 Laravel 查询生成器转换 MySQL 查询
- nginx - 添加新入口时如何避免重新启动/断开所有连接?
- android - Recyclerview 自动每 10 行更改一次信息
- c# - 无法加载文件或程序集 Autofac
- node.js - 如何将多个媒体 url 的数组传递到 Twilio 消息中