ansible - Ansible 在 delegate_to 之后无法评估变量
问题描述
我有一部戏,有一套角色。最里面的角色有一个委托任务,试图评估一个变量。并且打了几层未定义。
角色:
- 我有一个 App 特定的角色
deploy_app1
,它可以与 play 和 host vars 一起使用来部署这个应用程序。 - 应用特定角色使用服务角色
deploy_service
- 为应用设置负载平衡和 IP 地址。其他应用程序可能会使用此角色。 - 负载平衡/服务角色使用 DNS 角色
dns_records
,在需要它们的系统中创建 DNS 条目。
所以调用堆栈将是 Playbook->deploy_app1->deploy_service->dns_records
DNS 角色有一个怪癖,它需要与 WinRM 交互。所以它有 delegate_to 的任务。我必须从该系统的主机变量中设置连接变量,例如:
ansible_connection: "{{ hostvars[dns_records_winrm_host]['ansible_connection'] }}"
库存设置如下:
host_app1_dev
运行应用程序部署。它有一个变量来说明哪个 winrm 主机将进行部署。app1_dns_winrm_host
,以及所有 DNS gubbins。- Winrm 主机能够执行 AD DNS 任务。它有一个带有 winrm 和 credssp 连接变量的主机文件。
在角色中,变量被包裹在堆栈中。我以 winrm 主机为例。还有其他变量。
- app1 获取由 vars 中的变量设置的变量
host_app1_dev
。然后它将这些传递给子角色。 - 该
deploy_service
角色比 app1 更通用,适用于许多应用程序。因此,当包含在内时,它会获得一个名为deploy_service_dns_winrm_host: "{{app1_dns_winrm_host}}"
. - 该
dns_records
角色的目的不仅仅是服务,它提供了它需要创建的记录的详细信息(多个系统、多个记录层)。这也需要知道winrm_host。因此,当服务包含在内时,其 vars 包括:dns_records_winrm_host: "{{ deploy_service_dns_winrm_host }}"
.
然后在dns_records
角色中引用它:
- name: Debug that we connected to winrm host
delegate_to: "{{dns_records_winrm_host}}"
vars:
ansible_connection: "{{ hostvars[dns_records_winrm_host]['ansible_connection'] }}"
.
.
debug:
msg: This role ran remotely.
因此,当我运行它时,我希望调试输出它远程运行。就像是:
TASK [dns_records : Debug that we connected to winrm host] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [app1_play] => {
"msg": "This ran remotely"
}
实际发生的是这样的:
TASK [dns_records : Debug that we connected to winrm host]] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [app1_play]: FAILED! => {"msg": "{{ deploy_service_dns_winrm_host }}: {{ app1_dns_winrm_host }}: 'app1_dns_winrm_host' is undefined"}
我在这里想念什么?如何让 delegate_to 能够引用这些变量?
解决方案
推荐阅读
- react-native - 使水平行的垂直滚动视图反应原生
- mysql - 如何先返回特定行,然后在 MySQL 中随机化其余行?
- spring - Spring Boot 仅适用于微服务吗?我可以将 Spring Boot 用于单体架构吗?
- javascript - 如何以“星期六,dd.MM.YYYY”的格式获取一年中的所有星期六?
- javascript - 队列管理系统(Laravel 6)的实时更新通知声音
- python - 如何使用另一个 Python 返回一个变量
- java - 关于 JAVA 中的连接池:我需要消耗服务器响应吗?如果不 ?池化如何工作
- python - 同时显示所有验证错误,Django Rest Framework中的错误累积
- c# - 无法使用 Microsoft Graph api graphServiceClient.Users.Request() C# 感染所有用户详细信息
- excel - 基于日期锁定列