zuul-jobs/roles/write-inventory/library/write_inventory.py
James E. Blair 9e22cfdb0f Remove shebang from all python ansible modules
This commit in Ansible:
9142be2f6c

now allows Python modules to specify their interpreter with the shebang.
We expect our roles to use the discovered python interpreter on remote
nodes, and on the executor, we need them to use the virtualenv.  Removing
the specific shebang accomplishes this under Ansible 6, and has no effect
under older versions of Ansible.

Without this, for example, the log upload roles would not have access to
their cloud libraries.

Also update our ansible/cli check in our module files.  Many of our modules
can be run from the command line for ease of testing, but the check that we
perform to determine if the module is being invoked from the command line
or Ansible fails on Ansible 5.  Update it to a check that should work in
all 4 versions of Ansible that Zuul uses.

Change-Id: I4e6e85156459cca032e6c3e1d8a9284be919ccca
2022-09-15 13:48:10 -07:00

106 lines
3.0 KiB
Python
Executable File

# 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, per_host):
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
if per_host:
if host in per_host:
d.update(per_host[host])
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'),
per_host_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', {})
per_host = p.get('per_host_hostvars', {})
run(dest, hostvars, groups, include, exclude, additional, per_host)
module.exit_json(changed=True)
if __name__ == '__main__':
ansible_main()