From 82b24c162e7d0395355e3d34f79d8eb539cd3de8 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 3 Apr 2014 16:29:15 -0500 Subject: [PATCH] Adding delimiter fixes to action_plugin + fixing local assembling with a delimiter Also added a new integration test for assemble using local assembly with a delimiter. --- lib/ansible/module_utils/basic.py | 1 + lib/ansible/runner/action_plugins/assemble.py | 41 ++++++++++++++----- .../roles/test_assemble/tasks/main.yml | 10 +++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 654042370b5..94326711ba2 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -115,6 +115,7 @@ FILE_COMMON_ARGUMENTS=dict( backup = dict(), force = dict(), remote_src = dict(), # used by assemble + delimiter = dict(), # used by assemble directory_mode = dict(), # used by copy ) diff --git a/lib/ansible/runner/action_plugins/assemble.py b/lib/ansible/runner/action_plugins/assemble.py index 2fd76391769..741053f4cf0 100644 --- a/lib/ansible/runner/action_plugins/assemble.py +++ b/lib/ansible/runner/action_plugins/assemble.py @@ -31,24 +31,43 @@ class ActionModule(object): def __init__(self, runner): self.runner = runner - def _assemble_from_fragments(self, src_path, delimiter=None): + def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None): ''' assemble a file from a directory of fragments ''' tmpfd, temp_path = tempfile.mkstemp() tmp = os.fdopen(tmpfd,'w') delimit_me = False + add_newline = False + for f in sorted(os.listdir(src_path)): + if compiled_regexp and not compiled_regexp.search(f): + continue fragment = "%s/%s" % (src_path, f) - if delimit_me and delimiter: - # en-escape things like new-lines - delimiter = delimiter.decode('unicode-escape') - tmp.write(delimiter) - # always make sure there's a newline after the - # delimiter, so lines don't run together - if delimiter[-1] != '\n': - tmp.write('\n') - if os.path.isfile(fragment): - tmp.write(file(fragment).read()) + if not os.path.isfile(fragment): + continue + fragment_content = file(fragment).read() + + # always put a newline between fragments if the previous fragment didn't end with a newline. + if add_newline: + tmp.write('\n') + + # delimiters should only appear between fragments + if delimit_me: + if delimiter: + # un-escape anything like newlines + delimiter = delimiter.decode('unicode-escape') + tmp.write(delimiter) + # always make sure there's a newline after the + # delimiter, so lines don't run together + if delimiter[-1] != '\n': + tmp.write('\n') + + tmp.write(fragment_content) delimit_me = True + if fragment_content.endswith('\n'): + add_newline = False + else: + add_newline = True + tmp.close() return temp_path diff --git a/test/integration/roles/test_assemble/tasks/main.yml b/test/integration/roles/test_assemble/tasks/main.yml index b20551f8866..f06cee6ace8 100644 --- a/test/integration/roles/test_assemble/tasks/main.yml +++ b/test/integration/roles/test_assemble/tasks/main.yml @@ -69,3 +69,13 @@ - "result.state == 'file'" - "result.md5sum == '96905702a2ece40de6bf3a94b5062513'" +- name: test assemble with remote_src=False and a delimiter + assemble: src="./" dest="{{output_dir}}/assembled5" remote_src=no delimiter="#--- delimiter ---#" + register: result + +- name: assert the fragments were assembled without remote + assert: + that: + - "result.state == 'file'" + - "result.md5sum == '4773eac67aba3f0be745876331c8a450'" +