def construct_scan_inventory(hosts, connection_port, concurrency_count): """Create a dictionary inventory for Ansible to execute with. :param hosts: The collection of hosts/credential tuples :param connection_port: The connection port :param concurrency_count: The number of concurrent scans :returns: A dictionary of the ansible invetory """ concurreny_groups = list([ hosts[i:i + concurrency_count] for i in range(0, len(hosts), concurrency_count) ]) vars_dict = _construct_vars(connection_port) children = {} inventory = {'all': {'children': children, 'vars': vars_dict}} i = 0 group_names = [] for concurreny_group in concurreny_groups: hosts_dict = {} for host in concurreny_group: host_vars = _credential_vars(host[1]) host_vars['ansible_host'] = host[0] hosts_dict[host[0]] = host_vars group_name = 'group_{}'.format(i) i += 1 group_names.append(group_name) children[group_name] = {'hosts': hosts_dict} return group_names, inventory
def construct_connect_inventory(hosts, credential, connection_port, exclude_hosts=None): """Create a dictionary inventory for Ansible to execute with. :param hosts: The collection of hosts to test connections :param credential: The credential used for connections :param connection_port: The connection port :param exclude_hosts: Optional. Hosts to exclude test connections :returns: A dictionary of the ansible inventory """ inventory = None hosts_dict = {} if exclude_hosts is not None: hosts = list(set(hosts) - set(exclude_hosts)) for host in hosts: hosts_dict[host] = None vars_dict = _construct_vars(connection_port, credential) inventory = {'all': {'hosts': hosts_dict, 'vars': vars_dict}} return inventory
def test_construct_vars(self): """Test constructing ansible vars dictionary.""" hc_serializer = CredentialSerializer(self.cred) cred = hc_serializer.data vars_dict = _construct_vars(22, cred) expected = {'ansible_become_pass': '******', 'ansible_port': 22, 'ansible_ssh_pass': '******', 'ansible_ssh_private_key_file': 'keyfile', 'ansible_user': '******', 'ansible_become_method': 'sudo', 'ansible_become_user': '******'} self.assertEqual(vars_dict, expected)
def _construct_connect_inventory(hosts, credential, connection_port, concurrency_count, exclude_hosts=None, ssh_executable=None, ssh_args=None): """Create a dictionary inventory for Ansible to execute with. :param hosts: The collection of hosts to test connections :param credential: The credential used for connections :param connection_port: The connection port :param concurrency_count: The number of concurrent scans :param exclude_hosts: Optional. Hosts to exclude test connections :param ssh_executable: the ssh executable to use, or None for 'ssh' :param ssh_args: a list of extra ssh arguments, or None :returns: A dictionary of the ansible inventory """ if exclude_hosts is not None: hosts = list(set(hosts) - set(exclude_hosts)) concurreny_groups = \ list( [hosts[i:i + concurrency_count] for i in range(0, len(hosts), concurrency_count)]) vars_dict = _construct_vars(connection_port, credential) children = {} inventory = {'all': {'children': children, 'vars': vars_dict}} i = 0 group_names = [] for concurreny_group in concurreny_groups: hosts_dict = {} for host in concurreny_group: host_vars = {} host_vars['ansible_host'] = host if ssh_executable: host_vars['ansible_ssh_executable'] = ssh_executable if ssh_args: host_vars['ansible_ssh_common_args'] = ' '.join(ssh_args) hosts_dict[host] = host_vars group_name = 'group_{}'.format(i) i += 1 group_names.append(group_name) children[group_name] = {'hosts': hosts_dict} return group_names, inventory
def construct_connect_inventory(hosts, credential, connection_port): """Create a dictionary inventory for Ansible to execute with. :param hosts: The collection of hosts to test connections :param credential: The credential used for connections :param connection_port: The connection port :returns: A dictionary of the ansible invetory """ inventory = None hosts_dict = {} for host in hosts: hosts_dict[host] = None vars_dict = _construct_vars(connection_port, credential) inventory = {'all': {'hosts': hosts_dict, 'vars': vars_dict}} return inventory
def _construct_scan_inventory(hosts, connection_port, concurrency_count, ssh_executable=None, ssh_args=None): """Create a dictionary inventory for Ansible to execute with. :param hosts: The collection of hosts/credential tuples :param connection_port: The connection port :param concurrency_count: The number of concurrent scans :param ssh_executable: the ssh executable to use, or None for 'ssh' :param ssh_args: a list of extra ssh arguments, or None :returns: A list of group names and a dict of the ansible inventory """ concurreny_groups = list([ hosts[i:i + concurrency_count] for i in range(0, len(hosts), concurrency_count) ]) vars_dict = _construct_vars(connection_port) children = {} inventory = {'all': {'children': children, 'vars': vars_dict}} i = 0 group_names = [] for concurreny_group in concurreny_groups: hosts_dict = {} for host in concurreny_group: host_vars = _credential_vars(host[1]) host_vars['ansible_host'] = host[0] if ssh_executable: host_vars['ansible_ssh_executable'] = ssh_executable if ssh_args: host_vars['ansible_ssh_common_args'] = ' '.join(ssh_args) hosts_dict[host[0]] = host_vars group_name = 'group_{}'.format(i) i += 1 group_names.append(group_name) children[group_name] = {'hosts': hosts_dict} return group_names, inventory
def test_construct_vars(self, decrypt_data): """Test constructing ansible vars dictionary.""" decrypt_data.side_effect = lambda x: x vars_dict = utils._construct_vars( 22, { 'name': 'cred1', 'username': '******', 'password': '******', 'become_password': '******', 'ssh_keyfile': 'keyfile', 'become_method': 'sudo', 'become_user': '******' }) expected = { 'ansible_become_pass': '******', 'ansible_port': 22, 'ansible_ssh_pass': '******', 'ansible_ssh_private_key_file': 'keyfile', 'ansible_user': '******', 'ansible_become_user': '******', 'ansible_become_method': 'sudo' } self.assertEqual(vars_dict, expected)