def _handle_inventory(self, inventory_path): """ Scan inventory. As Ansible is a big mess without any kind of preconceived notion of design, there are several (and I use that word lightly) different ways inventory_path can be handled: - a non-executable file: handled as a Ansible 'hosts' file. - an executable file: handled as a dynamic inventory file. - a directory: scanned for Ansible 'hosts' and dynamic inventory files. """ if os.path.isfile(inventory_path) and \ util.is_executable(inventory_path): # It's a file and it's executable. Handle as dynamic inventory script self._parse_dyn_inventory(inventory_path) elif os.path.isfile(inventory_path): # Static inventory hosts file self._parse_hosts_inventory(inventory_path) elif os.path.isdir(inventory_path): # Scan directory for fname in os.listdir(inventory_path): # Skip files that end with certain extensions or characters if any( fname.endswith(ext) for ext in [ "~", ".orig", ".bak", ".ini", ".cfg", ".retry", ".pyc", ".pyo" ]): continue self._handle_inventory(os.path.join(inventory_path, fname)) else: raise IOError( "Invalid inventory file / dir: '{0}'".format(inventory_path))
def _handle_inventory(self, inventory_path): """ Scan inventory. As Ansible is a big mess without any kind of preconceived notion of design, there are several (and I use that word lightly) different ways inventory_path can be handled: - a non-executable file: handled as a Ansible 'hosts' file. - an executable file: handled as a dynamic inventory file. - a directory: scanned for Ansible 'hosts' and dynamic inventory files. """ self.log.debug( "Determining type of inventory_path {}".format(inventory_path)) if os.path.isfile(inventory_path) and \ util.is_executable(inventory_path): # It's a file and it's executable. Handle as dynamic inventory script self.log.debug( "{} is a executable. Handle as dynamic inventory script". format(inventory_path)) self._parse_dyn_inventory(inventory_path) if os.path.isfile(inventory_path) and \ (inventory_path.endswith('.yml') or inventory_path.endswith('.yaml')): self.log.debug( "{} is a YAML inventory file. Parsing it with ansible-inventory" .format(inventory_path)) self._parse_ansible_inventory(inventory_path) elif os.path.isfile(inventory_path): # Static inventory hosts file self.log.debug( "{} is a file. Handle as static inventory file".format( inventory_path)) self._parse_hosts_inventory(inventory_path) elif os.path.isdir(inventory_path): # Directory self.log.debug( "{} is a dir. Just try most files to see if they happen to be inventory files" .format(inventory_path)) # Don't parse folder as inventory if it is a .git or group/host_vars if any( os.path.basename(inventory_path) == name for name in ['.git', 'group_vars', 'host_vars']): return # Scan directory for fname in os.listdir(inventory_path): # Skip files that end with certain extensions or characters if any( fname.endswith(ext) for ext in [ "~", ".orig", ".bak", ".ini", ".cfg", ".retry", ".pyc", ".pyo", ".gitignore" ]): continue self._handle_inventory(os.path.join(inventory_path, fname)) else: raise IOError( "Invalid inventory file / dir: '{0}'".format(inventory_path))
def _handle_inventory(self, inventory_path): """ Scan inventory. As Ansible is a big mess without any kind of preconceived notion of design, there are several (and I use that word lightly) different ways inventory_path can be handled: - a non-executable file: handled as a Ansible 'hosts' file. - an executable file: handled as a dynamic inventory file. - a directory: scanned for Ansible 'hosts' and dynamic inventory files. """ self.log.debug("Determining type of inventory_path {}".format(inventory_path)) if os.path.isfile(inventory_path) and \ util.is_executable(inventory_path): # It's a file and it's executable. Handle as dynamic inventory script self.log.debug("{} is a executable. Handle as dynamic inventory script".format(inventory_path)) self._parse_dyn_inventory(inventory_path) elif os.path.isfile(inventory_path): # Static inventory hosts file self.log.debug("{} is a file. Handle as static inventory file".format(inventory_path)) self._parse_hosts_inventory(inventory_path) elif os.path.isdir(inventory_path): # Directory self.log.debug("{} is a dir. Just try most files to see if they happen to be inventory files".format(inventory_path)) # Don't parse folder as inventory if it is a .git or group/host_vars if any(os.path.basename(inventory_path) == name for name in ['.git', 'group_vars', 'host_vars']): return # Scan directory for fname in os.listdir(inventory_path): # Skip files that end with certain extensions or characters if any(fname.endswith(ext) for ext in ["~", ".orig", ".bak", ".ini", ".cfg", ".retry", ".pyc", ".pyo", ".gitignore"]): continue self._handle_inventory(os.path.join(inventory_path, fname)) else: raise IOError("Invalid inventory file / dir: '{0}'".format(inventory_path))