Merge pull request #4525 from ko-zu/ansible
Add a regexp parameter to assemble module to match or exclude filenames.
This commit is contained in:
parent
85a398c2b4
commit
0f806cfdfa
1 changed files with 23 additions and 2 deletions
|
@ -22,6 +22,7 @@ import os
|
|||
import os.path
|
||||
import shutil
|
||||
import tempfile
|
||||
import re
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
|
@ -70,6 +71,14 @@ options:
|
|||
required: false
|
||||
default: "True"
|
||||
version_added: "1.4"
|
||||
regexp:
|
||||
description:
|
||||
- Assemble files only if C(regex) matches the filename. If not set,
|
||||
all files are assembled. All "\" (backslash) must be escaped as
|
||||
"\\\\" to comply yaml syntax. Uses Python regular expressions; see
|
||||
U(http://docs.python.org/2/library/re.html).
|
||||
required: false
|
||||
default: null
|
||||
others:
|
||||
description:
|
||||
- all arguments accepted by the M(file) module also work here
|
||||
|
@ -88,12 +97,14 @@ EXAMPLES = '''
|
|||
# ===========================================
|
||||
# Support method
|
||||
|
||||
def assemble_from_fragments(src_path, delimiter=None):
|
||||
def assemble_from_fragments(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
|
||||
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:
|
||||
tmp.write(delimiter)
|
||||
|
@ -116,6 +127,7 @@ def main():
|
|||
dest = dict(required=True),
|
||||
backup=dict(default=False, type='bool'),
|
||||
remote_src=dict(default=False, type='bool'),
|
||||
regexp = dict(required=False),
|
||||
),
|
||||
add_file_common_args=True
|
||||
)
|
||||
|
@ -127,6 +139,8 @@ def main():
|
|||
dest = os.path.expanduser(module.params['dest'])
|
||||
backup = module.params['backup']
|
||||
delimiter = module.params['delimiter']
|
||||
regexp = module.params['regexp']
|
||||
compiled_regexp = None
|
||||
|
||||
if not os.path.exists(src):
|
||||
module.fail_json(msg="Source (%s) does not exist" % src)
|
||||
|
@ -134,7 +148,13 @@ def main():
|
|||
if not os.path.isdir(src):
|
||||
module.fail_json(msg="Source (%s) is not a directory" % src)
|
||||
|
||||
path = assemble_from_fragments(src, delimiter)
|
||||
if regexp != None:
|
||||
try:
|
||||
compiled_regexp = re.compile(regexp)
|
||||
except re.error, e:
|
||||
module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp))
|
||||
|
||||
path = assemble_from_fragments(src, delimiter, compiled_regexp)
|
||||
pathmd5 = module.md5(path)
|
||||
|
||||
if os.path.exists(dest):
|
||||
|
@ -157,3 +177,4 @@ def main():
|
|||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
|
||||
main()
|
||||
|
||||
|
|
Loading…
Reference in a new issue