Irfad
Irfad

Reputation: 35

ansible delegate_to variable

Hi I have below dynamic/static host group entry which has information about datacenter and environment

tag_Cluster_gateway_use1_qa_gw_proxy

I need to use above group entry for delegate_to with two variables passed inline like below

- 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"

But when when result was

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}]}

Which I assume Ansible is taking groups.tag_Cluster_gateway_use1_qa_gw_proxy as a host entry rather group entry.

Also tried with

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

And result was

[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"}

But if I statically mention like groups.tag_Cluster_gateway_use1_qa_gw_proxy it works fine.

Hope I've provided enough details for the issue. Please help me on how to have variables passed with deletage_to for host group

Upvotes: 0

Views: 8937

Answers (1)

Florian Wegert
Florian Wegert

Reputation: 11

Your question was pretty long ago, so I assume it is not a problem anymore. If it still is, try the following:

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

The {{ }} brackets around the whole statement lets ansible search for the whole "tag_..._proxy" statement as variable. Ansible still seems to try to replace {{ item }} with the whole statement, which is why the error message states "'item' is undefined"

Upvotes: 1

Related Questions