来自 with_items 的 Ansible 设置变量 [英] Ansible setting vars from with_items

查看:30
本文介绍了来自 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?

我得到了实例 id 来填充调试单引号 vars 语句中的变量:

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 设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆