├── .gitignore
├── LICENSE
├── README.md
├── action_plugins
└── copyv.py
├── hosts
├── library
└── copyv.py
├── notasecret.txt
├── secret.txt
└── test.yml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Ansible 'copyv' Plugin
2 |
3 | This is known to work with Ansible v1.9.2. I've not tested it with anything else so YMMV.
4 |
5 | A plugin for Ansible v2.x is available on [this branch](https://github.com/saygoweb/ansible-plugin-copyv/tree/ansible-2.x).
6 |
7 | ## TLDR;
8 |
9 | ### Install
10 | Copy the two files in the folders action_plugins and library alongside your own main playbook files. Ansible includes the `action_plugins` folder in the plugin search path.
11 |
12 | Your folder structure should look like this:
13 |
14 | SomePlay.yml
15 | action_plugins/copyv.py
16 | library/copyv.py
17 |
18 |
19 | ### Use
20 |
21 | - name: Copy Some Secret File
22 | copyv: src="secret.txt" dest="/tmp/"
23 |
24 | See test.yml for an example.
25 |
26 | You can use anything that the [copy module](http://docs.ansible.com/copy_module.html) supports.
27 |
28 | ## References
29 |
30 | There's a [Stack Overflow](https://stackoverflow.com/questions/22773294/how-to-upload-encrypted-file-using-ansible-vault) question relating to this issue which then leads to several issues and pull-requests in ansible, and ansible module repositories.
31 |
32 | ## Running test.yml
33 |
34 | If you want to run the included test.yml you can do so with something like this:
35 |
36 | ansible-playbook -h hosts test.yml --ask-vault
37 |
38 | The vault password is `test`.
--------------------------------------------------------------------------------
/action_plugins/copyv.py:
--------------------------------------------------------------------------------
1 | # (c) 2012-2014, Michael DeHaan
2 | #
3 | # This file is part of Ansible
4 | #
5 | # Ansible is free software: you can redistribute it and/or modify
6 | # it under the terms of the GNU General Public License as published by
7 | # the Free Software Foundation, either version 3 of the License, or
8 | # (at your option) any later version.
9 | #
10 | # Ansible is distributed in the hope that it will be useful,
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | # GNU General Public License for more details.
14 | #
15 | # You should have received a copy of the GNU General Public License
16 | # along with Ansible. If not, see .
17 |
18 | import os
19 |
20 | from ansible import utils
21 | import ansible.constants as C
22 | import ansible.utils.template as template
23 | from ansible.utils.vault import VaultLib
24 | from ansible import errors
25 | from ansible.runner.return_data import ReturnData
26 | import base64
27 | import json
28 | import stat
29 | import tempfile
30 | import pipes
31 |
32 | ## fixes https://github.com/ansible/ansible/issues/3518
33 | # http://mypy.pythonblogs.com/12_mypy/archive/1253_workaround_for_python_bug_ascii_codec_cant_encode_character_uxa0_in_position_111_ordinal_not_in_range128.html
34 | import sys
35 | reload(sys)
36 | sys.setdefaultencoding("utf8")
37 |
38 |
39 | class ActionModule(object):
40 |
41 | def __init__(self, runner):
42 | self.runner = runner
43 |
44 | def run(self, conn, tmp_path, module_name, module_args, inject, complex_args=None, **kwargs):
45 | ''' handler for file transfer operations '''
46 |
47 | # load up options
48 | options = {}
49 | if complex_args:
50 | options.update(complex_args)
51 | options.update(utils.parse_kv(module_args))
52 | source = options.get('src', None)
53 | content = options.get('content', None)
54 | dest = options.get('dest', None)
55 | raw = utils.boolean(options.get('raw', 'no'))
56 | force = utils.boolean(options.get('force', 'yes'))
57 |
58 | # content with newlines is going to be escaped to safely load in yaml
59 | # now we need to unescape it so that the newlines are evaluated properly
60 | # when writing the file to disk
61 | if content:
62 | if isinstance(content, unicode):
63 | try:
64 | content = content.decode('unicode-escape')
65 | except UnicodeDecodeError:
66 | pass
67 |
68 | if (source is None and content is None and not 'first_available_file' in inject) or dest is None:
69 | result=dict(failed=True, msg="src (or content) and dest are required")
70 | return ReturnData(conn=conn, result=result)
71 | elif (source is not None or 'first_available_file' in inject) and content is not None:
72 | result=dict(failed=True, msg="src and content are mutually exclusive")
73 | return ReturnData(conn=conn, result=result)
74 |
75 | # Check if the source ends with a "/"
76 | source_trailing_slash = False
77 | if source:
78 | source_trailing_slash = source.endswith("/")
79 |
80 | # Define content_tempfile in case we set it after finding content populated.
81 | content_tempfile = None
82 |
83 | # If content is defined make a temp file and write the content into it.
84 | if content is not None:
85 | try:
86 | # If content comes to us as a dict it should be decoded json.
87 | # We need to encode it back into a string to write it out.
88 | if type(content) is dict:
89 | content_tempfile = self._create_content_tempfile(json.dumps(content))
90 | else:
91 | content_tempfile = self._create_content_tempfile(content)
92 | source = content_tempfile
93 | except Exception, err:
94 | result = dict(failed=True, msg="could not write content temp file: %s" % err)
95 | return ReturnData(conn=conn, result=result)
96 | # if we have first_available_file in our vars
97 | # look up the files and use the first one we find as src
98 | elif 'first_available_file' in inject:
99 | found = False
100 | for fn in inject.get('first_available_file'):
101 | fn_orig = fn
102 | fnt = template.template(self.runner.basedir, fn, inject)
103 | fnd = utils.path_dwim(self.runner.basedir, fnt)
104 | if not os.path.exists(fnd) and '_original_file' in inject:
105 | fnd = utils.path_dwim_relative(inject['_original_file'], 'files', fnt, self.runner.basedir, check=False)
106 | if os.path.exists(fnd):
107 | source = fnd
108 | found = True
109 | break
110 | if not found:
111 | results = dict(failed=True, msg="could not find src in first_available_file list")
112 | return ReturnData(conn=conn, result=results)
113 | else:
114 | source = template.template(self.runner.basedir, source, inject)
115 | if '_original_file' in inject:
116 | source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
117 | else:
118 | source = utils.path_dwim(self.runner.basedir, source)
119 |
120 | # A list of source file tuples (full_path, relative_path) which will try to copy to the destination
121 | source_files = []
122 |
123 | # If source is a directory populate our list else source is a file and translate it to a tuple.
124 | if os.path.isdir(source):
125 | # Get the amount of spaces to remove to get the relative path.
126 | if source_trailing_slash:
127 | sz = len(source) + 1
128 | else:
129 | sz = len(source.rsplit('/', 1)[0]) + 1
130 |
131 | # Walk the directory and append the file tuples to source_files.
132 | for base_path, sub_folders, files in os.walk(source):
133 | for file in files:
134 | full_path = os.path.join(base_path, file)
135 | rel_path = full_path[sz:]
136 | source_files.append((full_path, rel_path))
137 |
138 | # If it's recursive copy, destination is always a dir,
139 | # explicitly mark it so (note - copy module relies on this).
140 | if not conn.shell.path_has_trailing_slash(dest):
141 | dest = conn.shell.join_path(dest, '')
142 | else:
143 | source_files.append((source, os.path.basename(source)))
144 |
145 | changed = False
146 | diffs = []
147 | module_result = {"changed": False}
148 |
149 | # A register for if we executed a module.
150 | # Used to cut down on command calls when not recursive.
151 | module_executed = False
152 |
153 | # Tell _execute_module to delete the file if there is one file.
154 | delete_remote_tmp = (len(source_files) == 1)
155 |
156 | # If this is a recursive action create a tmp_path that we can share as the _exec_module create is too late.
157 | if not delete_remote_tmp:
158 | if "-tmp-" not in tmp_path:
159 | tmp_path = self.runner._make_tmp_path(conn)
160 |
161 | # expand any user home dir specifier
162 | dest = self.runner._remote_expand_user(conn, dest, tmp_path)
163 |
164 | vault = VaultLib(password=self.runner.vault_pass)
165 |
166 | for source_full, source_rel in source_files:
167 |
168 | vault_temp_file = None
169 | data = None
170 |
171 | try:
172 | data = open(source_full).read()
173 | except IOError:
174 | raise errors.AnsibleError("file could not read: %s" % source_full)
175 |
176 | if vault.is_encrypted(data):
177 | # if the file is encrypted and no password was specified,
178 | # the decrypt call would throw an error, but we check first
179 | # since the decrypt function doesn't know the file name
180 | if self.runner.vault_pass is None:
181 | raise errors.AnsibleError("A vault password must be specified to decrypt %s" % source_full)
182 |
183 | data = vault.decrypt(data)
184 | # Make a temp file
185 | vault_temp_file = self._create_content_tempfile(data)
186 | source_full = vault_temp_file;
187 |
188 | # Generate a hash of the local file.
189 | local_checksum = utils.checksum(source_full)
190 |
191 | # If local_checksum is not defined we can't find the file so we should fail out.
192 | if local_checksum is None:
193 | result = dict(failed=True, msg="could not find src=%s" % source_full)
194 | return ReturnData(conn=conn, result=result)
195 |
196 | # This is kind of optimization - if user told us destination is
197 | # dir, do path manipulation right away, otherwise we still check
198 | # for dest being a dir via remote call below.
199 | if conn.shell.path_has_trailing_slash(dest):
200 | dest_file = conn.shell.join_path(dest, source_rel)
201 | else:
202 | dest_file = conn.shell.join_path(dest)
203 |
204 | # Attempt to get the remote checksum
205 | remote_checksum = self.runner._remote_checksum(conn, tmp_path, dest_file, inject)
206 |
207 | if remote_checksum == '3':
208 | # The remote_checksum was executed on a directory.
209 | if content is not None:
210 | # If source was defined as content remove the temporary file and fail out.
211 | self._remove_tempfile_if_content_defined(content, content_tempfile)
212 | result = dict(failed=True, msg="can not use content with a dir as dest")
213 | return ReturnData(conn=conn, result=result)
214 | else:
215 | # Append the relative source location to the destination and retry remote_checksum
216 | dest_file = conn.shell.join_path(dest, source_rel)
217 | remote_checksum = self.runner._remote_checksum(conn, tmp_path, dest_file, inject)
218 |
219 | if remote_checksum == '4':
220 | result = dict(msg="python isn't present on the system. Unable to compute checksum", failed=True)
221 | return ReturnData(conn=conn, result=result)
222 |
223 | if remote_checksum != '1' and not force:
224 | # remote_file exists so continue to next iteration.
225 | continue
226 |
227 | if local_checksum != remote_checksum:
228 | # The checksums don't match and we will change or error out.
229 | changed = True
230 |
231 | # Create a tmp_path if missing only if this is not recursive.
232 | # If this is recursive we already have a tmp_path.
233 | if delete_remote_tmp:
234 | if "-tmp-" not in tmp_path:
235 | tmp_path = self.runner._make_tmp_path(conn)
236 |
237 | if self.runner.diff and not raw:
238 | diff = self._get_diff_data(conn, tmp_path, inject, dest_file, source_full)
239 | else:
240 | diff = {}
241 |
242 | if self.runner.noop_on_check(inject):
243 | self._remove_tempfile_if_content_defined(content, content_tempfile)
244 | diffs.append(diff)
245 | changed = True
246 | module_result = dict(changed=True)
247 | continue
248 |
249 | # Define a remote directory that we will copy the file to.
250 | tmp_src = tmp_path + 'source'
251 |
252 | if not raw:
253 | conn.put_file(source_full, tmp_src)
254 | else:
255 | conn.put_file(source_full, dest_file)
256 |
257 | # We have copied the file remotely and no longer require our content_tempfile
258 | self._remove_tempfile_if_content_defined(content, content_tempfile)
259 |
260 | # Remove the vault tempfile if we have one
261 | if vault_temp_file:
262 | os.remove(vault_temp_file);
263 | vault_temp_file = None
264 |
265 | # fix file permissions when the copy is done as a different user
266 | if self.runner.become and self.runner.become_user != 'root' and not raw:
267 | self.runner._remote_chmod(conn, 'a+r', tmp_src, tmp_path)
268 |
269 | if raw:
270 | # Continue to next iteration if raw is defined.
271 | continue
272 |
273 | # Run the copy module
274 |
275 | # src and dest here come after original and override them
276 | # we pass dest only to make sure it includes trailing slash in case of recursive copy
277 | new_module_args = dict(
278 | src=tmp_src,
279 | dest=dest,
280 | original_basename=source_rel
281 | )
282 | if self.runner.noop_on_check(inject):
283 | new_module_args['CHECKMODE'] = True
284 | if self.runner.no_log:
285 | new_module_args['NO_LOG'] = True
286 |
287 | module_args_tmp = utils.merge_module_args(module_args, new_module_args)
288 |
289 | module_return = self.runner._execute_module(conn, tmp_path, 'copy', module_args_tmp, inject=inject, complex_args=complex_args, delete_remote_tmp=delete_remote_tmp)
290 | module_executed = True
291 |
292 | else:
293 | # no need to transfer the file, already correct hash, but still need to call
294 | # the file module in case we want to change attributes
295 | self._remove_tempfile_if_content_defined(content, content_tempfile)
296 |
297 | # Remove the vault tempfile if we have one
298 | if vault_temp_file:
299 | os.remove(vault_temp_file);
300 | vault_temp_file = None
301 |
302 | if raw:
303 | # Continue to next iteration if raw is defined.
304 | # self.runner._remove_tmp_path(conn, tmp_path)
305 | continue
306 |
307 | tmp_src = tmp_path + source_rel
308 |
309 | # Build temporary module_args.
310 | new_module_args = dict(
311 | src=tmp_src,
312 | dest=dest,
313 | original_basename=source_rel
314 | )
315 | if self.runner.noop_on_check(inject):
316 | new_module_args['CHECKMODE'] = True
317 | if self.runner.no_log:
318 | new_module_args['NO_LOG'] = True
319 |
320 | module_args_tmp = utils.merge_module_args(module_args, new_module_args)
321 |
322 | # Execute the file module.
323 | module_return = self.runner._execute_module(conn, tmp_path, 'file', module_args_tmp, inject=inject, complex_args=complex_args, delete_remote_tmp=delete_remote_tmp)
324 | module_executed = True
325 |
326 | module_result = module_return.result
327 | if not module_result.get('checksum'):
328 | module_result['checksum'] = local_checksum
329 | if module_result.get('failed') == True:
330 | return module_return
331 | if module_result.get('changed') == True:
332 | changed = True
333 |
334 | # Delete tmp_path if we were recursive or if we did not execute a module.
335 | if (not C.DEFAULT_KEEP_REMOTE_FILES and not delete_remote_tmp) \
336 | or (not C.DEFAULT_KEEP_REMOTE_FILES and delete_remote_tmp and not module_executed):
337 | self.runner._remove_tmp_path(conn, tmp_path)
338 |
339 | # the file module returns the file path as 'path', but
340 | # the copy module uses 'dest', so add it if it's not there
341 | if 'path' in module_result and 'dest' not in module_result:
342 | module_result['dest'] = module_result['path']
343 |
344 | # TODO: Support detailed status/diff for multiple files
345 | if len(source_files) == 1:
346 | result = module_result
347 | else:
348 | result = dict(dest=dest, src=source, changed=changed)
349 | if len(diffs) == 1:
350 | return ReturnData(conn=conn, result=result, diff=diffs[0])
351 | else:
352 | return ReturnData(conn=conn, result=result)
353 |
354 | def _create_content_tempfile(self, content):
355 | ''' Create a tempfile containing defined content '''
356 | fd, content_tempfile = tempfile.mkstemp()
357 | f = os.fdopen(fd, 'w')
358 | try:
359 | f.write(content)
360 | except Exception, err:
361 | os.remove(content_tempfile)
362 | raise Exception(err)
363 | finally:
364 | f.close()
365 | return content_tempfile
366 |
367 | def _get_diff_data(self, conn, tmp, inject, destination, source):
368 | peek_result = self.runner._execute_module(conn, tmp, 'file', "path=%s diff_peek=1" % destination, inject=inject, persist_files=True)
369 |
370 | if not peek_result.is_successful():
371 | return {}
372 |
373 | diff = {}
374 | if peek_result.result['state'] == 'absent':
375 | diff['before'] = ''
376 | elif peek_result.result['appears_binary']:
377 | diff['dst_binary'] = 1
378 | elif peek_result.result['size'] > utils.MAX_FILE_SIZE_FOR_DIFF:
379 | diff['dst_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
380 | else:
381 | dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % destination, inject=inject, persist_files=True)
382 | if 'content' in dest_result.result:
383 | dest_contents = dest_result.result['content']
384 | if dest_result.result['encoding'] == 'base64':
385 | dest_contents = base64.b64decode(dest_contents)
386 | else:
387 | raise Exception("unknown encoding, failed: %s" % dest_result.result)
388 | diff['before_header'] = destination
389 | diff['before'] = dest_contents
390 |
391 | src = open(source)
392 | src_contents = src.read(8192)
393 | st = os.stat(source)
394 | if "\x00" in src_contents:
395 | diff['src_binary'] = 1
396 | elif st[stat.ST_SIZE] > utils.MAX_FILE_SIZE_FOR_DIFF:
397 | diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
398 | else:
399 | src.seek(0)
400 | diff['after_header'] = source
401 | diff['after'] = src.read()
402 |
403 | return diff
404 |
405 | def _remove_tempfile_if_content_defined(self, content, content_tempfile):
406 | if content is not None:
407 | os.remove(content_tempfile)
408 |
409 |
410 | def _result_key_merge(self, options, results):
411 | # add keys to file module results to mimic copy
412 | if 'path' in results.result and 'dest' not in results.result:
413 | results.result['dest'] = results.result['path']
414 | del results.result['path']
415 | return results
416 |
--------------------------------------------------------------------------------
/hosts:
--------------------------------------------------------------------------------
1 | [test_hosts]
2 | localhost
3 |
4 |
--------------------------------------------------------------------------------
/library/copyv.py:
--------------------------------------------------------------------------------
1 | # This file intentionally blank. Allows the action plugin of the same name to be used as a module name.
2 |
--------------------------------------------------------------------------------
/notasecret.txt:
--------------------------------------------------------------------------------
1 | Not a secret file.
2 |
--------------------------------------------------------------------------------
/secret.txt:
--------------------------------------------------------------------------------
1 | $ANSIBLE_VAULT;1.1;AES256
2 | 63363265623130336336316236646166323065313835663962643739373262613730313536633966
3 | 3130663964623132363963303663383535366639653535350a396662623239306337356264333764
4 | 61346666343236386334656265356334383233376136663634666339623863666166386561643562
5 | 3665646231303163650a646230356135383263383563396233613630396665613433356162316562
6 | 62646231393439386235393838373862643563333030396231386233656138343063
7 |
--------------------------------------------------------------------------------
/test.yml:
--------------------------------------------------------------------------------
1 | - hosts: test_hosts
2 | name: Test Copy
3 | gather_facts: no
4 | tasks:
5 | - name: Copy Secret File
6 | copyv: src="secret.txt" dest="/tmp/"
7 |
8 | - name: Copy File
9 | copyv: src="notasecret.txt" dest="/tmp/"
10 |
--------------------------------------------------------------------------------