Add hostvars to dynamic inventory to specify ssh ProxyCommand

This commit is contained in:
Paul-Henri Froidmont 2018-09-16 02:26:14 +02:00
parent 9f8e3dc0c7
commit 3bcd961c81
3 changed files with 23 additions and 6 deletions

View file

@ -52,11 +52,17 @@ class SCWInventory(object):
[i['name'], i['public_ip'], i['tags'], i['private_ip']] for i in result_par1['servers'] + result_ams1['servers']
]
for host, ip_info, tags, private_ip in self.inventory:
self.response['_meta']['hostvars'][host] = {
'ansible_host': ip_info['address'] if ip_info else private_ip,
'public_ip': ip_info['address'] if ip_info else None,
'private_ip': private_ip
host_vars = {
'private_ip': private_ip,
'ansible_python_interpreter': '/usr/bin/python3'
}
if ip_info:
host_vars['ansible_host'] = ip_info['address']
host_vars['public_ip'] = ip_info['address']
else:
host_vars['ansible_host'] = private_ip
self.response['_meta']['hostvars'][host] = host_vars
if tags:
for tag in tags:
self._add_to_response(
@ -64,6 +70,10 @@ class SCWInventory(object):
host
)
for host, variables in self.response['_meta']['hostvars'].items():
if host != 'proxy1':
variables['ansible_ssh_common_args'] = '-o ProxyCommand="ssh -W %h:%p -q root@' + self.response['_meta']['hostvars']['proxy1']['public_ip'] + '"'
def _add_to_response(self, group, hostname):
'''
add a host to a group within the response