def run_custom_command(host, command, run_pattern=None, skeleton=False): """ Runs a custom command on a host and returns the output in a dictionary as generated by ansible.runner.Runner :param host: Hostname of the server to run on :type host: string :param command: command to execute via the shell module from ansible :type command: string :param run_pattern: The pattern of hosts reachable via ansible :type run_pattern: list :param skeleton: Whether we are generating a skeleton. If True, don't actually run anything. :type skeleton: bool :return: Datastructure generated by ansible.runner.Runner :rtype: dict """ if not run_pattern: run_pattern = [] inventory = ansible.inventory.Inventory() run_host_inventory = [i.name for i in inventory.get_hosts(run_pattern)] custom_inventory = [i.name for i in inventory.get_hosts(host)] new_pattern = [] for run_host in custom_inventory: if run_host in run_host_inventory: new_pattern.append(run_host) if not new_pattern: return None host = ':'.join(new_pattern) if skeleton: ret_dict = {} for h in new_pattern: try: ret_dict['contacted'][h] = { 'cmd': command, 'stdout': '', } except KeyError: ret_dict['contacted'] = { h: { 'cmd': command, 'stdout': '', } } return ret_dict runner = ansible.runner.Runner( module_name="shell", module_args=command, pattern=host, ) return runner.run()
def get_hosts(cls, host, **kwargs): AnsibleBackend._check_ansible() ansible_inventory = kwargs.get("ansible_inventory") if ansible_inventory is not None: inventory = ansible.inventory.Inventory(ansible_inventory) else: inventory = ansible.inventory.Inventory() return [e.name for e in inventory.get_hosts(pattern=host or "all")]
def get_hosts(cls, host, **kwargs): AnsibleBackend._check_ansible() ansible_inventory = kwargs.get("ansible_inventory") if HAS_ANSIBLE_1: if ansible_inventory is not None: inventory = ansible.inventory.Inventory(ansible_inventory) else: inventory = ansible.inventory.Inventory() else: if ansible_inventory is not None: variable_manager = VariableManager() loader = DataLoader() inventory = ansible.inventory.Inventory(loader,variable_manager,ansible_inventory) else: inventory = ansible.inventory.Inventory() return [e.name for e in inventory.get_hosts(pattern=host or "all")]
def get_hosts(cls, host, **kwargs): AnsibleBackend._check_ansible() ansible_inventory = kwargs.get("ansible_inventory") if HAS_ANSIBLE_1: if ansible_inventory is not None: inventory = ansible.inventory.Inventory(ansible_inventory) else: inventory = ansible.inventory.Inventory() else: if ansible_inventory is not None: variable_manager = VariableManager() loader = DataLoader() inventory = ansible.inventory.Inventory( loader, variable_manager, ansible_inventory) else: inventory = ansible.inventory.Inventory() return [e.name for e in inventory.get_hosts(pattern=host or "all")]