items中的Ansible设置变量

items中的Ansible设置变量

本文介绍了with_items中的Ansible设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

显示IP提取的调试示例:

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

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

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

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

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?

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

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"

我正在为eni_dc_ip和eni_spoke_ip留空.

I'm getting blanks for eni_dc_ip and eni_spoke_ip.

推荐答案

我最终添加了另一个任务,以在使用查询变量的任务之前设置查询变量.

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

这篇关于with_items中的Ansible设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:32