The module and action plugin are now remaining in base.
| 2 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,101 +0,0 @@ |
| 1 |
-# test code for the assemble module |
|
| 2 |
-# (c) 2014, James Cammarata <jcammarata@ansible.com> |
|
| 3 |
- |
|
| 4 |
-# This file is part of Ansible |
|
| 5 |
-# |
|
| 6 |
-# Ansible is free software: you can redistribute it and/or modify |
|
| 7 |
-# it under the terms of the GNU General Public License as published by |
|
| 8 |
-# the Free Software Foundation, either version 3 of the License, or |
|
| 9 |
-# (at your option) any later version. |
|
| 10 |
-# |
|
| 11 |
-# Ansible is distributed in the hope that it will be useful, |
|
| 12 |
-# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
-# GNU General Public License for more details. |
|
| 15 |
-# |
|
| 16 |
-# You should have received a copy of the GNU General Public License |
|
| 17 |
-# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
|
| 18 |
- |
|
| 19 |
-- name: create a new directory for file source |
|
| 20 |
- file: dest="{{output_dir}}/src" state=directory
|
|
| 21 |
- register: result |
|
| 22 |
- |
|
| 23 |
-- name: assert the directory was created |
|
| 24 |
- assert: |
|
| 25 |
- that: |
|
| 26 |
- - "result.state == 'directory'" |
|
| 27 |
- |
|
| 28 |
-- name: copy the files to a new directory |
|
| 29 |
- copy: src="./" dest="{{output_dir}}/src"
|
|
| 30 |
- register: result |
|
| 31 |
- |
|
| 32 |
-- name: create unicode file for test |
|
| 33 |
- shell: echo "π" > {{ output_dir }}/src/ßΩ.txt
|
|
| 34 |
- register: result |
|
| 35 |
- |
|
| 36 |
-- name: assert that the new file was created |
|
| 37 |
- assert: |
|
| 38 |
- that: |
|
| 39 |
- - "result.changed == true" |
|
| 40 |
- |
|
| 41 |
-- name: test assemble with all fragments |
|
| 42 |
- assemble: src="{{output_dir}}/src" dest="{{output_dir}}/assembled1"
|
|
| 43 |
- register: result |
|
| 44 |
- |
|
| 45 |
-- name: assert the fragments were assembled |
|
| 46 |
- assert: |
|
| 47 |
- that: |
|
| 48 |
- - "result.state == 'file'" |
|
| 49 |
- - "result.changed == True" |
|
| 50 |
- - "result.checksum == '74152e9224f774191bc0bedf460d35de86ad90e6'" |
|
| 51 |
- |
|
| 52 |
-- name: test assemble with all fragments |
|
| 53 |
- assemble: src="{{output_dir}}/src" dest="{{output_dir}}/assembled1"
|
|
| 54 |
- register: result |
|
| 55 |
- |
|
| 56 |
-- name: assert that the same assemble made no changes |
|
| 57 |
- assert: |
|
| 58 |
- that: |
|
| 59 |
- - "result.state == 'file'" |
|
| 60 |
- - "result.changed == False" |
|
| 61 |
- - "result.checksum == '74152e9224f774191bc0bedf460d35de86ad90e6'" |
|
| 62 |
- |
|
| 63 |
-- name: test assemble with fragments matching a regex |
|
| 64 |
- assemble: src="{{output_dir}}/src" dest="{{output_dir}}/assembled2" regexp="^fragment[1-3]$"
|
|
| 65 |
- register: result |
|
| 66 |
- |
|
| 67 |
-- name: assert the fragments were assembled with a regex |
|
| 68 |
- assert: |
|
| 69 |
- that: |
|
| 70 |
- - "result.state == 'file'" |
|
| 71 |
- - "result.checksum == 'edfe2d7487ef8f5ebc0f1c4dc57ba7b70a7b8e2b'" |
|
| 72 |
- |
|
| 73 |
-- name: test assemble with a delimiter |
|
| 74 |
- assemble: src="{{output_dir}}/src" dest="{{output_dir}}/assembled3" delimiter="#--- delimiter ---#"
|
|
| 75 |
- register: result |
|
| 76 |
- |
|
| 77 |
-- name: assert the fragments were assembled with a delimiter |
|
| 78 |
- assert: |
|
| 79 |
- that: |
|
| 80 |
- - "result.state == 'file'" |
|
| 81 |
- - "result.checksum == 'd986cefb82e34e4cf14d33a3cda132ff45aa2980'" |
|
| 82 |
- |
|
| 83 |
-- name: test assemble with remote_src=False |
|
| 84 |
- assemble: src="./" dest="{{output_dir}}/assembled4" remote_src=no
|
|
| 85 |
- register: result |
|
| 86 |
- |
|
| 87 |
-- name: assert the fragments were assembled without remote |
|
| 88 |
- assert: |
|
| 89 |
- that: |
|
| 90 |
- - "result.state == 'file'" |
|
| 91 |
- - "result.checksum == '048a1bd1951aa5ccc427eeb4ca19aee45e9c68b3'" |
|
| 92 |
- |
|
| 93 |
-- name: test assemble with remote_src=False and a delimiter |
|
| 94 |
- assemble: src="./" dest="{{output_dir}}/assembled5" remote_src=no delimiter="#--- delimiter ---#"
|
|
| 95 |
- register: result |
|
| 96 |
- |
|
| 97 |
-- name: assert the fragments were assembled without remote |
|
| 98 |
- assert: |
|
| 99 |
- that: |
|
| 100 |
- - "result.state == 'file'" |
|
| 101 |
- - "result.checksum == '505359f48c65b3904127cf62b912991d4da7ed6d'" |
| 102 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,165 +0,0 @@ |
| 1 |
-# (c) 2013-2016, Michael DeHaan <michael.dehaan@gmail.com> |
|
| 2 |
-# Stephen Fromm <sfromm@gmail.com> |
|
| 3 |
-# Brian Coca <briancoca+dev@gmail.com> |
|
| 4 |
-# Toshio Kuratomi <tkuratomi@ansible.com> |
|
| 5 |
-# |
|
| 6 |
-# This file is part of Ansible |
|
| 7 |
-# |
|
| 8 |
-# Ansible is free software: you can redistribute it and/or modify |
|
| 9 |
-# it under the terms of the GNU General Public License as published by |
|
| 10 |
-# the Free Software Foundation, either version 3 of the License, or |
|
| 11 |
-# (at your option) any later version. |
|
| 12 |
-# |
|
| 13 |
-# Ansible is distributed in the hope that it will be useful, |
|
| 14 |
-# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 |
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 16 |
-# GNU General Public License for more details. |
|
| 17 |
-# |
|
| 18 |
-# You should have received a copy of the GNU General Public License |
|
| 19 |
-from __future__ import (absolute_import, division, print_function) |
|
| 20 |
-__metaclass__ = type |
|
| 21 |
- |
|
| 22 |
-import codecs |
|
| 23 |
-import os |
|
| 24 |
-import os.path |
|
| 25 |
-import re |
|
| 26 |
-import tempfile |
|
| 27 |
- |
|
| 28 |
-from ansible import constants as C |
|
| 29 |
-from ansible.errors import AnsibleError, AnsibleAction, _AnsibleActionDone, AnsibleActionFail |
|
| 30 |
-from ansible.module_utils._text import to_native, to_text |
|
| 31 |
-from ansible.module_utils.parsing.convert_bool import boolean |
|
| 32 |
-from ansible.plugins.action import ActionBase |
|
| 33 |
-from ansible.utils.hashing import checksum_s |
|
| 34 |
- |
|
| 35 |
- |
|
| 36 |
-class ActionModule(ActionBase): |
|
| 37 |
- |
|
| 38 |
- TRANSFERS_FILES = True |
|
| 39 |
- |
|
| 40 |
- def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False, decrypt=True): |
|
| 41 |
- ''' assemble a file from a directory of fragments ''' |
|
| 42 |
- |
|
| 43 |
- tmpfd, temp_path = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP) |
|
| 44 |
- tmp = os.fdopen(tmpfd, 'wb') |
|
| 45 |
- delimit_me = False |
|
| 46 |
- add_newline = False |
|
| 47 |
- |
|
| 48 |
- for f in (to_text(p, errors='surrogate_or_strict') for p in sorted(os.listdir(src_path))): |
|
| 49 |
- if compiled_regexp and not compiled_regexp.search(f): |
|
| 50 |
- continue |
|
| 51 |
- fragment = u"%s/%s" % (src_path, f) |
|
| 52 |
- if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
|
| 53 |
- continue |
|
| 54 |
- |
|
| 55 |
- with open(self._loader.get_real_file(fragment, decrypt=decrypt), 'rb') as fragment_fh: |
|
| 56 |
- fragment_content = fragment_fh.read() |
|
| 57 |
- |
|
| 58 |
- # always put a newline between fragments if the previous fragment didn't end with a newline. |
|
| 59 |
- if add_newline: |
|
| 60 |
- tmp.write(b'\n') |
|
| 61 |
- |
|
| 62 |
- # delimiters should only appear between fragments |
|
| 63 |
- if delimit_me: |
|
| 64 |
- if delimiter: |
|
| 65 |
- # un-escape anything like newlines |
|
| 66 |
- delimiter = codecs.escape_decode(delimiter)[0] |
|
| 67 |
- tmp.write(delimiter) |
|
| 68 |
- # always make sure there's a newline after the |
|
| 69 |
- # delimiter, so lines don't run together |
|
| 70 |
- if delimiter[-1] != b'\n': |
|
| 71 |
- tmp.write(b'\n') |
|
| 72 |
- |
|
| 73 |
- tmp.write(fragment_content) |
|
| 74 |
- delimit_me = True |
|
| 75 |
- if fragment_content.endswith(b'\n'): |
|
| 76 |
- add_newline = False |
|
| 77 |
- else: |
|
| 78 |
- add_newline = True |
|
| 79 |
- |
|
| 80 |
- tmp.close() |
|
| 81 |
- return temp_path |
|
| 82 |
- |
|
| 83 |
- def run(self, tmp=None, task_vars=None): |
|
| 84 |
- |
|
| 85 |
- self._supports_check_mode = False |
|
| 86 |
- |
|
| 87 |
- result = super(ActionModule, self).run(tmp, task_vars) |
|
| 88 |
- del tmp # tmp no longer has any effect |
|
| 89 |
- |
|
| 90 |
- if task_vars is None: |
|
| 91 |
- task_vars = dict() |
|
| 92 |
- |
|
| 93 |
- src = self._task.args.get('src', None)
|
|
| 94 |
- dest = self._task.args.get('dest', None)
|
|
| 95 |
- delimiter = self._task.args.get('delimiter', None)
|
|
| 96 |
- remote_src = self._task.args.get('remote_src', 'yes')
|
|
| 97 |
- regexp = self._task.args.get('regexp', None)
|
|
| 98 |
- follow = self._task.args.get('follow', False)
|
|
| 99 |
- ignore_hidden = self._task.args.get('ignore_hidden', False)
|
|
| 100 |
- decrypt = self._task.args.get('decrypt', True)
|
|
| 101 |
- |
|
| 102 |
- try: |
|
| 103 |
- if src is None or dest is None: |
|
| 104 |
- raise AnsibleActionFail("src and dest are required")
|
|
| 105 |
- |
|
| 106 |
- if boolean(remote_src, strict=False): |
|
| 107 |
- result.update(self._execute_module(module_name='assemble', task_vars=task_vars)) |
|
| 108 |
- raise _AnsibleActionDone() |
|
| 109 |
- else: |
|
| 110 |
- try: |
|
| 111 |
- src = self._find_needle('files', src)
|
|
| 112 |
- except AnsibleError as e: |
|
| 113 |
- raise AnsibleActionFail(to_native(e)) |
|
| 114 |
- |
|
| 115 |
- if not os.path.isdir(src): |
|
| 116 |
- raise AnsibleActionFail(u"Source (%s) is not a directory" % src) |
|
| 117 |
- |
|
| 118 |
- _re = None |
|
| 119 |
- if regexp is not None: |
|
| 120 |
- _re = re.compile(regexp) |
|
| 121 |
- |
|
| 122 |
- # Does all work assembling the file |
|
| 123 |
- path = self._assemble_from_fragments(src, delimiter, _re, ignore_hidden, decrypt) |
|
| 124 |
- |
|
| 125 |
- path_checksum = checksum_s(path) |
|
| 126 |
- dest = self._remote_expand_user(dest) |
|
| 127 |
- dest_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=follow) |
|
| 128 |
- |
|
| 129 |
- diff = {}
|
|
| 130 |
- |
|
| 131 |
- # setup args for running modules |
|
| 132 |
- new_module_args = self._task.args.copy() |
|
| 133 |
- |
|
| 134 |
- # clean assemble specific options |
|
| 135 |
- for opt in ['remote_src', 'regexp', 'delimiter', 'ignore_hidden', 'decrypt']: |
|
| 136 |
- if opt in new_module_args: |
|
| 137 |
- del new_module_args[opt] |
|
| 138 |
- new_module_args['dest'] = dest |
|
| 139 |
- |
|
| 140 |
- if path_checksum != dest_stat['checksum']: |
|
| 141 |
- |
|
| 142 |
- if self._play_context.diff: |
|
| 143 |
- diff = self._get_diff_data(dest, path, task_vars) |
|
| 144 |
- |
|
| 145 |
- remote_path = self._connection._shell.join_path(self._connection._shell.tmpdir, 'src') |
|
| 146 |
- xfered = self._transfer_file(path, remote_path) |
|
| 147 |
- |
|
| 148 |
- # fix file permissions when the copy is done as a different user |
|
| 149 |
- self._fixup_perms2((self._connection._shell.tmpdir, remote_path)) |
|
| 150 |
- |
|
| 151 |
- new_module_args.update(dict(src=xfered,)) |
|
| 152 |
- |
|
| 153 |
- res = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars) |
|
| 154 |
- if diff: |
|
| 155 |
- res['diff'] = diff |
|
| 156 |
- result.update(res) |
|
| 157 |
- else: |
|
| 158 |
- result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars)) |
|
| 159 |
- |
|
| 160 |
- except AnsibleAction as e: |
|
| 161 |
- result.update(e.result) |
|
| 162 |
- finally: |
|
| 163 |
- self._remove_tmp_path(self._connection._shell.tmpdir) |
|
| 164 |
- |
|
| 165 |
- return result |
| 166 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,261 +0,0 @@ |
| 1 |
-#!/usr/bin/python |
|
| 2 |
-# -*- coding: utf-8 -*- |
|
| 3 |
- |
|
| 4 |
-# Copyright: (c) 2012, Stephen Fromm <sfromm@gmail.com> |
|
| 5 |
-# Copyright: (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com> |
|
| 6 |
-# Copyright: (c) 2017, Ansible Project |
|
| 7 |
-# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
|
| 8 |
- |
|
| 9 |
-from __future__ import absolute_import, division, print_function |
|
| 10 |
-__metaclass__ = type |
|
| 11 |
- |
|
| 12 |
-ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
| 13 |
- 'status': ['stableinterface'], |
|
| 14 |
- 'supported_by': 'core'} |
|
| 15 |
- |
|
| 16 |
-DOCUMENTATION = r''' |
|
| 17 |
-module: assemble |
|
| 18 |
-short_description: Assemble configuration files from fragments |
|
| 19 |
-description: |
|
| 20 |
-- Assembles a configuration file from fragments. |
|
| 21 |
-- Often a particular program will take a single configuration file and does not support a |
|
| 22 |
- C(conf.d) style structure where it is easy to build up the configuration |
|
| 23 |
- from multiple sources. C(assemble) will take a directory of files that can be |
|
| 24 |
- local or have already been transferred to the system, and concatenate them |
|
| 25 |
- together to produce a destination file. |
|
| 26 |
-- Files are assembled in string sorting order. |
|
| 27 |
-- Puppet calls this idea I(fragments). |
|
| 28 |
-version_added: '0.5' |
|
| 29 |
-options: |
|
| 30 |
- src: |
|
| 31 |
- description: |
|
| 32 |
- - An already existing directory full of source files. |
|
| 33 |
- type: path |
|
| 34 |
- required: true |
|
| 35 |
- dest: |
|
| 36 |
- description: |
|
| 37 |
- - A file to create using the concatenation of all of the source files. |
|
| 38 |
- type: path |
|
| 39 |
- required: true |
|
| 40 |
- backup: |
|
| 41 |
- description: |
|
| 42 |
- - Create a backup file (if C(yes)), including the timestamp information so |
|
| 43 |
- you can get the original file back if you somehow clobbered it |
|
| 44 |
- incorrectly. |
|
| 45 |
- type: bool |
|
| 46 |
- default: no |
|
| 47 |
- delimiter: |
|
| 48 |
- description: |
|
| 49 |
- - A delimiter to separate the file contents. |
|
| 50 |
- type: str |
|
| 51 |
- version_added: '1.4' |
|
| 52 |
- remote_src: |
|
| 53 |
- description: |
|
| 54 |
- - If C(no), it will search for src at originating/master machine. |
|
| 55 |
- - If C(yes), it will go to the remote/target machine for the src. |
|
| 56 |
- type: bool |
|
| 57 |
- default: no |
|
| 58 |
- version_added: '1.4' |
|
| 59 |
- regexp: |
|
| 60 |
- description: |
|
| 61 |
- - Assemble files only if C(regex) matches the filename. |
|
| 62 |
- - If not set, all files are assembled. |
|
| 63 |
- - Every "\" (backslash) must be escaped as "\\" to comply to YAML syntax. |
|
| 64 |
- - Uses L(Python regular expressions,http://docs.python.org/2/library/re.html). |
|
| 65 |
- type: str |
|
| 66 |
- ignore_hidden: |
|
| 67 |
- description: |
|
| 68 |
- - A boolean that controls if files that start with a '.' will be included or not. |
|
| 69 |
- type: bool |
|
| 70 |
- default: no |
|
| 71 |
- version_added: '2.0' |
|
| 72 |
- validate: |
|
| 73 |
- description: |
|
| 74 |
- - The validation command to run before copying into place. |
|
| 75 |
- - The path to the file to validate is passed in via '%s' which must be present as in the sshd example below. |
|
| 76 |
- - The command is passed securely so shell features like expansion and pipes won't work. |
|
| 77 |
- type: str |
|
| 78 |
- version_added: '2.0' |
|
| 79 |
-seealso: |
|
| 80 |
-- module: copy |
|
| 81 |
-- module: template |
|
| 82 |
-- module: win_copy |
|
| 83 |
-author: |
|
| 84 |
-- Stephen Fromm (@sfromm) |
|
| 85 |
-extends_documentation_fragment: |
|
| 86 |
-- decrypt |
|
| 87 |
-- files |
|
| 88 |
-''' |
|
| 89 |
- |
|
| 90 |
-EXAMPLES = r''' |
|
| 91 |
-- name: Assemble from fragments from a directory |
|
| 92 |
- assemble: |
|
| 93 |
- src: /etc/someapp/fragments |
|
| 94 |
- dest: /etc/someapp/someapp.conf |
|
| 95 |
- |
|
| 96 |
-- name: Inserted provided delimiter in between each fragment |
|
| 97 |
- assemble: |
|
| 98 |
- src: /etc/someapp/fragments |
|
| 99 |
- dest: /etc/someapp/someapp.conf |
|
| 100 |
- delimiter: '### START FRAGMENT ###' |
|
| 101 |
- |
|
| 102 |
-- name: Assemble a new "sshd_config" file into place, after passing validation with sshd |
|
| 103 |
- assemble: |
|
| 104 |
- src: /etc/ssh/conf.d/ |
|
| 105 |
- dest: /etc/ssh/sshd_config |
|
| 106 |
- validate: /usr/sbin/sshd -t -f %s |
|
| 107 |
-''' |
|
| 108 |
- |
|
| 109 |
-import codecs |
|
| 110 |
-import os |
|
| 111 |
-import re |
|
| 112 |
-import tempfile |
|
| 113 |
- |
|
| 114 |
-from ansible.module_utils.basic import AnsibleModule |
|
| 115 |
-from ansible.module_utils.six import b, indexbytes |
|
| 116 |
-from ansible.module_utils._text import to_native |
|
| 117 |
- |
|
| 118 |
- |
|
| 119 |
-def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False, tmpdir=None): |
|
| 120 |
- ''' assemble a file from a directory of fragments ''' |
|
| 121 |
- tmpfd, temp_path = tempfile.mkstemp(dir=tmpdir) |
|
| 122 |
- tmp = os.fdopen(tmpfd, 'wb') |
|
| 123 |
- delimit_me = False |
|
| 124 |
- add_newline = False |
|
| 125 |
- |
|
| 126 |
- for f in sorted(os.listdir(src_path)): |
|
| 127 |
- if compiled_regexp and not compiled_regexp.search(f): |
|
| 128 |
- continue |
|
| 129 |
- fragment = os.path.join(src_path, f) |
|
| 130 |
- if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
|
| 131 |
- continue |
|
| 132 |
- with open(fragment, 'rb') as fragment_fh: |
|
| 133 |
- fragment_content = fragment_fh.read() |
|
| 134 |
- |
|
| 135 |
- # always put a newline between fragments if the previous fragment didn't end with a newline. |
|
| 136 |
- if add_newline: |
|
| 137 |
- tmp.write(b('\n'))
|
|
| 138 |
- |
|
| 139 |
- # delimiters should only appear between fragments |
|
| 140 |
- if delimit_me: |
|
| 141 |
- if delimiter: |
|
| 142 |
- # un-escape anything like newlines |
|
| 143 |
- delimiter = codecs.escape_decode(delimiter)[0] |
|
| 144 |
- tmp.write(delimiter) |
|
| 145 |
- # always make sure there's a newline after the |
|
| 146 |
- # delimiter, so lines don't run together |
|
| 147 |
- |
|
| 148 |
- # byte indexing differs on Python 2 and 3, |
|
| 149 |
- # use indexbytes for compat |
|
| 150 |
- # chr(10) == '\n' |
|
| 151 |
- if indexbytes(delimiter, -1) != 10: |
|
| 152 |
- tmp.write(b('\n'))
|
|
| 153 |
- |
|
| 154 |
- tmp.write(fragment_content) |
|
| 155 |
- delimit_me = True |
|
| 156 |
- if fragment_content.endswith(b('\n')):
|
|
| 157 |
- add_newline = False |
|
| 158 |
- else: |
|
| 159 |
- add_newline = True |
|
| 160 |
- |
|
| 161 |
- tmp.close() |
|
| 162 |
- return temp_path |
|
| 163 |
- |
|
| 164 |
- |
|
| 165 |
-def cleanup(path, result=None): |
|
| 166 |
- # cleanup just in case |
|
| 167 |
- if os.path.exists(path): |
|
| 168 |
- try: |
|
| 169 |
- os.remove(path) |
|
| 170 |
- except (IOError, OSError) as e: |
|
| 171 |
- # don't error on possible race conditions, but keep warning |
|
| 172 |
- if result is not None: |
|
| 173 |
- result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, to_native(e))] |
|
| 174 |
- |
|
| 175 |
- |
|
| 176 |
-def main(): |
|
| 177 |
- |
|
| 178 |
- module = AnsibleModule( |
|
| 179 |
- # not checking because of daisy chain to file module |
|
| 180 |
- argument_spec=dict( |
|
| 181 |
- src=dict(type='path', required=True), |
|
| 182 |
- delimiter=dict(type='str'), |
|
| 183 |
- dest=dict(type='path', required=True), |
|
| 184 |
- backup=dict(type='bool', default=False), |
|
| 185 |
- remote_src=dict(type='bool', default=False), |
|
| 186 |
- regexp=dict(type='str'), |
|
| 187 |
- ignore_hidden=dict(type='bool', default=False), |
|
| 188 |
- validate=dict(type='str'), |
|
| 189 |
- ), |
|
| 190 |
- add_file_common_args=True, |
|
| 191 |
- ) |
|
| 192 |
- |
|
| 193 |
- changed = False |
|
| 194 |
- path_hash = None |
|
| 195 |
- dest_hash = None |
|
| 196 |
- src = module.params['src'] |
|
| 197 |
- dest = module.params['dest'] |
|
| 198 |
- backup = module.params['backup'] |
|
| 199 |
- delimiter = module.params['delimiter'] |
|
| 200 |
- regexp = module.params['regexp'] |
|
| 201 |
- compiled_regexp = None |
|
| 202 |
- ignore_hidden = module.params['ignore_hidden'] |
|
| 203 |
- validate = module.params.get('validate', None)
|
|
| 204 |
- |
|
| 205 |
- result = dict(src=src, dest=dest) |
|
| 206 |
- if not os.path.exists(src): |
|
| 207 |
- module.fail_json(msg="Source (%s) does not exist" % src) |
|
| 208 |
- |
|
| 209 |
- if not os.path.isdir(src): |
|
| 210 |
- module.fail_json(msg="Source (%s) is not a directory" % src) |
|
| 211 |
- |
|
| 212 |
- if regexp is not None: |
|
| 213 |
- try: |
|
| 214 |
- compiled_regexp = re.compile(regexp) |
|
| 215 |
- except re.error as e: |
|
| 216 |
- module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (to_native(e), regexp)) |
|
| 217 |
- |
|
| 218 |
- if validate and "%s" not in validate: |
|
| 219 |
- module.fail_json(msg="validate must contain %%s: %s" % validate) |
|
| 220 |
- |
|
| 221 |
- path = assemble_from_fragments(src, delimiter, compiled_regexp, ignore_hidden, module.tmpdir) |
|
| 222 |
- path_hash = module.sha1(path) |
|
| 223 |
- result['checksum'] = path_hash |
|
| 224 |
- |
|
| 225 |
- # Backwards compat. This won't return data if FIPS mode is active |
|
| 226 |
- try: |
|
| 227 |
- pathmd5 = module.md5(path) |
|
| 228 |
- except ValueError: |
|
| 229 |
- pathmd5 = None |
|
| 230 |
- result['md5sum'] = pathmd5 |
|
| 231 |
- |
|
| 232 |
- if os.path.exists(dest): |
|
| 233 |
- dest_hash = module.sha1(dest) |
|
| 234 |
- |
|
| 235 |
- if path_hash != dest_hash: |
|
| 236 |
- if validate: |
|
| 237 |
- (rc, out, err) = module.run_command(validate % path) |
|
| 238 |
- result['validation'] = dict(rc=rc, stdout=out, stderr=err) |
|
| 239 |
- if rc != 0: |
|
| 240 |
- cleanup(path) |
|
| 241 |
- module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc, err)) |
|
| 242 |
- if backup and dest_hash is not None: |
|
| 243 |
- result['backup_file'] = module.backup_local(dest) |
|
| 244 |
- |
|
| 245 |
- module.atomic_move(path, dest, unsafe_writes=module.params['unsafe_writes']) |
|
| 246 |
- changed = True |
|
| 247 |
- |
|
| 248 |
- cleanup(path, result) |
|
| 249 |
- |
|
| 250 |
- # handle file permissions |
|
| 251 |
- file_args = module.load_file_common_arguments(module.params) |
|
| 252 |
- result['changed'] = module.set_fs_attributes_if_different(file_args, changed) |
|
| 253 |
- |
|
| 254 |
- # Mission complete |
|
| 255 |
- result['msg'] = "OK" |
|
| 256 |
- module.exit_json(**result) |
|
| 257 |
- |
|
| 258 |
- |
|
| 259 |
-if __name__ == '__main__': |
|
| 260 |
- main() |