def yamlrolesfile(candidate, settings): rolesfile = os.path.join(os.path.dirname(candidate.path), "rolesfile") result = Result(candidate) if os.path.exists(rolesfile) and not os.path.exists(rolesfile + ".yml"): result.errors = [Error(None, "Rolesfile %s does not " "have a yaml extension" % rolesfile)] return result rolesfile = os.path.join(os.path.dirname(candidate.path), "rolesfile.yml") if os.path.exists(rolesfile): with codecs.open(rolesfile, mode='rb', encoding='utf-8') as f: try: yaml.safe_load(f) except Exception, e: result.errors = [Error(None, "Cannot parse YAML from %s: %s" % (rolesfile, str(e)))]
def parse(candidate, options): result = Result(candidate.path) try: parse_inventory(candidate.path) except Exception as e: result.errors = [Error(None, "Inventory is broken: %s" % e.message)] return result
def metamain(candidate, settings): try: fh = codecs.open(candidate.path, mode='rb', encoding='utf-8') except IOError, e: result = Result(candidate) result.errors = [Error(None, "Could not open %s: %s" % (candidate.path, e))]
def yamlrolesfile(candidate, settings): rolesfile = os.path.join(os.path.dirname(candidate.path), "rolesfile") result = Result(candidate) if os.path.exists(rolesfile) and not os.path.exists(rolesfile + ".yml"): result.errors = [Error(None, "Rolesfile %s does not " "have a .yml extension" % rolesfile)] return result rolesfile = os.path.join(os.path.dirname(candidate.path), "rolesfile.yml") if os.path.exists(rolesfile): with codecs.open(rolesfile, mode='rb', encoding='utf-8') as f: try: yaml.safe_load(f) except Exception as e: result.errors = [Error(None, "Cannot parse YAML from %s: %s" % (rolesfile, str(e)))] return result
def metamain(candidate, settings): try: fh = codecs.open(candidate.path, mode='rb', encoding='utf-8') except IOError, e: result = Result(candidate) result.errors = [ Error(None, "Could not open %s: %s" % (candidate.path, e)) ]
def same_variable_defined_in_competing_groups(candidate, options): result = Result(candidate.path) # assume that group_vars file is under an inventory *directory* invfile = os.path.dirname(os.path.dirname(candidate.path)) global _inv try: if ANSIBLE > 1: loader = ansible.parsing.dataloader.DataLoader() try: from ansible.inventory.manager import InventoryManager inv = _inv or InventoryManager(loader=loader, sources=invfile) except ImportError: var_manager = VariableManager() inv = _inv or ansible.inventory.Inventory(loader=loader, variable_manager=var_manager, host_list=invfile) _inv = inv else: inv = _inv or ansible.inventory.Inventory(invfile) _inv = inv except AnsibleError as e: result.errors = [Error(None, "Inventory is broken: %s" % e.message)] return result if hasattr(inv, 'groups'): group = inv.groups.get(os.path.basename(candidate.path)) else: group = inv.get_group(os.path.basename(candidate.path)) if not group: # group file exists in group_vars but no related group # in inventory directory return result remove_inherited_and_overridden_group_vars(group, inv) group_vars = set(_vars[group].keys()) child_hosts = group.hosts child_groups = group.child_groups siblings = set() for child_host in child_hosts: siblings.update(child_host.groups) for child_group in child_groups: siblings.update(child_group.parent_groups) for sibling in siblings: if sibling != group: remove_inherited_and_overridden_group_vars(sibling, inv) sibling_vars = set(_vars[sibling].keys()) common_vars = sibling_vars & group_vars common_hosts = [host.name for host in set(child_hosts) & set(sibling.hosts)] if common_vars and common_hosts: for var in common_vars: error_msg_template = "Sibling groups {0} and {1} with common hosts {2} " + \ "both define variable {3}" error_msg = error_msg_template.format(group.name, sibling.name, ", ".join(common_hosts), var) result.errors.append(Error(None, error_msg)) return result
def same_variable_defined_in_competing_groups(candidate, options): result = Result(candidate.path) vaultpass = get_vault_password(options) # assume that group_vars file is under an inventory *directory* sdirs = candidate.path.split(os.sep) if sdirs.index('group_vars') == 0: invfile = os.getcwd() else: invfile = os.path.join(*sdirs[:sdirs.index('group_vars')]) grpname = os.path.splitext(sdirs[sdirs.index('group_vars') + 1])[0] global _inv try: inv = _inv or parse_inventory(invfile) except AnsibleError as e: result.errors = [Error(None, "Inventory is broken: %s" % e.message)] return result if hasattr(inv, 'groups'): group = inv.groups.get(grpname) else: group = inv.get_group(grpname) if not group: # group file exists in group_vars but no related group # in inventory directory return result remove_inherited_and_overridden_group_vars(group, inv, invfile, vaultpass) group_vars = set(_vars[group].keys()) child_hosts = group.hosts child_groups = group.child_groups siblings = set() for child_host in child_hosts: siblings.update(child_host.groups) for child_group in child_groups: siblings.update(child_group.parent_groups) for sibling in siblings: if sibling != group: remove_inherited_and_overridden_group_vars(sibling, inv, invfile, vaultpass) sibling_vars = set(_vars[sibling].keys()) common_vars = sibling_vars & group_vars common_hosts = [ host.name for host in set(child_hosts) & set(sibling.hosts) ] if common_vars and common_hosts: for var in common_vars: error_msg_template = "Sibling groups {0} and {1} with common hosts {2} " + \ "both define variable {3}" error_msg = error_msg_template.format( group.name, sibling.name, ", ".join(common_hosts), var) result.errors.append(Error(None, error_msg)) return result
def parse(candidate, options): result = Result(candidate.path) try: if ANSIBLE > 1: loader = ansible.parsing.dataloader.DataLoader() var_manager = ansible.vars.VariableManager() ansible.inventory.Inventory(loader=loader, variable_manager=var_manager, host_list=candidate.path) else: ansible.inventory.Inventory(candidate.path) except Exception, e: result.errors = [Error(None, "Inventory is broken: %s" % e.message)]
def rolesfile_contains_scm_in_src(candidate, settings): result = Result(candidate.path) if candidate.path.endswith(".yml") and os.path.exists(candidate.path): try: with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: roles = parse_yaml_linenumbers(f.read(), candidate.path) for role in roles: if '+' in role.get('src'): error = Error(role['__line__'], "Use scm key rather " "than src: scm+url") result.errors.append(error) except Exception, e: result.errors = [Error(None, "Cannot parse YAML from %s: %s" % (candidate.path, str(e)))]
def rolesfile_contains_scm_in_src(candidate, settings): result = Result(candidate.path) if candidate.path.endswith(".yml") and os.path.exists(candidate.path): try: with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: roles = parse_yaml_linenumbers(f.read(), candidate.path) for role in roles: if '+' in role.get('src'): error = Error(role['__line__'], "Use scm key rather " "than src: scm+url") result.errors.append(error) except Exception as e: result.errors = [Error(None, "Cannot parse YAML from %s: %s" % (candidate.path, str(e)))] return result
def same_variable_defined_in_competing_groups(candidate, options): result = Result(candidate.path) # assume that group_vars file is under an inventory *directory* invfile = os.path.dirname(os.path.dirname(candidate.path)) global _inv try: if ANSIBLE > 1: loader = ansible.parsing.dataloader.DataLoader() var_manager = ansible.vars.VariableManager() inv = _inv or ansible.inventory.Inventory( loader=loader, variable_manager=var_manager, host_list=invfile) _inv = inv else: inv = _inv or ansible.inventory.Inventory(invfile) _inv = inv except AnsibleError, e: result.errors = [Error(None, "Inventory is broken: %s" % e.message)] return result
try: fh = codecs.open(candidate.path, mode='rb', encoding='utf-8') except IOError, e: result = Result(candidate) result.errors = [ Error(None, "Could not open %s: %s" % (candidate.path, e)) ] try: result = Result(candidate) data = yaml.safe_load(fh) if 'dependencies' in data: if data["dependencies"] == []: return result else: result.errors = [ Error(None, "Role dependencies are " "not empty") ] else: result.errors = [ Error( None, "Role meta/main.yml does " "not contain a dependencies section") ] except Exception, e: result.errors = [ Error(None, "Could not parse in %s: %s" % (candidate.path, e)) ] finally: fh.close() return result
def metamain(candidate, settings): try: fh = codecs.open(candidate.path, mode='rb', encoding='utf-8') except IOError, e: result = Result(candidate) result.errors = [Error(None, "Could not open %s: %s" % (candidate.path, e))] try: result = Result(candidate) data = yaml.safe_load(fh) if 'dependencies' in data: if data["dependencies"] == []: return result else: result.errors = [Error(None, "Role dependencies are " "not empty")] else: result.errors = [Error(None, "Role meta/main.yml does " "not contain a dependencies section")] except Exception, e: result.errors = [Error(None, "Could not parse in %s: %s" % (candidate.path, e))] finally: fh.close() return result def yamlrolesfile(candidate, settings): rolesfile = os.path.join(os.path.dirname(candidate.path), "rolesfile") result = Result(candidate) if os.path.exists(rolesfile) and not os.path.exists(rolesfile + ".yml"):