Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
ansible: omit nonexistant hosts from inventory groups
Browse files Browse the repository at this point in the history
Otherwise, we would get spurious "UNREACHABLE" errors during
provisioning. This could be due wither to mistakes in the lxdock config
file or to a command line filtering of the hosts.
  • Loading branch information
Virgil Dupras committed Jul 26, 2017
1 parent 086073c commit 7351feb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lxdock/provisioners/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ def line(guest):
return '{} ansible_host={} ansible_user=root'.format(guest.container.name, ip)

def fmtgroup(name, hosts):
hosts = [host for host in hosts if host in guestnames]
return '[{}]\n{}'.format(name, '\n'.join(hosts))

all_hosts_lines = '\n'.join(line(guest) for guest in self.guests)
groups = self.options.get('groups', {})
guestnames = {guest.container.name for guest in self.guests}
groups_lines = '\n\n'.join(fmtgroup(key, val) for key, val in groups.items())
return '\n\n'.join([all_hosts_lines, groups_lines])

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/provisioners/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ def test_can_properly_setup_ssh_for_alpine_guests(self):
def test_inventory_contains_groups(self):
c1 = FakeContainer(name='c1')
c2 = FakeContainer(name='c2')
# c3 is deliberately not part of our guests list. This is to test that it doesn't end up
# in the inventory and result in spurious unreachable hosts. These situations can happen
# in two ways: errors in the config file, or guest filtering in the command line ("lxdock
# provision c1, c2" for example).
provisioner = AnsibleProvisioner(
'./',
Host(),
[DebianGuest(c1), DebianGuest(c2)],
{'playbook': 'deploy.yml', 'groups': {'g1': ['c1', 'c2'], 'g2': ['c1']}}
{'playbook': 'deploy.yml', 'groups': {'g1': ['c1', 'c2'], 'g2': ['c1', 'c3']}}
)
inv = provisioner.get_inventory()
# group order is not guaranteed. our tests have to be written with that in mind.
Expand Down

0 comments on commit 7351feb

Please sign in to comment.