update inventory builder for public and private IP per node (#4323)

pull/4339/head
tikitavi 2019-03-07 18:30:12 +03:00 committed by Matthew Mosesohn
parent 33024731e4
commit 44de04be89
2 changed files with 47 additions and 6 deletions

View File

@ -17,6 +17,8 @@
# #
# Advanced usage: # Advanced usage:
# Add another host after initial creation: inventory.py 10.10.1.5 # Add another host after initial creation: inventory.py 10.10.1.5
# Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
# Add hosts with different ip and access ip: inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.10.3
# Delete a host: inventory.py -10.10.1.3 # Delete a host: inventory.py -10.10.1.3
# Delete a host by id: inventory.py -node1 # Delete a host by id: inventory.py -node1
# #
@ -169,18 +171,23 @@ class KubesprayInventory(object):
self.debug("Marked {0} for deletion.".format(realhost)) self.debug("Marked {0} for deletion.".format(realhost))
self.delete_host_by_ip(all_hosts, realhost) self.delete_host_by_ip(all_hosts, realhost)
elif host[0].isdigit(): elif host[0].isdigit():
if ',' in host:
ip, access_ip = host.split(',')
else:
ip = host
access_ip = host
if self.exists_hostname(all_hosts, host): if self.exists_hostname(all_hosts, host):
self.debug("Skipping existing host {0}.".format(host)) self.debug("Skipping existing host {0}.".format(host))
continue continue
elif self.exists_ip(all_hosts, host): elif self.exists_ip(all_hosts, ip):
self.debug("Skipping existing host {0}.".format(host)) self.debug("Skipping existing host {0}.".format(ip))
continue continue
next_host = "{0}{1}".format(HOST_PREFIX, next_host_id) next_host = "{0}{1}".format(HOST_PREFIX, next_host_id)
next_host_id += 1 next_host_id += 1
all_hosts[next_host] = {'ansible_host': host, all_hosts[next_host] = {'ansible_host': access_ip,
'ip': host, 'ip': ip,
'access_ip': host} 'access_ip': access_ip}
elif host[0].isalpha(): elif host[0].isalpha():
raise Exception("Adding hosts by hostname is not supported.") raise Exception("Adding hosts by hostname is not supported.")
@ -235,7 +242,7 @@ class KubesprayInventory(object):
all_hosts = self.yaml_config['all']['hosts'].copy() all_hosts = self.yaml_config['all']['hosts'].copy()
for host in all_hosts.keys(): for host in all_hosts.keys():
if host not in hostnames and host not in protected_names: if host not in hostnames and host not in protected_names:
self.debug("Host {0} removed from role all") self.debug("Host {0} removed from role all".format(host))
del self.yaml_config['all']['hosts'][host] del self.yaml_config['all']['hosts'][host]
def add_host_to_group(self, group, host, opts=""): def add_host_to_group(self, group, host, opts=""):
@ -346,6 +353,8 @@ print_ips - Write a space-delimited list of IPs from "all" group
Advanced usage: Advanced usage:
Add another host after initial creation: inventory.py 10.10.1.5 Add another host after initial creation: inventory.py 10.10.1.5
Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
Add hosts with different ip and access ip: inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.10.3
Delete a host: inventory.py -10.10.1.3 Delete a host: inventory.py -10.10.1.3
Delete a host by id: inventory.py -node1 Delete a host by id: inventory.py -node1

View File

@ -311,3 +311,35 @@ class TestInventory(unittest.TestCase):
host_range = ['10.90.0.4-a.9b.c.e'] host_range = ['10.90.0.4-a.9b.c.e']
self.assertRaisesRegexp(Exception, "Range of ip_addresses isn't valid", self.assertRaisesRegexp(Exception, "Range of ip_addresses isn't valid",
self.inv.range2ips, host_range) self.inv.range2ips, host_range)
def test_build_hostnames_different_ips_add_one(self):
changed_hosts = ['10.90.0.2,192.168.0.2']
expected = OrderedDict([('node1',
{'ansible_host': '192.168.0.2',
'ip': '10.90.0.2',
'access_ip': '192.168.0.2'})])
result = self.inv.build_hostnames(changed_hosts)
self.assertEqual(expected, result)
def test_build_hostnames_different_ips_add_duplicate(self):
changed_hosts = ['10.90.0.2,192.168.0.2']
expected = OrderedDict([('node1',
{'ansible_host': '192.168.0.2',
'ip': '10.90.0.2',
'access_ip': '192.168.0.2'})])
self.inv.yaml_config['all']['hosts'] = expected
result = self.inv.build_hostnames(changed_hosts)
self.assertEqual(expected, result)
def test_build_hostnames_different_ips_add_two(self):
changed_hosts = ['10.90.0.2,192.168.0.2', '10.90.0.3,192.168.0.3']
expected = OrderedDict([
('node1', {'ansible_host': '192.168.0.2',
'ip': '10.90.0.2',
'access_ip': '192.168.0.2'}),
('node2', {'ansible_host': '192.168.0.3',
'ip': '10.90.0.3',
'access_ip': '192.168.0.3'})])
self.inv.yaml_config['all']['hosts'] = OrderedDict()
result = self.inv.build_hostnames(changed_hosts)
self.assertEqual(expected, result)