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

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.

3
  • "In my example, some subnets have a different number of host_routes or none." err, no. In your example there is only one destination and only one nexthop. How are other SO users supposed to guess what your data looks like? Commented Aug 15, 2018 at 9:26
  • @techraf: I have edited the question to make it a little bit clearer. Commented Aug 15, 2018 at 12:27
  • I did not ask you to change text, but to show your data structure. Commented Aug 15, 2018 at 12:28

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it really works. Is any way to write it in a more condensed way - in one line and between the curly brackets? e.g. - { 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 }
You are welcome. This is yaml syntax, { 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, host_routes: [{destination: 10.10.10.0/24, nexthop: 172.10.114.125},{destination: 10.10.50.0/24, nexthop: X.Y.Z.Z}] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.