来自with_items的Ansible设置变量

时间:2017-05-09 15:16:19

标签: python ansible jinja2 jmespath

我试图为每个ec2实例运行一个模板任务,从其他注册变量中获取变量。实例日期存储在ec2.tagged_instances中,其他两个接口的IP信息分别存储在eni_dc和eni_spoke中。

显示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"
    ]
}

尝试使用ec2注册变量中的实例id提取IP的调试示例:

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

我得到了ec2.tagged_instance字典,但它似乎没有填充inst_id。调试变量s_query,我得到了这个:

"msg": "[?interface.attachment.instance_id==inst_id].interface.private_ip_address"

关于如何让变量填充每次迭代任务的任何建议?

修改

我在调试单中填充了实例id,引用了vars语句中的变量:

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

但是,我现在正在尝试基于此构建配置:

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

我正在为eni_dc_ip和eni_spoke_ip获取空白。

1 个答案:

答案 0 :(得分:1)

我最后添加了另一个任务来在使用它们的任务之前设置查询变量。

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