Reputation: 311
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
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 }
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.
Upvotes: 2
Views: 7067
Reputation: 420
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
Upvotes: 3