diff --git a/contrib/inventory_builder/inventory.py b/contrib/inventory_builder/inventory.py index 6ab805948..118a986dd 100644 --- a/contrib/inventory_builder/inventory.py +++ b/contrib/inventory_builder/inventory.py @@ -17,6 +17,8 @@ # # Advanced usage: # 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 by id: inventory.py -node1 # @@ -169,18 +171,23 @@ class KubesprayInventory(object): self.debug("Marked {0} for deletion.".format(realhost)) self.delete_host_by_ip(all_hosts, realhost) 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): self.debug("Skipping existing host {0}.".format(host)) continue - elif self.exists_ip(all_hosts, host): - self.debug("Skipping existing host {0}.".format(host)) + elif self.exists_ip(all_hosts, ip): + self.debug("Skipping existing host {0}.".format(ip)) continue next_host = "{0}{1}".format(HOST_PREFIX, next_host_id) next_host_id += 1 - all_hosts[next_host] = {'ansible_host': host, - 'ip': host, - 'access_ip': host} + all_hosts[next_host] = {'ansible_host': access_ip, + 'ip': ip, + 'access_ip': access_ip} elif host[0].isalpha(): raise Exception("Adding hosts by hostname is not supported.") @@ -235,7 +242,7 @@ class KubesprayInventory(object): all_hosts = self.yaml_config['all']['hosts'].copy() for host in all_hosts.keys(): 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] 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: 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 by id: inventory.py -node1 diff --git a/contrib/inventory_builder/tests/test_inventory.py b/contrib/inventory_builder/tests/test_inventory.py index 6a5f1448a..c6d8d96f4 100644 --- a/contrib/inventory_builder/tests/test_inventory.py +++ b/contrib/inventory_builder/tests/test_inventory.py @@ -311,3 +311,35 @@ class TestInventory(unittest.TestCase): host_range = ['10.90.0.4-a.9b.c.e'] self.assertRaisesRegexp(Exception, "Range of ip_addresses isn't valid", 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)