Custom ansible module in python - How to resolve relative role file paths? -
i'm writing new custom ansible module in python.
i want able resolve paths in code (in order read content) "file" , "copy" modules when receiving relative path in "src" argument (the path relative root-dir/roles/x/files
), instance.
is possible so? , if so, how?
update
this seems impossible @ moment. i've added feature request here.
default_roles_path
defined in ansible's constants.py
file:
default_roles_path = shell_expand_path(get_config(p, defaults, 'roles_path', 'ansible_roles_path', '/etc/ansible/roles'))
by importing from ansible.constant import default_roles_path
module follow path changes might occur in ansible project.
utils.py
next file should become familiar when writing modules contains path-lookup functions (see path_dwim
) , other ansible-specific interaction functions.
if want plugin work copy module, can see logic file pathing in runner's __init__.py
:
if items_plugin not none , items_plugin in utils.plugins.lookup_loader: basedir = self.basedir if '_original_file' in inject: basedir = os.path.dirname(inject['_original_file']) filesdir = os.path.join(basedir, '..', 'files') if os.path.exists(filesdir): basedir = filesdir
where self.basedir defined utils.default(basedir, lambda: os.getcwd())
, basedir optionally playbook directory.
by providing proper argument spec module, should able automagically take advantage of these underlying details.
at bottom of module, include basic module_utils hook methods expected exist ('fail_json', etc) , populate other defaults you:
from ansible.module_utils.basic import *
i believe these portions in place, you'll trigger file search path, please tell me if i'm incorrect.
Comments
Post a Comment