Add support for allocation pools to IP allocation role

Only allocate IPs from within the allocation pool range.
This commit is contained in:
Mark Goddard 2017-02-16 10:44:29 +00:00
parent a082bd2446
commit ead380c4d0
6 changed files with 45 additions and 5 deletions

View File

@ -1,10 +1,24 @@
---
- name: Ensure IP addresses are allocated
hosts: controllers
hosts: seed:controllers
gather_facts: no
pre_tasks:
- set_fact:
ip_allocations: "{{ ip_allocations|default([]) + [{'net_name': item, 'cidr': item|net_cidr}] }}"
- name: Initialise the IP allocations fact
set_fact:
ip_allocations: []
- name: Update the IP allocations fact with IP allocation requests
set_fact:
ip_allocations: >
{{
ip_allocations +
[{
'net_name': item,
'cidr': item|net_cidr,
'allocation_pool_start': item|net_allocation_pool_start,
'allocation_pool_end': item|net_allocation_pool_end
}]
}}
with_items: "{{ network_interfaces }}"
roles:
- role: ip-allocation

View File

@ -0,0 +1,13 @@
---
# Path to file in which to store IP allocations.
ip_allocation_filename:
# Name of host to allocate IPs for.
ip_allocation_hostname:
# List of IP allocations. Each item should be a dict with the following items:
# net_name: Name of the network
# cidr: CIDR representation of the IP subnet
# allocation_pool_start: First IP address in the allocation pool (optional)
# allocation_pool_end: Last IP address in the allocation pool (optional)
ip_allocations:

View File

@ -57,6 +57,8 @@ def update_allocation(module, allocations):
net_name = module.params['net_name']
hostname = module.params['hostname']
cidr = module.params['cidr']
allocation_pool_start = module.params['allocation_pool_start']
allocation_pool_end = module.params['allocation_pool_end']
network = netaddr.IPNetwork(cidr)
result = {
'changed': False,
@ -70,8 +72,13 @@ def update_allocation(module, allocations):
(network, ", ".join("%s: %s" % (hn, ip) for hn, ip in invalid_allocations.items())))
if hostname not in net_allocations:
result['changed'] = True
ips = netaddr.IPSet(net_allocations.values())
free_ips = netaddr.IPSet([network]) - ips
allocated_ips = netaddr.IPSet(net_allocations.values())
if allocation_pool_start and allocation_pool_end:
allocation_pool = netaddr.IPRange(allocation_pool_start, allocation_pool_end)
allocation_pool = netaddr.IPSet(allocation_pool)
else:
allocation_pool = netaddr.IPSet([network])
free_ips = allocation_pool - allocated_ips
for free_cidr in free_ips.iter_cidrs():
ip = free_cidr[0]
break
@ -98,6 +105,8 @@ def main():
net_name=dict(required=True, type='str'),
hostname=dict(required=True, type='str'),
cidr=dict(required=True, type='str'),
allocation_pool_start=dict(required=False, type='str'),
allocation_pool_end=dict(required=False, type='str'),
allocation_file=dict(required=True, type='str'),
),
supports_check_mode=True,

View File

@ -6,4 +6,6 @@
hostname: "{{ ip_allocation_hostname }}"
net_name: "{{ item.net_name }}"
cidr: "{{ item.cidr }}"
allocation_pool_start: "{{ item.allocation_pool_start | default(omit) }}"
allocation_pool_end: "{{ item.allocation_pool_end | default(omit) }}"
with_items: "{{ ip_allocations }}"

View File

@ -33,6 +33,7 @@ function run_kolla_ansible {
}
function configure_os {
run_playbook ansible/ip-allocation.yml -l controllers
run_playbook ansible/ssh-known-host.yml -l controllers
run_playbook ansible/disable-selinux.yml -l controllers
run_playbook ansible/network.yml -l controllers

View File

@ -34,6 +34,7 @@ function run_kolla_ansible {
}
function configure_os {
run_playbook ansible/ip-allocation.yml -l seed
run_playbook ansible/ssh-known-host.yml -l seed
run_playbook ansible/disable-selinux.yml -l seed
run_playbook ansible/network.yml -l seed