问题
I am attempting to run a template task per ec2 instance, grabbing variables from other registered variables. The instance date is stored in ec2.tagged_instances, the IP information for the other two interfaces are stored in eni_dc and eni_spoke respectively.
Debug example showing extraction of IP:
- debug:
msg: "{{ eni_dc.results|json_query(s_query) }}"
vars:
s_query: "[?interface.attachment.instance_id=='i-x].interface.private_ip_address"
TASK [configure_vsrx : debug] **********************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
"10.24.200.57"
]
}
Debug example attempting to extract the IP using the instance id from the ec2 registered variable:
- debug:
msg: "{{ eni_dc.results|json_query(s_query) }}"
vars:
s_query: "[?interface.attachment.instance_id==inst_id].interface.private_ip_address"
inst_id: "{{ item.id }}"
with_items:
- "{{ ec2.tagged_instances }}"
TASK [configure_vsrx : debug] **********************************************************************
ok: [localhost] => (item={u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-10-24-200-11.us-west-2.compute.internal', u'public_ip': None, u'private_ip': u'10.24.200.11', u'id': u'i-x', u'ebs_optimized': False, u'state': u'running', u'virtualization_type': u'hvm', u'architecture': u'x86_64', u'ramdisk': None, u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-x}}, u'key_name': u'USWest-TransVPC', u'image_id': u'ami-408b1620', u'tenancy': u'default', u'groups': {u'sg-f51e838e': u'secgroup-vsrx-transit'}, u'public_dns_name': u'', u'state_code': 16, u'tags': {u'Name': u'vSRX-hub', u'vsrx': u'vsrx-hub'}, u'placement': u'us-west-2a', u'ami_launch_index': u'0', u'dns_name': u'', u'region': u'us-west-2', u'launch_time': u'2017-05-05T12:16:11.000Z', u'instance_type': u'm4.xlarge', u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen'}) => {
"item": {
<per instance dict>
},
"msg": []
}
I get the ec2.tagged_instance dictionary but it does not seem to populate inst_id. Debugging the variable s_query, I get this:
"msg": "[?interface.attachment.instance_id==inst_id].interface.private_ip_address"
Any suggestions on how I can get the variable to populate with each iteration of task?
EDIT:
I got the instance id to populate in the debug single quoting the variable in the vars statement:
debug:
msg:
- "{{ eni_dc.results|json_query(s_query) }}"
vars:
s_query: "[?interface.attachment.instance_id=='{{ item.id }}'].interface.private_ip_address"
with_items:
- "{{ ec2.tagged_instances }}"
However, I'm now trying to build config based on that:
- name: Build Interface config
template: >
src=vrf.conf.j2
dest={{ build_dir }}/{{ item.id }}-vrf.conf.part
with_items:
- "{{ ec2.tagged_instances }}"
vars:
eni_dc_ip: "{{ eni_dc | json_query(s_query) }}"
eni_spoke_ip: "{{ eni_spoke | json_query(s_query) }}"
s_query: "[?interface.attachment.instance_id=='{{ item.id }}'].interface.private_ip_address"
I'm getting blanks for eni_dc_ip and eni_spoke_ip.
回答1:
I ended up adding another task to set the query variables before the task that was using them.
- name: Setting Instance ID and query to use in looking up instance variables
set_fact:
inst_query:
- inst_id: "{{ item.id }}"
ip_query: "[?interface.attachment.instance_id=='{{ item.id }}'].interface.private_ip_address|[0]"
sec_ip_query: "[?interface.attachment.instance_id=='{{ item.id }}'].interface.private_ip_addresses[?primary_address==false].[private_ip_address]"
with_items:
- "{{ ec2.tagged_instances }}"
- debug:
msg: "{{ eni_dc.results|json_query(item.sec_ip_query) }}"
with_items:
- "{{ inst_query }}"
- name: Build VRF config
template: >
src=vrf.conf.j2
dest={{ build_dir }}/{{ item.inst_id }}-vrf.conf.part
with_items:
- "{{ inst_query }}"
vars:
eni_dc_ip: "{{ eni_dc.results|json_query(item.ip_query) }}"
eni_spoke_ip: "{{ eni_spoke.results|json_query(item.ip_query) }}"
来源:https://stackoverflow.com/questions/43873687/ansible-setting-vars-from-with-items