首页 > 解决方案 > Ansible 在 delegate_to 之后无法评估变量

问题描述

我有一部戏,有一套角色。最里面的角色有一个委托任务,试图评估一个变量。并且打了几层未定义。

角色:

所以调用堆栈将是 Playbook->deploy_app1->deploy_service->dns_records

DNS 角色有一个怪癖,它需要与 WinRM 交互。所以它有 delegate_to 的任务。我必须从该系统的主机变量中设置连接变量,例如: ansible_connection: "{{ hostvars[dns_records_winrm_host]['ansible_connection'] }}"

库存设置如下:

在角色中,变量被包裹在堆栈中。我以 winrm 主机为例。还有其他变量。

然后在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 能够引用这些变量?

标签: ansible

解决方案


推荐阅读