Skip to content

Commit

Permalink
Avoid crash when server_ip is not there. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored Apr 16, 2022
1 parent 99b31b9 commit 78d0e7b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/47-inventory-crash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "robot inventory plugin - do not crash if a server neither has name or primary IP set. Instead, fall back to using the server's number as the name. This can happen if unnamed rack reservations show up in your server list (https://github.com/ansible-collections/community.hrobot/issues/40, https://github.com/ansible-collections/community.hrobot/pull/47)."
48 changes: 26 additions & 22 deletions plugins/inventory/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,33 @@ def populate(self, servers):
server_lists = []
for server in servers:
s = server['server']
server_name = s.get('server_name') or s['server_ip']
server_name = s.get('server_name') or s.get('server_ip') or str(s['server_number'])
matched = self.filter(s, filters)
if matched:
if server_name not in server_lists:
self.inventory.add_host(server_name)
server_lists.append(server_name)
self.inventory.set_variable(server_name, 'ansible_host', s['server_ip'])
for hostvar, hostval in s.items():
self.inventory.set_variable(server_name, "{0}_{1}".format('hrobot', hostvar), hostval)

# Composed variables
server_vars = self.inventory.get_host(server_name).get_vars()
self._set_composite_vars(self.get_option('compose'), server_vars, server_name, strict=strict)

# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(self.get_option('groups'), server, server_name, strict=strict)

# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), server, server_name, strict=strict)
else:
display.warning('Two of your Hetzner servers use the same server name ({0}). '
'Please make sure that your server names are unique. '
'Only the first server named {0} will be included in the inventory.'.format(server_name))
if not matched:
continue

if server_name in server_lists:
display.warning('Two of your Hetzner servers use the same server name ({0}). '
'Please make sure that your server names are unique. '
'Only the first server named {0} will be included in the inventory.'.format(server_name))
continue

self.inventory.add_host(server_name)
server_lists.append(server_name)
if 'server_ip' in s:
self.inventory.set_variable(server_name, 'ansible_host', s['server_ip'])
for hostvar, hostval in s.items():
self.inventory.set_variable(server_name, "{0}_{1}".format('hrobot', hostvar), hostval)

# Composed variables
server_vars = self.inventory.get_host(server_name).get_vars()
self._set_composite_vars(self.get_option('compose'), server_vars, server_name, strict=strict)

# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(self.get_option('groups'), server, server_name, strict=strict)

# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), server, server_name, strict=strict)

def filter(self, server, filters):
matched = True
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/plugins/inventory/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def test_populate(inventory, mocker):
'server_name': 'test-server',
},
},
{
'server': {
'server_number': 5,
},
},
])
.expect_url('{0}/server'.format(BASE_URL)),
])
Expand All @@ -96,16 +101,21 @@ def test_populate(inventory, mocker):

host_1 = inventory.inventory.get_host('1.2.3.4')
host_2 = inventory.inventory.get_host('test-server')
host_3 = inventory.inventory.get_host('5')

host_1_vars = host_1.get_vars()
host_2_vars = host_2.get_vars()
host_3_vars = host_3.get_vars()

assert host_1_vars['ansible_host'] == '1.2.3.4'
assert host_1_vars['hrobot_server_ip'] == '1.2.3.4'
assert 'hrobot_server_name' not in host_1_vars
assert host_2_vars['ansible_host'] == '1.2.3.5'
assert host_2_vars['hrobot_server_ip'] == '1.2.3.5'
assert host_2_vars['hrobot_server_name'] == 'test-server'
assert 'ansible_host' not in host_3_vars
assert 'hrobot_server_ip' not in host_3_vars
assert 'hrobot_server_name' not in host_3_vars


def test_inventory_file_simple(mocker):
Expand Down

0 comments on commit 78d0e7b

Please sign in to comment.