@ -78,16 +78,47 @@ class RoleMixin(object):
Note : The methods for actual display of role data are not present here .
Note : The methods for actual display of role data are not present here .
"""
"""
ROLE_ARGSPEC_FILE = ' main.yml '
# Potential locations of the role arg spec file in the meta subdir, with main.yml
# having the lowest priority.
ROLE_ARGSPEC_FILES = [ ' argument_specs ' + e for e in C . YAML_FILENAME_EXTENSIONS ] + [ " main " + e for e in C . YAML_FILENAME_EXTENSIONS ]
def _load_argspec ( self , role_name , collection_path = None , role_path = None ) :
def _load_argspec ( self , role_name , collection_path = None , role_path = None ) :
""" Load the role argument spec data from the source file.
: param str role_name : The name of the role for which we want the argspec data .
: param str collection_path : Path to the collection containing the role . This
will be None for standard roles .
: param str role_path : Path to the standard role . This will be None for
collection roles .
We support two files containing the role arg spec data : either meta / main . yml
or meta / argument_spec . yml . The argument_spec . yml file will take precedence
over the meta / main . yml file , if it exists . Data is NOT combined between the
two files .
: returns : A dict of all data underneath the ` ` argument_specs ` ` top - level YAML
key in the argspec data file . Empty dict is returned if there is no data .
"""
if collection_path :
if collection_path :
path = os . path . join ( collection_path , ' roles ' , role_name , ' meta ' , self . ROLE_ARGSPEC_FILE )
meta_ path = os . path . join ( collection_path , ' roles ' , role_name , ' meta ' )
elif role_path :
elif role_path :
path = os . path . join ( role_path , ' meta ' , self . ROLE_ARGSPEC_FILE )
meta_ path = os . path . join ( role_path , ' meta ' )
else :
else :
raise AnsibleError ( " A path is required to load argument specs for role ' %s ' " % role_name )
raise AnsibleError ( " A path is required to load argument specs for role ' %s ' " % role_name )
path = None
# Check all potential spec files
for specfile in self . ROLE_ARGSPEC_FILES :
full_path = os . path . join ( meta_path , specfile )
if os . path . exists ( full_path ) :
path = full_path
break
if path is None :
return { }
try :
try :
with open ( path , ' r ' ) as f :
with open ( path , ' r ' ) as f :
data = from_yaml ( f . read ( ) , file_name = path )
data = from_yaml ( f . read ( ) , file_name = path )
@ -110,18 +141,25 @@ class RoleMixin(object):
"""
"""
found = set ( )
found = set ( )
found_names = set ( )
found_names = set ( )
for path in role_paths :
for path in role_paths :
if not os . path . isdir ( path ) :
if not os . path . isdir ( path ) :
continue
continue
# Check each subdir for a meta/main.yml file
# Check each subdir for an argument spec file
for entry in os . listdir ( path ) :
for entry in os . listdir ( path ) :
role_path = os . path . join ( path , entry )
role_path = os . path . join ( path , entry )
full_path = os . path . join ( role_path , ' meta ' , self . ROLE_ARGSPEC_FILE )
if os . path . exists ( full_path ) :
# Check all potential spec files
if name_filters is None or entry in name_filters :
for specfile in self . ROLE_ARGSPEC_FILES :
if entry not in found_names :
full_path = os . path . join ( role_path , ' meta ' , specfile )
found . add ( ( entry , role_path ) )
if os . path . exists ( full_path ) :
found_names . add ( entry )
if name_filters is None or entry in name_filters :
if entry not in found_names :
found . add ( ( entry , role_path ) )
found_names . add ( entry )
# select first-found
break
return found
return found
def _find_all_collection_roles ( self , name_filters = None , collection_filter = None ) :
def _find_all_collection_roles ( self , name_filters = None , collection_filter = None ) :
@ -147,19 +185,23 @@ class RoleMixin(object):
roles_dir = os . path . join ( path , ' roles ' )
roles_dir = os . path . join ( path , ' roles ' )
if os . path . exists ( roles_dir ) :
if os . path . exists ( roles_dir ) :
for entry in os . listdir ( roles_dir ) :
for entry in os . listdir ( roles_dir ) :
full_path = os . path . join ( roles_dir , entry , ' meta ' , self . ROLE_ARGSPEC_FILE )
if os . path . exists ( full_path ) :
# Check all potential spec files
if name_filters is None :
for specfile in self . ROLE_ARGSPEC_FILES :
found . add ( ( entry , collname , path ) )
full_path = os . path . join ( roles_dir , entry , ' meta ' , specfile )
else :
if os . path . exists ( full_path ) :
# Name filters might contain a collection FQCN or not.
if name_filters is None :
for fqcn in name_filters :
found . add ( ( entry , collname , path ) )
if len ( fqcn . split ( ' . ' ) ) == 3 :
else :
( ns , col , role ) = fqcn . split ( ' . ' )
# Name filters might contain a collection FQCN or not.
if ' . ' . join ( [ ns , col ] ) == collname and entry == role :
for fqcn in name_filters :
if len ( fqcn . split ( ' . ' ) ) == 3 :
( ns , col , role ) = fqcn . split ( ' . ' )
if ' . ' . join ( [ ns , col ] ) == collname and entry == role :
found . add ( ( entry , collname , path ) )
elif fqcn == entry :
found . add ( ( entry , collname , path ) )
found . add ( ( entry , collname , path ) )
elif fqcn == entry :
break
found . add ( ( entry , collname , path ) )
return found
return found
def _build_summary ( self , role , collection , argspec ) :
def _build_summary ( self , role , collection , argspec ) :