ansible delegate_to变量

时间:2016-10-28 06:20:58

标签: variables delegates ansible load-balancing

您好我有以下动态/静态主机组条目,其中包含有关数据中心和环境的信息

tag_Cluster_gateway_use1_qa_gw_proxy

我需要使用上面的delegate_to组条目,其中两个变量是内联传递的,如下所示

- name: Copy Nginx Template
  template:
    src: nginx.conf.ctmpl.j2
    dest: "/var/consul/template/web.conf.ctmpl"
  delegate_to: "{{ item }}"
  with_items: "groups.tag_Cluster_gateway_{{ datacenter }}_{{ stage }}_gw_proxy"

但是当结果是

failed: [172.16.16.136] (item=groups.tag_Cluster_gateway_use1_qa_gw_proxy) => {"item": "groups.tag_Cluster_gateway_use1_qa_gw_proxy", "msg": "Failed to connect to the host via ssh.", "unreachable": true}
fatal: [172.16.16.136]: UNREACHABLE! => {"changed": false, "msg": "All items completed", "results": [{"_ansible_item_result": true, "item": "groups.tag_Cluster_gateway_use1_qa_gw_proxy", "msg": "Failed to connect to the host via ssh.", "unreachable": true}]}

我认为Ansible正在将groups.tag_Cluster_gateway_use1_qa_gw_proxy作为主持人条目而不是群组条目。

还尝试了

delegate_to: "{{ item }}"
  with_items: "{{ groups['tag_Cluster_gateway_{{ datacenter }}_{{ stage }}_gw_proxy'] }}"

结果是

[DEPRECATION WARNING]: Skipping task due to undefined Error, in the future this will be a fatal error.: 'dict object' has no attribute 
'tag_Cluster_gateway_{{ datacenter }}_{{ stage }}_gw_proxy'.
This feature will be removed in a future release. Deprecation warnings can
 be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [172.16.16.136]: FAILED! => {"failed": true, "msg": "'item' is undefined"}

但如果我像groups.tag_Cluster_gateway_use1_qa_gw_proxy那样静态提及它可以正常工作。

希望我已经为这个问题提供了足够的细节。请帮助我了解如何通过deletage_to传递给主机组的变量

1 个答案:

答案 0 :(得分:1)

你的问题很久以前,所以我认为它不再是问题了。如果仍然存在,请尝试以下操作:

delegate_to: "{{ item }}"
with_items: "groups['tag_Cluster_gateway_{{ datacenter }}_{{ stage }}_gw_proxy']"

整个语句的{{}}括号允许ansible搜索整个“tag _..._ proxy”语句作为变量。 Ansible似乎仍尝试用整个语句替换{{item}},这就是为什么错误消息指出“'item'未定义”