def get_ansible_host(config,
                     inventory,
                     host,
                     ssh_config=None,
                     ssh_identity_file=None):
    if is_empty_inventory(inventory):
        if host == 'localhost':
            return testinfra.get_host('local://')
        return None
    hostvars = inventory['_meta'].get('hostvars', {}).get(host, {})
    connection = hostvars.get('ansible_connection', 'ssh')
    if connection not in (
            'smart',
            'ssh',
            'paramiko_ssh',
            'local',
            'docker',
            'lxc',
            'lxd',
    ):
        # unhandled connection type, must use force_ansible=True
        return None
    connection = {
        'lxd': 'lxc',
        'paramiko_ssh': 'paramiko',
        'smart': 'ssh',
    }.get(connection, connection)
    testinfra_host = hostvars.get('ansible_host', host)
    user = hostvars.get('ansible_user')
    port = hostvars.get('ansible_port')
    kwargs = {}
    if hostvars.get('ansible_become', False):
        kwargs['sudo'] = True
    kwargs['sudo_user'] = hostvars.get('ansible_become_user')
    if ssh_config is not None:
        kwargs['ssh_config'] = ssh_config
    if ssh_identity_file is not None:
        kwargs['ssh_identity_file'] = ssh_identity_file

    # Support both keys as advertised by Ansible
    if 'ansible_ssh_private_key_file' in hostvars:
        kwargs['ssh_identity_file'] = hostvars['ansible_ssh_private_key_file']
    elif 'ansible_private_key_file' in hostvars:
        kwargs['ssh_identity_file'] = hostvars['ansible_private_key_file']
    kwargs['ssh_extra_args'] = '{} {}'.format(
        hostvars.get('ansible_ssh_common_args', ''),
        hostvars.get('ansible_ssh_extra_args', '')).strip()

    spec = '{}://'.format(connection)
    if user:
        spec += '{}@'.format(user)
    if check_ip_address(testinfra_host) == 6:
        spec += '[' + testinfra_host + ']'
    else:
        spec += testinfra_host
    if port:
        spec += ':{}'.format(port)
    return testinfra.get_host(spec, **kwargs)
Esempio n. 2
0
def get_ansible_host(config,
                     inventory,
                     host,
                     ssh_config=None,
                     ssh_identity_file=None):
    if is_empty_inventory(inventory):
        return testinfra.get_host('local://')
    hostvars = inventory['_meta'].get('hostvars', {}).get(host, {})
    connection = hostvars.get('ansible_connection', 'ssh')
    if connection not in ('ssh', 'local', 'docker', 'lxc', 'lxd'):
        raise NotImplementedError(
            'unhandled ansible_connection {}'.format(connection))
    if connection == 'lxd':
        connection = 'lxc'
    if connection == 'ssh':
        connection = 'paramiko'
    testinfra_host = hostvars.get('ansible_host', host)
    user = hostvars.get('ansible_user')
    port = hostvars.get('ansible_port')
    kwargs = {}
    if hostvars.get('ansible_become', False):
        kwargs['sudo'] = True
    kwargs['sudo_user'] = hostvars.get('ansible_become_user')
    if ssh_config is not None:
        kwargs['ssh_config'] = ssh_config
    if ssh_identity_file is not None:
        kwargs['ssh_identity_file'] = ssh_identity_file

    # Support both keys as advertised by Ansible
    if 'ansible_ssh_private_key_file' in hostvars:
        kwargs['ssh_identity_file'] = hostvars['ansible_ssh_private_key_file']
    elif 'ansible_private_key_file' in hostvars:
        kwargs['ssh_identity_file'] = hostvars['ansible_private_key_file']

    spec = '{}://'.format(connection)
    if user:
        spec += '{}@'.format(user)
    if check_ip_address(testinfra_host) == 6:
        spec += '[' + testinfra_host + ']'
    else:
        spec += testinfra_host
    if port:
        spec += ':{}'.format(port)
    return testinfra.get_host(spec, **kwargs)