diff --git a/ci/roles/subnet/tasks/subnet-allocation.yml b/ci/roles/subnet/tasks/subnet-allocation.yml index 773d334c..8cbefe71 100644 --- a/ci/roles/subnet/tasks/subnet-allocation.yml +++ b/ci/roles/subnet/tasks/subnet-allocation.yml @@ -68,6 +68,80 @@ name: "{{ subnet_name }}" state: absent +- name: Create subnet {{ subnet_name }} with multiple allocation pools on network {{ network_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.0.0/24 + gateway_ip: 192.168.0.1 + allocation_pools: + - start: 192.168.0.2 + end: 192.168.0.4 + - start: 192.168.0.10 + end: 192.168.0.12 + +- name: Create subnet {{ subnet_name }} on network {{ network_name }} again + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.0.0/24 + gateway_ip: 192.168.0.1 + allocation_pools: + - start: 192.168.0.2 + end: 192.168.0.4 + - start: 192.168.0.10 + end: 192.168.0.12 + register: idem2 + +- name: Update subnet {{ subnet_name }} allocation pools + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.0.0/24 + gateway_ip: 192.168.0.1 + allocation_pools: + - start: 192.168.0.2 + end: 192.168.0.8 + - start: 192.168.0.10 + end: 192.168.0.16 + +- name: Get Subnet Info + openstack.cloud.subnets_info: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + register: subnet_result + +- name: Verify Subnet Allocation Pools Exist + assert: + that: + - idem2 is not changed + - subnet_result.subnets is defined + - subnet_result.subnets | length == 1 + - subnet_result.subnets[0].allocation_pools is defined + - subnet_result.subnets[0].allocation_pools | length == 2 + +- name: Verify Subnet Allocation Pools + assert: + that: + - (subnet_result.subnets[0].allocation_pools.0.start == '192.168.0.2' and subnet_result.subnets[0].allocation_pools.0.end == '192.168.0.8') or + (subnet_result.subnets[0].allocation_pools.0.start == '192.168.0.10' and subnet_result.subnets[0].allocation_pools.0.end == '192.168.0.16') + - (subnet_result.subnets[0].allocation_pools.1.start == '192.168.0.2' and subnet_result.subnets[0].allocation_pools.1.end == '192.168.0.8') or + (subnet_result.subnets[0].allocation_pools.1.start == '192.168.0.10' and subnet_result.subnets[0].allocation_pools.1.end == '192.168.0.16') + +- name: Delete subnet {{ subnet_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + state: absent + - name: Delete network {{ network_name }} openstack.cloud.network: cloud: "{{ cloud }}" diff --git a/ci/roles/subnet/tasks/subnet-pool.yaml b/ci/roles/subnet/tasks/subnet-pool.yaml index 94135b9e..abb3de0b 100644 --- a/ci/roles/subnet/tasks/subnet-pool.yaml +++ b/ci/roles/subnet/tasks/subnet-pool.yaml @@ -84,6 +84,70 @@ name: "{{ subnet_name }}" state: absent +- name: Create subnet {{ subnet_name }} with multiple allocation pools on network {{ network_name }} from subnet pool {{ subnet_pool_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.42.0/24 # we want specific cidr from subnet pool + ip_version: 4 + subnet_pool: "{{ subnet_pool_name }}" + gateway_ip: 192.168.42.1 + allocation_pools: + - start: 192.168.42.2 + end: 192.168.42.4 + - start: 192.168.42.6 + end: 192.168.42.8 + +- name: Create subnet {{ subnet_name }} on network {{ network_name }} from subnet pool {{ subnet_pool_name }} again + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.42.0/24 + ip_version: 4 + subnet_pool: "{{ subnet_pool_name }}" + gateway_ip: 192.168.42.1 + allocation_pools: + - start: 192.168.42.2 + end: 192.168.42.4 + - start: 192.168.42.6 + end: 192.168.42.8 + register: idem2 + +- name: Get Subnet Info + openstack.cloud.subnets_info: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + register: subnet_result + +- name: Verify Subnet Allocation Pools Exist + assert: + that: + - idem2 is not changed + - subnet_result.subnets is defined + - subnet_result.subnets | length == 1 + - subnet_result.subnets[0].allocation_pools is defined + - subnet_result.subnets[0].allocation_pools | length == 2 + +- name: Verify Subnet Allocation Pools + assert: + that: + - (subnet_result.subnets[0].allocation_pools.0.start == '192.168.42.2' and subnet_result.subnets[0].allocation_pools.0.end == '192.168.42.4') or + (subnet_result.subnets[0].allocation_pools.0.start == '192.168.42.6' and subnet_result.subnets[0].allocation_pools.0.end == '192.168.42.8') + - (subnet_result.subnets[0].allocation_pools.1.start == '192.168.42.2' and subnet_result.subnets[0].allocation_pools.1.end == '192.168.42.4') or + (subnet_result.subnets[0].allocation_pools.1.start == '192.168.42.6' and subnet_result.subnets[0].allocation_pools.1.end == '192.168.42.8') + +- name: Delete subnet {{ subnet_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + state: absent + - name: Delete created subnet pool openstack.cloud.subnet_pool: cloud: "{{ cloud }}" diff --git a/plugins/modules/subnet.py b/plugins/modules/subnet.py index aa0b8b81..d0b8e7a6 100644 --- a/plugins/modules/subnet.py +++ b/plugins/modules/subnet.py @@ -28,6 +28,12 @@ options: - From the subnet pool the last IP that should be assigned to the virtual machines. type: str + allocation_pools: + description: + - List of allocation pools to assign to the subnet. Each element + consists of a 'start' and 'end' value. + type: list + elements: dict cidr: description: - The CIDR representation of the subnet that should be assigned to @@ -299,6 +305,7 @@ class SubnetModule(OpenStackModule): dns_nameservers=dict(type='list', elements='str'), allocation_pool_start=dict(), allocation_pool_end=dict(), + allocation_pools=dict(type='list', elements='dict'), host_routes=dict(type='list', elements='dict'), ipv6_ra_mode=dict(choices=ipv6_mode_choices), ipv6_address_mode=dict(choices=ipv6_mode_choices), @@ -321,7 +328,9 @@ class SubnetModule(OpenStackModule): ('cidr', 'use_default_subnet_pool', 'subnet_pool'), True), ], mutually_exclusive=[ - ('use_default_subnet_pool', 'subnet_pool') + ('use_default_subnet_pool', 'subnet_pool'), + ('allocation_pool_start', 'allocation_pools'), + ('allocation_pool_end', 'allocation_pools') ] ) @@ -367,7 +376,10 @@ class SubnetModule(OpenStackModule): params['project_id'] = project.id if subnet_pool: params['subnet_pool_id'] = subnet_pool.id - params['allocation_pools'] = self._build_pool() + if self.params['allocation_pool_start']: + params['allocation_pools'] = self._build_pool() + else: + params['allocation_pools'] = self.params['allocation_pools'] params = self._add_extra_attrs(params) params = {k: v for k, v in params.items() if v is not None} return params @@ -382,6 +394,10 @@ class SubnetModule(OpenStackModule): params['host_routes'].sort(key=lambda r: sorted(r.items())) subnet['host_routes'].sort(key=lambda r: sorted(r.items())) + if 'allocation_pools' in params: + params['allocation_pools'].sort(key=lambda r: sorted(r.items())) + subnet['allocation_pools'].sort(key=lambda r: sorted(r.items())) + updates = {k: params[k] for k in params if params[k] != subnet[k]} if self.params['disable_gateway_ip'] and subnet.gateway_ip: updates['gateway_ip'] = None