zuul-jobs/roles/write-inventory/library/write_inventory.py
James E. Blair 41d4d6c41d Allow mapping additional hostvars in write-inventory
In OpenDev, we use an inventory file with the public v4 and v6
addresses listed as hostvars.  We would like to use that feature in
testing as well.  In order to do that, we need to mutate the inventory
so that the public_ipv4 address supplied by nodepool is mapped to
the public_v4 address in the inventory.  This additional option allows
the user to configure such mappings.

Change-Id: I48f03cacdf4531c42d33f6e807845d5c2a1da1d5
2020-05-18 12:34:34 -07:00

103 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright 2018 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
from ansible.module_utils.basic import AnsibleModule
# The list of variables we might include
VARS = [
'ansible_python_interpreter',
'ansible_connection',
'ansible_host',
'ansible_port',
'ansible_user'
]
def run(dest, hostvars, groups, include, exclude, additional):
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': {
'children': children,
'hosts': out_all
}
}
for host, hvars in hostvars.items():
d = {}
for v in VARS:
if v not in hvars:
continue
if include is not None:
if v not in include:
continue
if exclude is not None:
if v in exclude:
continue
d[v] = hvars[v]
out_all[host] = d
if additional:
for new_var_name, old_var_name in additional.items():
old_var = hvars
for part in old_var_name.split('.'):
old_var = old_var.get(part)
if old_var is None:
break
else:
d[new_var_name] = old_var
with open(dest, 'w') as f:
f.write(json.dumps(out))
def ansible_main():
module = AnsibleModule(
argument_spec=dict(
dest=dict(required=True, type='path'),
hostvars=dict(required=True, type='raw'),
groups=dict(required=True, type='raw'),
include_hostvars=dict(type='list'),
exclude_hostvars=dict(type='list'),
additional_hostvars=dict(type='raw'),
)
)
p = module.params
dest = p.get('dest')
hostvars = p.get('hostvars')
groups = p.get('groups')
include = p.get('include_hostvars')
exclude = p.get('exclude_hostvars')
additional = p.get('additional_hostvars', {})
run(dest, hostvars, groups, include, exclude, additional)
module.exit_json(changed=True)
if __name__ == '__main__':
ansible_main()