From 1f404d7f97f2937869c12939b32ab6f10f69d6b7 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Evrard Date: Wed, 5 Oct 2016 00:25:23 +0100 Subject: [PATCH] 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',) }}" to replace current behavior or: config_template: content: "{{ lookup('url','',wantlist=True) | join ('\n') }}" to bring inline templating of external sources. Change-Id: Id5b2743d309f0313603afbbf84279ce0b1e49cfb Signed-off-by: Jean-Philippe Evrard --- action/_v2_config_template.py | 22 ++++++++++++++++++---- tests/test-config_template.yml | 22 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/action/_v2_config_template.py b/action/_v2_config_template.py index 04f6930..3ee0cff 100644 --- a/action/_v2_config_template.py +++ b/action/_v2_config_template.py @@ -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', diff --git a/tests/test-config_template.yml b/tests/test-config_template.yml index 47dd59c..c8d695a 100644 --- a/tests/test-config_template.yml +++ b/tests/test-config_template.yml @@ -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 \ No newline at end of file + - four