Esempio n. 1
0
 def test_construct_error(self):
     """Test the creation of different errors."""
     error = utils._construct_error(TaskQueueManager.RUN_FAILED_HOSTS)
     self.assertEqual(error.message, utils.ANSIBLE_FAILED_HOST_ERR_MSG)
     error = utils._construct_error(TaskQueueManager.RUN_UNREACHABLE_HOSTS)
     self.assertEqual(error.message, utils.ANSIBLE_UNREACHABLE_HOST_ERR_MSG)
     error = utils._construct_error(TaskQueueManager.RUN_FAILED_BREAK_PLAY)
     self.assertEqual(error.message, utils.ANSIBLE_PLAYBOOK_ERR_MSG)
Esempio n. 2
0
def connect(hosts, callback, credential, connection_port, forks=50):
    """Attempt to connect to hosts using the given credential.

    :param hosts: The collection of hosts to test connections
    :param callback: The Ansible callback to accept the results.
    :param credential: The credential used for connections
    :param connection_port: The connection port
    :param forks: number of forks to run with, default of 50
    :returns: list of connected hosts credential tuples and
            list of host that failed connection
    """
    inventory = construct_connect_inventory(hosts, credential, connection_port)
    inventory_file = write_inventory(inventory)
    extra_vars = {}

    playbook = {'name': 'discovery play',
                'hosts': 'all',
                'gather_facts': 'no',
                'tasks': [{'action': {'module': 'raw',
                                      'args': parse_kv('echo "Hello"')}}]}

    _handle_ssh_passphrase(credential)
    result = run_playbook(inventory_file, callback, playbook,
                          extra_vars, forks=forks)
    if (result != TaskQueueManager.RUN_OK and
            result != TaskQueueManager.RUN_UNREACHABLE_HOSTS):
        raise _construct_error(result)