循环变量Ansible的多个值 [英] Looping multiple values for a variable Ansible

查看:654
本文介绍了循环变量Ansible的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为以下代码中使用的2个变量设置多个值:

I am trying to set multiple values for 2 variables that I am using in the following code:

- name: Create the subnets
  os_subnet:
     cloud: "{{ item.cloud }}"
     state: present
     validate_certs: no
     no_gateway_ip: yes
     enable_dhcp: yes
     name: "{{ item.subnet }}"
     network_name: "{{ item.network }}"
     cidr: "{{ item.cidr }}"
     allocation_pool_start: "{{ item.allocation_pool_start }}"
     allocation_pool_end: "{{ item.allocation_pool_end }}"
     host_routes:
     - destination: "{{ item.destination | default(omit) }}"
       nexthop: "{{ item.nexthop | default(omit) }}"
  with_items:
  - "{{ subnets }}"
  tags: create_subnets

在我的环境中,某些子网具有不同数量的host_routes或没有.我的变量文件带有一个用于destination + nexthop的变量,现在看起来像:

In my environment, some subnets have a different number of host_routes or none. My variable file with one variable for destination+nexthop looks now like:

--- #
subnets:
- { cloud: tenant1, network: nw, subnet: nw_subnet, cidr: 172.10.10.224/27, allocation_pool_start: 172.10.10.228, allocation_pool_end: 172.10.10.254, destination: 10.10.10.0/24, nexthop: 172.10.114.125 }

这有效.但是,如果我的destination + nexthop值不止一个,该如何进行? 我还没有尝试过复制同一行并仅在最后的destination + nexthop中进行更改,但没有成功.如果我要在同一列表中同一行的另一行添加另一个destination + nexthop,它将只取最后一个值.

And this works. But if I am having more than one value for destination+nexthop, how shall I proceed? I have tried yet to duplicate the same line and change just in the end the destination+nexthop, but didn't worked. If I am trying add in the same list one the same row another destination+nexthop, it will take just the last value.

推荐答案

host_routes定义为子网变量中的列表:

Define host_routes as list in the subnets variable:

subnets: 
- allocation_pool_end: "172.10.10.254"
  allocation_pool_start: "172.10.10.228"
  cidr: 172.10.10.224/27
  cloud: tenant1
  host_routes: 
    - destination: 10.10.10.0/24
      nexthop: "172.10.114.125"
    - destination: YYY.YYY.YYY.YYY/ZZ
      nexthop: XXX.XXX.XXX.XXX
  network: nw
  subnet: nw_subnet
- allocation_pool_end: "172.10.30.254"
  allocation_pool_start: "172.10.30.228"
  cidr: 172.10.30.224/27
  cloud: tenant2
  host_routes: 
    - destination: YYY.YYY.YYY.YYY/ZZ
      nexthop: XXX.XXX.XXX.XXX
  network: nw
  subnet: nw_subnet2

现在您可以在任务中使用它

Now you can use it in your task

- name: Ensure subnets are present
  os_subnet:
     cloud: "{{ item.cloud }}"
     state: present
     validate_certs: no
     no_gateway_ip: yes
     enable_dhcp: yes
     name: "{{ item.subnet }}"
     network_name: "{{ item.network }}"
     cidr: "{{ item.cidr }}"
     allocation_pool_start: "{{ item.allocation_pool_start }}"
     allocation_pool_end: "{{ item.allocation_pool_end }}"
     host_routes: "{{ item.host_routes }}"
  with_items:
  - "{{ subnets }}"
  tags: create_subnets

这篇关于循环变量Ansible的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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