Commit bf916fb5 by root Committed by Jon Hawkesworth

Adding first pass at win_copy, win_file and win_template modules.

parent bfe08560
......@@ -142,3 +142,25 @@ Function ConvertTo-Bool
return
}
# Helper function to calculate md5 of a file in a way which powershell 3
# and above can handle:
Function Get-FileMd5($path)
{
$hash = ""
If (Test-Path -PathType Leaf $path)
{
$sp = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
$fp = [System.IO.File]::Open($path, [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read);
[System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower();
$fp.Dispose();
}
ElseIf (Test-Path -PathType Container $path)
{
$hash= "3";
}
Else
{
$hash = "1";
}
return $hash
}
Subproject commit e1f90635af0e9ca09449fe47f94471bf9e4ffa5d
Subproject commit 08c5cc06c6ad9a1e0016ad89eb0f7ca009cc8108
Subproject commit b8071a8d5eebe405250774a0b7c6c74451bc9532
Subproject commit 317654dba5cae905b5d6eed78f5c6c6984cc2f02
......@@ -127,13 +127,13 @@ class ActionModule(object):
elif remote_checksum == '2':
result = dict(msg="no read permission on remote file, not transferring, ignored", file=source, changed=False)
elif remote_checksum == '3':
result = dict(msg="remote file is a directory, fetch cannot work on directories", file=source, changed=False)
result = dict(failed=True, msg="remote file is a directory, fetch cannot work on directories", file=source, changed=False)
elif remote_checksum == '4':
result = dict(msg="python isn't present on the system. Unable to compute checksum", file=source, changed=False)
return ReturnData(conn=conn, result=result)
# calculate checksum for the local file
local_checksum = utils.checksum(dest)
local_checksum = utils.md5(dest)
if remote_checksum != local_checksum:
# create the containing directories, if needed
......@@ -147,7 +147,8 @@ class ActionModule(object):
f = open(dest, 'w')
f.write(remote_data)
f.close()
new_checksum = utils.secure_hash(dest)
new_checksum = utils.md5(dest)
# new_checksum = utils.secure_hash(dest)
# For backwards compatibility. We'll return None on FIPS enabled
# systems
try:
......
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import pipes
from ansible.utils import template
from ansible import utils
from ansible import errors
from ansible.runner.return_data import ReturnData
import base64
class ActionModule(object):
TRANSFERS_FILES = True
def __init__(self, runner):
self.runner = runner
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
''' handler for template operations '''
if not self.runner.is_playbook:
raise errors.AnsibleError("in current versions of ansible, templates are only usable in playbooks")
# load up options
options = {}
if complex_args:
options.update(complex_args)
options.update(utils.parse_kv(module_args))
source = options.get('src', None)
dest = options.get('dest', None)
if (source is None and 'first_available_file' not in inject) or dest is None:
result = dict(failed=True, msg="src and dest are required")
return ReturnData(conn=conn, comm_ok=False, result=result)
# if we have first_available_file in our vars
# look up the files and use the first one we find as src
if 'first_available_file' in inject:
found = False
for fn in self.runner.module_vars.get('first_available_file'):
fn_orig = fn
fnt = template.template(self.runner.basedir, fn, inject)
fnd = utils.path_dwim(self.runner.basedir, fnt)
if not os.path.exists(fnd) and '_original_file' in inject:
fnd = utils.path_dwim_relative(inject['_original_file'], 'templates', fnt, self.runner.basedir, check=False)
if os.path.exists(fnd):
source = fnd
found = True
break
if not found:
result = dict(failed=True, msg="could not find src in first_available_file list")
return ReturnData(conn=conn, comm_ok=False, result=result)
else:
source = template.template(self.runner.basedir, source, inject)
if '_original_file' in inject:
source = utils.path_dwim_relative(inject['_original_file'], 'templates', source, self.runner.basedir)
else:
source = utils.path_dwim(self.runner.basedir, source)
if dest.endswith("\\"): # TODO: Check that this fixes the path for Windows hosts.
base = os.path.basename(source)
dest = os.path.join(dest, base)
# template the source data locally & get ready to transfer
try:
resultant = template.template_from_file(self.runner.basedir, source, inject, vault_password=self.runner.vault_pass)
except Exception, e:
result = dict(failed=True, msg=type(e).__name__ + ": " + str(e))
return ReturnData(conn=conn, comm_ok=False, result=result)
local_checksum = utils.checksum_s(resultant)
remote_checksum = self.runner._remote_checksum(conn, tmp, dest, inject)
if local_checksum != remote_checksum:
# template is different from the remote value
# if showing diffs, we need to get the remote value
dest_contents = ''
if self.runner.diff:
# using persist_files to keep the temp directory around to avoid needing to grab another
dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True)
if 'content' in dest_result.result:
dest_contents = dest_result.result['content']
if dest_result.result['encoding'] == 'base64':
dest_contents = base64.b64decode(dest_contents)
else:
raise Exception("unknown encoding, failed: %s" % dest_result.result)
xfered = self.runner._transfer_str(conn, tmp, 'source', resultant)
# fix file permissions when the copy is done as a different user
if self.runner.sudo and self.runner.sudo_user != 'root' or self.runner.su and self.runner.su_user != 'root':
self.runner._remote_chmod(conn, 'a+r', xfered, tmp)
# run the copy module
new_module_args = dict(
src=xfered,
dest=dest,
original_basename=os.path.basename(source),
follow=True,
)
module_args_tmp = utils.merge_module_args(module_args, new_module_args)
if self.runner.noop_on_check(inject):
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))
else:
res = self.runner._execute_module(conn, tmp, 'win_copy', module_args_tmp, inject=inject, complex_args=complex_args)
if res.result.get('changed', False):
res.diff = dict(before=dest_contents, after=resultant)
return res
else:
# when running the file module based on the template data, we do
# not want the source filename (the name of the template) to be used,
# since this would mess up links, so we clear the src param and tell
# the module to follow links
new_module_args = dict(
src=None,
follow=True,
)
# be sure to inject the check mode param into the module args and
# rely on the file module to report its changed status
if self.runner.noop_on_check(inject):
new_module_args['CHECKMODE'] = True
module_args = utils.merge_module_args(module_args, new_module_args)
return self.runner._execute_module(conn, tmp, 'win_file', module_args, inject=inject, complex_args=complex_args)
---
win_output_dir: 'C:/temp/'
output_dir: ~/ansible_testing
non_root_test_user: ansible
pip_test_package: epdb
......
# test code for the windows versions of copy, file and template module
# originally
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: clean out the test directory
win_file: name={{win_output_dir|mandatory}} state=absent
tags:
- prepare
- name: create the test directory
win_file: name={{win_output_dir}} state=directory
tags:
- prepare
# test code for the copy module and action plugin
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: record the output directory
set_fact: output_file={{win_output_dir}}/foo.txt
- name: initiate a basic copy
#- name: initiate a basic copy, and also test the mode
# win_copy: src=foo.txt dest={{output_file}} mode=0444
win_copy: src=foo.txt dest={{output_file}}
register: copy_result
- debug: var=copy_result
#- name: check the presence of the output file
- name: check the mode of the output file
win_file: name={{output_file}} state=file
register: file_result_check
- debug: var=file_result_check
#- name: assert the mode is correct
# assert:
# that:
# - "file_result_check.mode == '0444'"
- name: assert basic copy worked
assert:
that:
- "'changed' in copy_result"
# - "'dest' in copy_result"
# - "'group' in copy_result"
# - "'gid' in copy_result"
- "'checksum' in copy_result"
# - "'owner' in copy_result"
# - "'size' in copy_result"
# - "'src' in copy_result"
# - "'state' in copy_result"
# - "'uid' in copy_result"
- name: verify that the file was marked as changed
assert:
that:
- "copy_result.changed == true"
- name: verify that the file checksum is correct
assert:
that:
- "copy_result.checksum[0] == 'c47397529fe81ab62ba3f85e9f4c71f2'"
- name: check the stat results of the file
win_stat: path={{output_file}}
register: stat_results
- name: assert the stat results are correct
assert:
that:
- "stat_results.stat.exists == true"
# - "stat_results.stat.isblk == false"
# - "stat_results.stat.isfifo == false"
# - "stat_results.stat.isreg == true"
# - "stat_results.stat.issock == false"
- "stat_results.stat.md5[0] == 'c47397529fe81ab62ba3f85e9f4c71f2'"
- name: overwrite the file via same means
win_copy: src=foo.txt dest={{output_file}}
register: copy_result2
- name: assert that the file was not changed
assert:
that:
- "not copy_result2|changed"
# content system not available in win_copy right now
#- name: overwrite the file using the content system
# win_copy: content="modified" dest={{output_file}}
# register: copy_result3
#
#- name: assert that the file has changed
# assert:
# that:
# - "copy_result3|changed"
# - "'content' not in copy_result3"
# test recursive copy
- name: set the output subdirectory
set_fact: output_subdir={{win_output_dir}}/sub/
- name: make an output subdirectory
win_file: name={{output_subdir}} state=directory
- name: test recursive copy to directory
# win_copy: src=subdir dest={{output_subdir}} directory_mode=0700
win_copy: src=subdir dest={{output_subdir}}
register: recursive_copy_result
- debug: var=recursive_copy_result
- name: check that a file in a directory was transferred
win_stat: path={{win_output_dir}}/sub/subdir/bar.txt
register: stat_bar
- name: check that a file in a deeper directory was transferred
win_stat: path={{win_output_dir}}/sub/subdir/subdir2/baz.txt
register: stat_bar2
- name: check that a file in a directory whose parent contains a directory alone was transferred
win_stat: path={{win_output_dir}}/sub/subdir/subdir2/subdir3/subdir4/qux.txt
register: stat_bar3
- name: assert recursive copy things
assert:
that:
- "stat_bar.stat.exists"
- "stat_bar2.stat.exists"
- "stat_bar3.stat.exists"
- name: stat the recursively copied directories
win_stat: path={{win_output_dir}}/sub/{{item}}
register: dir_stats
with_items:
- "subdir"
- "subdir/subdir2"
- "subdir/subdir2/subdir3"
- "subdir/subdir2/subdir3/subdir4"
# can't check file mode on windows so commenting this one out.
#- name: assert recursive copied directories mode
# assert:
# that:
# - "{{item.stat.mode}} == 0700"
# with_items: dir_stats.results
# errors on this aren't presently ignored so this test is commented out. But it would be nice to fix.
#
# content param not available in win_copy
#- name: overwrite the file again using the content system, also passing along file params
# win_copy: content="modified" dest={{output_file}}
# register: copy_result4
#- name: assert invalid copy input location fails
# win_copy: src=invalid_file_location_does_not_exist dest={{win_output_dir}}/file.txt
# ignore_errors: True
# register: failed_copy
# owner not available in win_copy, commenting out
#- name: copy already copied directory again
# win_copy: src=subdir dest={{output_subdir | expanduser}} owner={{ansible_ssh_user}}
# register: copy_result5
#- name: assert that the directory was not changed
# assert:
# that:
# - "not copy_result5|changed"
# content not available in win_copy, commenting out.
# issue 8394
#- name: create a file with content and a literal multiline block
# win_copy: |
# content='this is the first line
# this is the second line
#
# this line is after an empty line
# this line is the last line
# '
# dest={{win_output_dir}}/multiline.txt
# register: copy_result6
#- debug: var=copy_result6
#- name: assert the multiline file was created correctly
# assert:
# that:
# - "copy_result6.changed"
# - "copy_result6.dest == '{{win_output_dir|expanduser}}/multiline.txt'"
# - "copy_result6.checksum == '1627d51e7e607c92cf1a502bf0c6cce3'"
# test overwriting a file as an unprivileged user (pull request #8624)
# this can't be relative to {{win_output_dir}} as ~root usually has mode 700
#- name: create world writable directory
#win_file: dest=/tmp/worldwritable state=directory mode=0777
#- name: create world writable file
# win_copy: dest=/tmp/worldwritable/file.txt content="bar" mode=0666
#- name: overwrite the file as user nobody
# win_copy: dest=/tmp/worldwritable/file.txt content="baz"
# sudo: yes
# sudo_user: nobody
# register: copy_result7
#- name: assert the file was overwritten
# assert:
# that:
# - "copy_result7.changed"
# - "copy_result7.dest == '/tmp/worldwritable/file.txt'"
# - "copy_result7.checksum == '73feffa4b7f6bb68e44cf984c85f6e88'"
#- name: clean up
# win_file: dest=/tmp/worldwritable state=absent
# test overwritting a link using "follow=yes" so that the link
# is preserved and the link target is updated
#- name: create a test file to symlink to
# win_copy: dest={{win_output_dir}}/follow_test content="this is the follow test file\n"
#
#- name: create a symlink to the test file
# win_file: path={{win_output_dir}}/follow_link src='./follow_test' state=link
#
#- name: update the test file using follow=True to preserve the link
# win_copy: dest={{win_output_dir}}/follow_link content="this is the new content\n" follow=yes
# register: replace_follow_result
#- name: stat the link path
# win_stat: path={{win_output_dir}}/follow_link
# register: stat_link_result
#
#- name: assert that the link is still a link
# assert:
# that:
# - stat_link_result.stat.islnk
#
#- name: get the md5 of the link target
# shell: checksum {{win_output_dir}}/follow_test | cut -f1 -sd ' '
# register: target_file_result
#- name: assert that the link target was updated
# assert:
# that:
# - replace_follow_result.checksum == target_file_result.stdout
- name: clean up sub
win_file: path={{win_output_dir}}/sub state=absent
- name: clean up foo.txt
win_file: path={{win_output_dir}}/foo.txt state=absent
# test code for the template module
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: fill in a basic template
# win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated mode=0644
win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated
register: template_result
- assert:
that:
- "'changed' in template_result"
# - "'dest' in template_result"
# - "'group' in template_result"
# - "'gid' in template_result"
# - "'checksum' in template_result"
# - "'owner' in template_result"
# - "'size' in template_result"
# - "'src' in template_result"
# - "'state' in template_result"
# - "'uid' in template_result"
- name: verify that the file was marked as changed
assert:
that:
- "template_result.changed == true"
# VERIFY CONTENTS
- name: copy known good into place
win_copy: src=foo.txt dest={{win_output_dir}}\foo.txt
- name: compare templated file to known good
raw: fc.exe {{win_output_dir}}\foo.templated {{win_output_dir}}\foo.txt
register: diff_result
- debug: var=diff_result
- name: verify templated file matches known good
assert:
that:
# - 'diff_result.stdout == ""'
- 'diff_result.stdout_lines[1] == "FC: no differences encountered"'
- "diff_result.rc == 0"
# VERIFY MODE
# can't set file mode on windows so commenting this test out
#- name: set file mode
# win_file: path={{win_output_dir}}/foo.templated mode=0644
# register: file_result
#- name: ensure file mode did not change
# assert:
# that:
# - "file_result.changed != True"
# commenting out all the following tests as expanduser and file modes not windows concepts.
# VERIFY dest as a directory does not break file attributes
# Note: expanduser is needed to go down the particular codepath that was broken before
#- name: setup directory for test
# win_file: state=directory dest={{win_output_dir | expanduser}}/template-dir mode=0755 owner=nobody group=root
#- name: set file mode when the destination is a directory
# win_template: src=foo.j2 dest={{win_output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root
#- name: set file mode when the destination is a directory
# win_template: src=foo.j2 dest={{win_output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root
# register: file_result
#
#- name: check that the file has the correct attributes
# win_stat: path={{win_output_dir | expanduser}}/template-dir/foo.j2
# register: file_attrs
#
#- assert:
# that:
# - "file_attrs.stat.uid == 0"
# - "file_attrs.stat.pw_name == 'root'"
# - "file_attrs.stat.mode == '0600'"
#
#- name: check that the containing directory did not change attributes
# win_stat: path={{win_output_dir | expanduser}}/template-dir/
# register: dir_attrs
#
#- assert:
# that:
# - "dir_attrs.stat.uid != 0"
# - "dir_attrs.stat.pw_name == 'nobody'"
# - "dir_attrs.stat.mode == '0755'"
......@@ -30,3 +30,6 @@
- { role: test_win_msi, tags: test_win_msi }
- { role: test_win_service, tags: test_win_service }
- { role: test_win_feature, tags: test_win_feature }
- { role: test_win_file, tags: test_win_file }
- { role: test_win_copy, tags: test_win_copy }
- { role: test_win_template, tags: test_win_template }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment