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

8
ansible.cfg Normal file
View file

@ -0,0 +1,8 @@
[defaults]
any_errors_fatal = True
deprecation_warnings = True
display_skipped_hosts = False
host_key_checking = False
nocows = 1
remote_user = root
retry_files_enabled = False

View file

@ -1,6 +1,5 @@
--- ---
ansible_user: deploy ansible_user: root
ansible_become: true
ansible_port: 22 ansible_port: 22
harden_linux_sshd_settings_user: harden_linux_sshd_settings_user:

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'] [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: for host, ip_info, tags, private_ip in self.inventory:
self.response['_meta']['hostvars'][host] = { host_vars = {
'ansible_host': ip_info['address'] if ip_info else private_ip, 'private_ip': private_ip,
'public_ip': ip_info['address'] if ip_info else None, 'ansible_python_interpreter': '/usr/bin/python3'
'private_ip': private_ip
} }
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: if tags:
for tag in tags: for tag in tags:
self._add_to_response( self._add_to_response(
@ -64,6 +70,10 @@ class SCWInventory(object):
host 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): def _add_to_response(self, group, hostname):
''' '''
add a host to a group within the response add a host to a group within the response