Introduce content argument

Copy module can take content argument, I think we should introduce this
feature to config_template. It brings the possibility to use lookups for
inline passing of content to template.

Example, we could use:

config_template:
  content: "{{ lookup('file',<file>) }}" to replace current behavior

or:

config_template:
  content: "{{ lookup('url','<url>',wantlist=True) | join ('\n') }}"

to bring inline templating of external sources.

Change-Id: Id5b2743d309f0313603afbbf84279ce0b1e49cfb
Signed-off-by: Jean-Philippe Evrard <jean-philippe.evrard@rackspace.co.uk>
This commit is contained in:
Jean-Philippe Evrard 2016-10-05 00:25:23 +01:00
parent 3d5f848a07
commit 1f404d7f97
2 changed files with 39 additions and 5 deletions

View File

@ -427,11 +427,25 @@ class ActionModule(ActionBase):
file_path = self._loader.get_basedir()
user_source = self._task.args.get('src')
user_content = self._task.args.get('content')
if not user_source:
return False, dict(
failed=True,
msg="No user provided [ src ] was provided"
)
if not user_content:
return False, dict(
failed=True,
msg="No user [ src ] or [ content ] was provided"
)
else:
tmp_content = None
try:
remote_user = task_vars.get('ansible_user') or self._play_context.remote_user
if not tmp_content:
tmp_content = self._make_tmp_path(remote_user) + 'content'
except TypeError:
if not tmp_content:
tmp_content = self._make_tmp_path() + 'content'
with open(tmp_content, 'w') as f:
f.writelines(user_content)
user_source = tmp_content
source = self._loader.path_dwim_relative(
file_path,
'templates',

View File

@ -38,6 +38,26 @@
- "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test.ini')) == 'new_value'"
- "(lookup('ini', 'baz section=foo file=/tmp/test.ini')) == 'bar'"
# Test basic function of config_template with content instead of src
- name: Template test INI template
config_template:
content: "{{ lookup('file', playbook_dir + '/templates/test.ini') }}"
dest: "/tmp/test_with_content.ini"
config_overrides: "{{ test_config_ini_overrides }}"
config_type: "ini"
- name: Read test.ini
slurp:
src: /tmp/test_with_content.ini
register: ini_file_with_content
- debug:
msg: "ini - {{ ini_file_with_content.content | b64decode }}"
- name: Validate output
assert:
that:
- "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test_with_content.ini')) == 'new_value'"
- "(lookup('ini', 'baz section=foo file=/tmp/test_with_content.ini')) == 'bar'"
# Test list additions in config_template
- name: Template test YML template
config_template:
@ -96,4 +116,4 @@
baz: "bar"
test_config_yml_overrides:
list_one:
- four
- four