我正在尝试将 Kubespray 的所有角色包装在块/救援块中,所以我不得不从通常的roles
包含中移动,如下所示:
- hosts: kube-master[0]
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
roles:
- { role: kubespray-defaults}
- { role: kubernetes-apps/rotate_tokens, tags: rotate_tokens, when: "secret_changed|default(false)" }
- { role: win_nodes/kubernetes_patch, tags: ["master", "win_nodes"]}
对此:
- hosts: kube-master[0]
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
vars:
roles:
- name: "kubespray-defaults"
- name: kubernetes-apps/rotate_tokens
tags: rotate_tokens
when: "secret_changed|default(false)"
- name: win_nodes/kubernetes_patch
tags: ["master", "win_nodes"]
- name: "ems-notification"
msg: kubespray-defaults, kubernetes-apps/rotate_tokens and win_nodes/kubernetes_patch completed
tasks:
- include_tasks: roles/a4-roles/tasks/main.yml
loop: "{{ roles }}"
a4-roles/tasks/main.yml 是:
- name: a4-roles
when: item.when | default(omit)
block:
- include_role:
name: "{{ item.name }}"
apply:
tags: >-
{%- if item.tags is defined -%}
"{{ item.tags }}"
{%- else -%}
""
{%- endif -%}
rescue:
- include_role:
name: "ems-notification"
vars:
msg: an error has occurred
host: "{{ inventory_hostname }}"
result: "{{ ansible_failed_result.msg | trim | default(omit) }}"
role: "{{ item.name }}"
error: "true"
问题在于,kubespray-defaults
以及其他角色正在设置roles
块中后续角色正在使用的一些变量和默认值。使用include_role
这些变量时,默认值会丢失。有什么办法可以保留它们并将它们传递给下一个角色?