def create_inventory(host_dict: dict, variable_manager, loader, ansible_password): """ Returns an inventory object for playbook execution. Args: host_list (list): list of IP addresses or hostnames variable_manager (ansible.vars.manager.VariableManager): Ansible variable manager for PlaybookExecutor loader (ansible.parsing.dataloader.DataLoader): Ansible data loader for PlaybookExecutor Returns: ansible.inventory.manager.InventoryManager for PlaybookExecutor """ # create inventory and pass to var manager data = InventoryData() for key, hosts in host_dict.items(): data.add_group(key) if key == "windows": data.set_variable(key, 'ansible_connection', 'winrm') data.set_variable(key, 'ansible_winrm_transport', 'ntlm') data.set_variable(key, 'ansible_winrm_scheme', 'http') data.set_variable(key, 'ansible_winrm_operation_timeout_sec', 40) data.set_variable(key, 'ansible_winrm_read_timeout_sec', 50) data.set_variable(key, 'ansible_winrm_server_cert_validation', 'ignore') else: data.set_variable(key, 'ansible_connection', 'ssh') for host in hosts: data.add_host(host['host_ip'], group=key, port=host['connection_port']) data.set_variable(host['host_ip'], 'user', host['username']) data.set_variable(host['host_ip'], 'ansible_password', host['password']) data.set_variable(host['host_ip'], 'ansible_user', host['username']) data.set_variable(host['host_ip'], 'ansible_become_password', host['password']) data.reconcile_inventory() inventory = InventoryManager(loader=loader) inventory._inventory = data variable_manager.set_inventory(inventory) return inventory
class AnsibleInventoryManager(InventoryManager): ''' Overwrite Creates and manages inventory ''' def __init__(self, loader, sources=None): # base objects self._loader = loader self._inventory = InventoryData() # a list of host(names) to contain current inquiries to self._restriction = None self._subset = None # caches self._hosts_patterns_cache = {} # resolved full patterns self._pattern_cache = {} # resolved individual patterns self._inventory_plugins = [] # for generating inventory # the inventory dirs, files, script paths or lists of hosts if sources is None: self._sources = [] elif isinstance(sources, string_types): self._sources = [sources] else: self._sources = sources # get to work! self.parse_source(sources) def parse_source(self, source, cache=False): ''' giữ nguyên và không parse haha ''' if len(source) > 0: # lấy list instance inventory input for inven in source: # add group for group in inven.groups_list: self._inventory.add_group(group.name) # add var của group if group.have_vars: for k_group_var, v_group_var in group.vars.items(): self._inventory.set_variable( group.name, k_group_var, v_group_var) # add host cho group if len(group.hosts.all()) > 0: # add host for host_ingroup in group.hosts.all(): # add host cho group self._inventory.add_host(host=host_ingroup.name, group=group.name) # add var if host_ingroup.have_vars: for k_host_ingroup_var, v_host_ingroup_var in host_ingroup.vars.items( ): self._inventory.set_variable( host_ingroup.name, k_host_ingroup_var, v_host_ingroup_var) # add host in ungroup for host_ungroup in inven.hosts_list: self._inventory.add_host(host=host_ungroup.name, group="ungrouped") # add var cho host ungroup if host_ungroup.have_vars: for k_host_var, v_host_var in host_ungroup.vars.items( ): self._inventory.set_variable( host_ungroup.name, k_host_var, v_host_var) # add var default cho inventory # if inven.have_vars: # for k_inven_var, v_inven_var in inven.vars.items(): # try: # self._inventory.set_variable(inven.name, k_inven_var, v_inven_var) # except: # pass else: display.warning("Unable to parse %s as an inventory source" % to_text(source)) return True