write-inventory: Add groups to created inventory

Add the groups from the "groups" ansible variable to the "children" of
the created inventory.

Change-Id: I6c797ff939f8198d5c0928558df8ed04bec14efd
This commit is contained in:
Ian Wienand 2018-08-28 16:40:12 +10:00
parent 2cb237acef
commit ab0508600a
3 changed files with 55 additions and 6 deletions

View File

@ -56,6 +56,14 @@ xenial:
region: GRA1 region: GRA1
""") """)
GROUPS_INPUT = yaml.safe_load("""
all: []
ungrouped: []
puppet:
- bionic
- xenial
""")
class TestWriteInventory(testtools.TestCase): class TestWriteInventory(testtools.TestCase):
def assertOutput(self, dest, ref): def assertOutput(self, dest, ref):
@ -67,10 +75,18 @@ class TestWriteInventory(testtools.TestCase):
'''Test passing all variables''' '''Test passing all variables'''
dest = self.useFixture(fixtures.TempDir()).path dest = self.useFixture(fixtures.TempDir()).path
dest = os.path.join(dest, 'out.yaml') dest = os.path.join(dest, 'out.yaml')
run(dest, INPUT, None, None) run(dest, INPUT, GROUPS_INPUT, None, None)
self.assertOutput(dest, { self.assertOutput(dest, {
'all': { 'all': {
'children': {
'puppet': {
'hosts': {
'bionic': None,
'xenial': None,
},
},
},
'hosts': { 'hosts': {
'bionic': { 'bionic': {
"ansible_connection": "ssh", "ansible_connection": "ssh",
@ -92,10 +108,18 @@ class TestWriteInventory(testtools.TestCase):
'''Test incuding vars''' '''Test incuding vars'''
dest = self.useFixture(fixtures.TempDir()).path dest = self.useFixture(fixtures.TempDir()).path
dest = os.path.join(dest, 'out.yaml') dest = os.path.join(dest, 'out.yaml')
run(dest, INPUT, ['ansible_host'], None) run(dest, INPUT, GROUPS_INPUT, ['ansible_host'], None)
self.assertOutput(dest, { self.assertOutput(dest, {
'all': { 'all': {
'children': {
'puppet': {
'hosts': {
'bionic': None,
'xenial': None,
},
},
},
'hosts': { 'hosts': {
'bionic': { 'bionic': {
"ansible_host": "104.130.217.77", "ansible_host": "104.130.217.77",
@ -111,10 +135,18 @@ class TestWriteInventory(testtools.TestCase):
'''Test passing all variables''' '''Test passing all variables'''
dest = self.useFixture(fixtures.TempDir()).path dest = self.useFixture(fixtures.TempDir()).path
dest = os.path.join(dest, 'out.yaml') dest = os.path.join(dest, 'out.yaml')
run(dest, INPUT, None, ['ansible_user']) run(dest, INPUT, GROUPS_INPUT, None, ['ansible_user'])
self.assertOutput(dest, { self.assertOutput(dest, {
'all': { 'all': {
'children': {
'puppet': {
'hosts': {
'bionic': None,
'xenial': None,
},
},
},
'hosts': { 'hosts': {
'bionic': { 'bionic': {
"ansible_connection": "ssh", "ansible_connection": "ssh",

View File

@ -28,9 +28,23 @@ VARS = [
] ]
def run(dest, hostvars, include, exclude): def run(dest, hostvars, groups, include, exclude):
children = {}
for group, hostnames in groups.items():
if group == 'all' or group == 'ungrouped':
continue
children[group] = {}
children[group]['hosts'] = {}
for host in hostnames:
children[group]['hosts'][host] = None
out_all = {} out_all = {}
out = {'all': {'hosts': out_all}} out = {
'all': {
'children': children,
'hosts': out_all
}
}
for host, hvars in hostvars.items(): for host, hvars in hostvars.items():
d = {} d = {}
for v in VARS: for v in VARS:
@ -54,6 +68,7 @@ def ansible_main():
argument_spec=dict( argument_spec=dict(
dest=dict(required=True, type='path'), dest=dict(required=True, type='path'),
hostvars=dict(required=True, type='raw'), hostvars=dict(required=True, type='raw'),
groups=dict(required=True, type='raw'),
include_hostvars=dict(type='list'), include_hostvars=dict(type='list'),
exclude_hostvars=dict(type='list'), exclude_hostvars=dict(type='list'),
) )
@ -63,10 +78,11 @@ def ansible_main():
dest = p.get('dest') dest = p.get('dest')
hostvars = p.get('hostvars') hostvars = p.get('hostvars')
groups = p.get('groups')
include = p.get('include_hostvars') include = p.get('include_hostvars')
exclude = p.get('exclude_hostvars') exclude = p.get('exclude_hostvars')
run(dest, hostvars, include, exclude) run(dest, hostvars, groups, include, exclude)
module.exit_json(changed=True) module.exit_json(changed=True)

View File

@ -2,5 +2,6 @@
write_inventory: write_inventory:
dest: "{{ write_inventory_dest }}" dest: "{{ write_inventory_dest }}"
hostvars: "{{ hostvars }}" hostvars: "{{ hostvars }}"
groups: "{{ groups }}"
include_hostvars: "{{ write_inventory_include_hostvars | default(omit) }}" include_hostvars: "{{ write_inventory_include_hostvars | default(omit) }}"
exclude_hostvars: "{{ write_inventory_exclude_hostvars | default(omit) }}" exclude_hostvars: "{{ write_inventory_exclude_hostvars | default(omit) }}"