I have read this question and this one as well as the relevant Ansible documentation.
I still can't work out what the correct syntax to use is. I am trying the following:
# Find all of the load-balancers reference
- name: Gather facts about all load-balancers
elb_application_lb_facts:
register: load_balancers
# Get the load-balancer we want
- name: Find the correct load-balancer
set_fact:
load_balancer_dns: "{{ load_balancer_dns }}"
elb_zone_id: "{{ elb_zone_id }}"
with_items: "{{ load_balancers | json_query(lb_query) }}"
vars:
lb_query: "load_balancers[?load_balancer_name=='{{load_balancer_name}}'].{load_balancer_dns: dns_name, elb_zone_id: canonical_hosted_zone_id}"
This always results in the error (in the second task): The task includes an option with an undefined variable. The error was: 'load_balancer_dns' is undefined
.
The JSON output from the elb_application_lb_facts
does include the dns_name
and canonical_hosted_zone_id
keys and they are on the same level as load_balancer_name
(not nested somewhere).
Please could somebody help me with the syntax for this?
You need to include "item" when you set the fact, because you're looping "with_items":
# Get the load-balancer we want
- name: Find the correct load-balancer
set_fact:
load_balancer_dns: "{{ item.load_balancer_dns }}"
elb_zone_id: "{{ item.elb_zone_id }}"
with_items: "{{ load_balancers | json_query(lb_query) }}"
vars:
lb_query: "load_balancers[?load_balancer_name=='simple-application-load-balancer'].{load_balancer_dns: dns_name, elb_zone_id: canonical_hosted_zone_id}"
Documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items