win_copy: added remote and content options (#21546)
* win_copy: added remote and content options * readded comment about original_basename accidentally removed
This commit is contained in:
parent
17a39e88a5
commit
778dc9ad38
3 changed files with 588 additions and 273 deletions
|
@ -24,77 +24,136 @@ $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "b
|
||||||
$src = Get-AnsibleParam -obj $params -name "src" -type "path" -failifempty $true
|
$src = Get-AnsibleParam -obj $params -name "src" -type "path" -failifempty $true
|
||||||
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true
|
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true
|
||||||
$force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $true
|
$force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $true
|
||||||
$original_basename = Get-AnsibleParam -obj $params -name "original_basename" -type "str" -failifempty $true
|
$original_basename = Get-AnsibleParam -obj $params -name "original_basename" -type "str"
|
||||||
|
|
||||||
$result = @{
|
|
||||||
changed = $FALSE
|
|
||||||
original_basename = $original_basename
|
|
||||||
src = $src
|
|
||||||
dest = $dest
|
|
||||||
}
|
|
||||||
|
|
||||||
# original_basename gets set if src and dest are dirs
|
# original_basename gets set if src and dest are dirs
|
||||||
# but includes subdir if the source folder contains sub folders
|
# but includes subdir if the source folder contains sub folders
|
||||||
# e.g. you could get subdir/foo.txt
|
# e.g. you could get subdir/foo.txt
|
||||||
|
|
||||||
|
$result = @{
|
||||||
|
changed = $false
|
||||||
|
src = $src
|
||||||
|
dest = $dest
|
||||||
|
}
|
||||||
|
|
||||||
if (($force -eq $false) -and (Test-Path -Path $dest)) {
|
if (($force -eq $false) -and (Test-Path -Path $dest)) {
|
||||||
$result.msg = "file already exists"
|
$result.msg = "file already exists"
|
||||||
Exit-Json $result
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
|
|
||||||
# detect if doing recursive folder copy and create any non-existent destination sub folder
|
Function Copy-Folder($src, $dest) {
|
||||||
$parent = Split-Path -Path $original_basename -Parent
|
if (Test-Path -Path $dest) {
|
||||||
if ($parent.length -gt 0)
|
if (-not (Get-Item -Path $dest -Force).PSIsContainer) {
|
||||||
{
|
Fail-Json $result "If src is a folder, dest must also be a folder. src: $src, dest: $dest"
|
||||||
$dest_folder = Join-Path $dest $parent
|
}
|
||||||
New-Item -Path $dest_folder -Type Directory -Force -WhatIf:$check_mode
|
} else {
|
||||||
|
try {
|
||||||
|
New-Item -Path $dest -ItemType Directory -Force -WhatIf:$check_mode
|
||||||
|
$result.changed = $true
|
||||||
|
} catch {
|
||||||
|
Fail-Json $result "Failed to create new folder $dest $($_.Exception.Message)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# if $dest is a dir, append $original_basename so the file gets copied with its intended name.
|
foreach ($item in Get-ChildItem -Path $src) {
|
||||||
if (Test-Path -Path $dest -PathType Container)
|
$dest_path = Join-Path -Path $dest -ChildPath $item.PSChildName
|
||||||
{
|
if ($item.PSIsContainer) {
|
||||||
$dest = Join-Path -Path $dest -ChildPath $original_basename
|
Copy-Folder -src $item.FullName -dest $dest_path
|
||||||
|
} else {
|
||||||
|
Copy-File -src $item.FullName -dest $dest_path
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$orig_checksum = Get-FileChecksum ($dest)
|
Function Copy-File($src, $dest) {
|
||||||
$src_checksum = Get-FileChecksum ($src)
|
if (Test-Path -Path $dest) {
|
||||||
|
if ((Get-Item -Path $dest -Force).PSIsContainer) {
|
||||||
|
Fail-Json $result "If src is a file, dest must also be a file. src: $src, dest: $dest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
If ($src_checksum.Equals($orig_checksum))
|
$src_checksum = Get-FileChecksum -Path $src
|
||||||
{
|
$dest_checksum = Get-FileChecksum -Path $dest
|
||||||
# if both are "3" then both are folders, ok to copy
|
if ($src_checksum -ne $dest_checksum) {
|
||||||
If ($src_checksum.Equals("3"))
|
try {
|
||||||
{
|
|
||||||
# New-Item -Force creates subdirs for recursive copies
|
|
||||||
New-Item -Path $dest -Type File -Force -WhatIf:$check_mode
|
|
||||||
Copy-Item -Path $src -Destination $dest -Force -WhatIf:$check_mode
|
Copy-Item -Path $src -Destination $dest -Force -WhatIf:$check_mode
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
|
} catch {
|
||||||
|
Fail-Json $result "Failed to copy file $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verify the file we copied is the same
|
||||||
|
$dest_checksum_verify = Get-FileChecksum -Path $dest
|
||||||
|
if (-not ($check_mode) -and ($src_checksum -ne $dest_checksum_verify)) {
|
||||||
|
Fail-Json $result "Copied file does not match checksum. src: $src_checksum, dest: $dest_checksum_verify. Failed to copy file from $src to $dest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Function Get-FileSize($path) {
|
||||||
|
$file = Get-Item -Path $path -Force
|
||||||
|
$size = $null
|
||||||
|
if ($file.PSIsContainer) {
|
||||||
|
$dir_files_sum = Get-ChildItem $file.FullName -Recurse
|
||||||
|
if ($dir_files_sum -eq $null -or ($dir_files_sum.PSObject.Properties.name -contains 'length' -eq $false)) {
|
||||||
|
$size = 0
|
||||||
|
} else {
|
||||||
|
$size = ($dir_files_sum | Measure-Object -property length -sum).Sum
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$size = $file.Length
|
||||||
|
}
|
||||||
|
|
||||||
|
$size
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not (Test-Path -Path $src)) {
|
||||||
|
Fail-Json $result "Cannot copy src file: $src as it does not exist"
|
||||||
|
}
|
||||||
|
|
||||||
|
# If copying from remote we need to get the original folder path and name and change dest to this path
|
||||||
|
if ($original_basename) {
|
||||||
|
$parent_path = Split-Path -Path $original_basename -Parent
|
||||||
|
if ($parent_path.length -gt 0) {
|
||||||
|
$dest_folder = Join-Path -Path $dest -ChildPath $parent_path
|
||||||
|
try {
|
||||||
|
New-Item -Path $dest_folder -Type directory -Force -WhatIf:$check_mode
|
||||||
|
$result.changed = $true
|
||||||
|
} catch {
|
||||||
|
Fail-Json $result "Failed to create directory $($dest_folder): $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
$dest = Join-Path $dest -ChildPath $original_basename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# If the source is a container prepare for some recursive magic
|
||||||
|
if ((Get-Item -Path $src -Force).PSIsContainer) {
|
||||||
|
if (Test-Path -Path $dest) {
|
||||||
|
if (-not (Get-Item -Path $dest -Force).PSIsContainer) {
|
||||||
|
Fail-Json $result "If src is a folder, dest must also be a folder. src: $src, dest: $dest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$folder_name = (Get-Item -Path $src -Force).Name
|
||||||
|
$dest_path = Join-Path -Path $dest -ChildPath $folder_name
|
||||||
|
Copy-Folder -src $src -dest $dest_path
|
||||||
|
if ($result.changed -eq $true) {
|
||||||
$result.operation = "folder_copy"
|
$result.operation = "folder_copy"
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
}
|
Copy-File -src $src -dest $dest
|
||||||
ElseIf (-Not $src_checksum.Equals($orig_checksum))
|
if ($result.changed -eq $true) {
|
||||||
{
|
|
||||||
If ($src_checksum.Equals("3"))
|
|
||||||
{
|
|
||||||
Fail-Json $result "If src is a folder, dest must also be a folder"
|
|
||||||
}
|
|
||||||
# The checksums don't match, there's something to do
|
|
||||||
Copy-Item -Path $src -Destination $dest -Force -WhatIf:$check_mode
|
|
||||||
|
|
||||||
$result.changed = $true
|
|
||||||
$result.operation = "file_copy"
|
$result.operation = "file_copy"
|
||||||
}
|
}
|
||||||
|
$result.original_basename = (Get-Item -Path $src -Force).Name
|
||||||
# Verify before we return that the file has changed
|
$result.checksum = Get-FileChecksum -Path $src
|
||||||
$dest_checksum = Get-FileChecksum($dest)
|
|
||||||
If (-Not $src_checksum.Equals($dest_checksum) -And -Not $check_mode)
|
|
||||||
{
|
|
||||||
Fail-Json $result "src checksum $src_checksum did not match dest_checksum $dest_checksum, failed to place file $original_basename in $dest"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$info = Get-Item $dest
|
if ($check_mode) {
|
||||||
$result.size = $info.Length
|
# When in check mode the dest won't exit, just get the source size
|
||||||
$result.src = $src
|
$result.size = Get-FileSize -path $src
|
||||||
$result.dest = $dest
|
} else {
|
||||||
|
$result.size = Get-FileSize -path $dest
|
||||||
|
}
|
||||||
|
|
||||||
Exit-Json $result
|
Exit-Json $result
|
||||||
|
|
|
@ -31,28 +31,46 @@ short_description: Copies files to remote locations on windows hosts.
|
||||||
description:
|
description:
|
||||||
- The C(win_copy) module copies a file on the local box to remote windows locations.
|
- The C(win_copy) module copies a file on the local box to remote windows locations.
|
||||||
options:
|
options:
|
||||||
src:
|
content:
|
||||||
description:
|
description:
|
||||||
- Local path to a file to copy to the remote server; can be absolute or relative.
|
- When used instead of C(src), sets the contents of a file directly to the
|
||||||
If path is a directory, it is copied recursively. In this case, if path ends
|
specified value. This is for simple values, for anything complex or with
|
||||||
with "/", only inside contents of that directory are copied to destination.
|
formatting please switch to the template module.
|
||||||
Otherwise, if it does not end with "/", the directory itself with all contents
|
version_added: "2.3"
|
||||||
is copied. This behavior is similar to Rsync.
|
|
||||||
required: true
|
|
||||||
dest:
|
dest:
|
||||||
description:
|
description:
|
||||||
- Remote absolute path where the file should be copied to. If src is a directory,
|
- Remote absolute path where the file should be copied to. If src is a
|
||||||
this must be a directory too. Use \\ for path separators.
|
directory, this must be a directory too.
|
||||||
|
- Use \ for path separators or \\ when in "double quotes".
|
||||||
required: true
|
required: true
|
||||||
force:
|
force:
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
description:
|
description:
|
||||||
- If set to C(yes), the remote file will be replaced when content is different than the source.
|
- If set to C(yes), the remote file will be replaced when content is
|
||||||
- If set to C(no), the remote file will only be transferred if the destination does not exist.
|
different than the source.
|
||||||
|
- If set to C(no), the remote file will only be transferred if the
|
||||||
|
destination does not exist.
|
||||||
default: True
|
default: True
|
||||||
choices:
|
choices:
|
||||||
- yes
|
- yes
|
||||||
- no
|
- no
|
||||||
|
remote_src:
|
||||||
|
description:
|
||||||
|
- If False, it will search for src at originating/master machine, if True
|
||||||
|
it will go to the remote/target machine for the src.
|
||||||
|
default: False
|
||||||
|
choices:
|
||||||
|
- True
|
||||||
|
- False
|
||||||
|
version_added: "2.3"
|
||||||
|
src:
|
||||||
|
description:
|
||||||
|
- Local path to a file to copy to the remote server; can be absolute or
|
||||||
|
relative. If path is a directory, it is copied recursively. In this case,
|
||||||
|
if path ends with "/", only inside contents of that directory are copied
|
||||||
|
to destination. Otherwise, if it does not end with "/", the directory
|
||||||
|
itself with all contents is copied. This behavior is similar to Rsync.
|
||||||
|
required: true
|
||||||
author: "Jon Hawkesworth (@jhawkesworth)"
|
author: "Jon Hawkesworth (@jhawkesworth)"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -60,13 +78,27 @@ EXAMPLES = r'''
|
||||||
- name: Copy a single file
|
- name: Copy a single file
|
||||||
win_copy:
|
win_copy:
|
||||||
src: /srv/myfiles/foo.conf
|
src: /srv/myfiles/foo.conf
|
||||||
dest: C:\Temp\foo.conf
|
dest: c:\Temp\foo.conf
|
||||||
|
|
||||||
- name: Copy files/temp_files to c:\temp
|
- name: Copy files/temp_files to c:\temp
|
||||||
win_copy:
|
win_copy:
|
||||||
src: files/temp_files/
|
src: files/temp_files/
|
||||||
dest: C:\Temp\
|
dest: c:\Temp
|
||||||
|
- name: Copy a single file where the source is on the remote host
|
||||||
|
win_copy:
|
||||||
|
src: C:\temp\foo.txt
|
||||||
|
dest: C:\ansible\foo.txt
|
||||||
|
remote_src: True
|
||||||
|
- name: Copy a folder recursively where the source is on the remote host
|
||||||
|
win_copy:
|
||||||
|
src: C:\temp
|
||||||
|
dest: C:\ansible
|
||||||
|
remote_src: True
|
||||||
|
- name: Set the contents of a file
|
||||||
|
win_copy:
|
||||||
|
dest: C:\temp\foo.txt
|
||||||
|
content: abc123
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = r'''
|
RETURN = r'''
|
||||||
dest:
|
dest:
|
||||||
description: destination file/path
|
description: destination file/path
|
||||||
|
@ -80,23 +112,22 @@ src:
|
||||||
sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source
|
sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source
|
||||||
checksum:
|
checksum:
|
||||||
description: sha1 checksum of the file after running copy
|
description: sha1 checksum of the file after running copy
|
||||||
returned: success
|
returned: success, src is a file
|
||||||
type: string
|
type: string
|
||||||
sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
|
sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
|
||||||
size:
|
size:
|
||||||
description: size of the target, after execution
|
description: size of the target, after execution
|
||||||
returned: changed (single files only)
|
returned: changed (src is a file or remote_src == True)
|
||||||
type: int
|
type: int
|
||||||
sample: 1220
|
sample: 1220
|
||||||
operation:
|
operation:
|
||||||
description: whether a single file copy took place or a folder copy
|
description: whether a single file copy took place or a folder copy
|
||||||
returned: changed (single files only)
|
returned: changed
|
||||||
type: string
|
type: string
|
||||||
sample: file_copy
|
sample: file_copy
|
||||||
original_basename:
|
original_basename:
|
||||||
description: basename of the copied file
|
description: basename of the copied file
|
||||||
returned: changed (single files only)
|
returned: changed, src is a file
|
||||||
type: string
|
type: string
|
||||||
sample: foo.txt
|
sample: foo.txt
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
|
@ -16,13 +16,20 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
- name: record the output directory
|
- name: remove win_output_dir
|
||||||
set_fact: output_file={{win_output_dir}}/foo.txt
|
win_file:
|
||||||
|
path: "{{win_output_dir}}"
|
||||||
|
state: absent
|
||||||
|
|
||||||
|
- name: recreate win_output_dir
|
||||||
|
win_file:
|
||||||
|
path: "{{win_output_dir}}"
|
||||||
|
state: directory
|
||||||
|
|
||||||
- name: copy an empty file
|
- name: copy an empty file
|
||||||
win_copy:
|
win_copy:
|
||||||
src: empty.txt
|
src: empty.txt
|
||||||
dest: "{{win_output_dir}}/empty.txt"
|
dest: "{{win_output_dir}}\\empty.txt"
|
||||||
register: copy_empty_result
|
register: copy_empty_result
|
||||||
|
|
||||||
- name: check copy empty result
|
- name: check copy empty result
|
||||||
|
@ -55,240 +62,458 @@
|
||||||
- copy_empty_again_result.checksum == 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
|
- copy_empty_again_result.checksum == 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
|
||||||
|
|
||||||
- name: initiate a basic copy
|
- name: initiate a basic copy
|
||||||
#- name: initiate a basic copy, and also test the mode
|
win_copy:
|
||||||
# win_copy: src=foo.txt dest={{output_file}} mode=0444
|
src: foo.txt
|
||||||
win_copy: src=foo.txt dest={{output_file}}
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
register: copy_result
|
register: copy_result
|
||||||
|
|
||||||
- debug: var=copy_result
|
- name: check that the basic copy of the file was created
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\foo.txt"
|
||||||
|
register: copy_result_stat
|
||||||
|
|
||||||
#- name: check the presence of the output file
|
- name: check basic copy result
|
||||||
- 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:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "'changed' in copy_result"
|
- copy_result|changed
|
||||||
# - "'dest' in copy_result"
|
- copy_result.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
|
||||||
# - "'group' in copy_result"
|
- copy_result_stat.stat.exists == True
|
||||||
# - "'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
|
- name: initiate a basic copy again
|
||||||
|
win_copy:
|
||||||
|
src: foo.txt
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
register: copy_result_again
|
||||||
|
|
||||||
|
- name: check basic copy result again
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "copy_result.changed == true"
|
- not copy_result_again|changed
|
||||||
|
- copy_result_again.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
|
||||||
|
|
||||||
- name: verify that the file checksum is correct
|
- name: copy file that exists on remote but checksum different and force is False
|
||||||
|
win_copy:
|
||||||
|
src: empty.txt
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
force: False
|
||||||
|
register: copy_result_no_force_different
|
||||||
|
|
||||||
|
- name: get stat on remote file for assertion
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\foo.txt"
|
||||||
|
register: copy_result_no_force_different_stat
|
||||||
|
|
||||||
|
- name: check that nothing changed when not forcing file and dest exists
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "copy_result.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'"
|
- not copy_result_no_force_different|changed
|
||||||
|
- copy_result_no_force_different_stat.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
|
||||||
|
|
||||||
- name: check the stat results of the file
|
- name: copy file that doesn't exist on remote and force is False
|
||||||
win_stat: path={{output_file}}
|
win_copy:
|
||||||
register: stat_results
|
src: empty.txt
|
||||||
|
dest: "{{win_output_dir}}\\no_force.txt"
|
||||||
|
force: False
|
||||||
|
register: copy_result_no_force
|
||||||
|
|
||||||
- debug: var=stat_results
|
- name: get stat on remote file for assertion
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\no_force.txt"
|
||||||
|
register: copy_result_no_force_stat
|
||||||
|
|
||||||
- name: assert the stat results are correct
|
- name: check that there was a change when not forcing file and dest does not exist
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "stat_results.stat.exists == true"
|
- copy_result_no_force|changed
|
||||||
# - "stat_results.stat.isblk == false"
|
- copy_result_no_force_stat.stat.exists == True
|
||||||
# - "stat_results.stat.isfifo == false"
|
- copy_result_no_force_stat.stat.checksum == 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
|
||||||
# - "stat_results.stat.isreg == true"
|
|
||||||
# - "stat_results.stat.issock == false"
|
|
||||||
- "stat_results.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'"
|
|
||||||
|
|
||||||
- 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
|
- name: make an output subdirectory
|
||||||
win_file: name={{output_subdir}} state=directory
|
win_file:
|
||||||
|
path: "{{win_output_dir}}\\sub"
|
||||||
|
state: directory
|
||||||
|
|
||||||
- name: test recursive copy to directory
|
- name: test recursive copy to directory
|
||||||
# win_copy: src=subdir dest={{output_subdir}} directory_mode=0700
|
win_copy:
|
||||||
win_copy: src=subdir dest={{output_subdir}}
|
src: subdir
|
||||||
|
dest: "{{win_output_dir}}\\sub"
|
||||||
register: recursive_copy_result
|
register: recursive_copy_result
|
||||||
|
|
||||||
- debug: var=recursive_copy_result
|
- name: get stats on files within sub directory
|
||||||
|
win_find:
|
||||||
|
paths: "{{win_output_dir}}\\sub"
|
||||||
|
recurse: True
|
||||||
|
register: recurse_find_results
|
||||||
|
|
||||||
- name: check that a file in a directory was transferred
|
- name: assert recursive copy worked
|
||||||
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:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "stat_bar.stat.exists"
|
- recursive_copy_result|changed
|
||||||
- "stat_bar2.stat.exists"
|
- recurse_find_results.examined == 7 # checks that it found 4 folders and 3 files
|
||||||
- "stat_bar3.stat.exists"
|
|
||||||
|
|
||||||
- name: stat the recursively copied directories
|
- name: test recursive copy to directory again
|
||||||
win_stat: path={{win_output_dir}}/sub/{{item}}
|
win_copy:
|
||||||
register: dir_stats
|
src: subdir
|
||||||
with_items:
|
dest: "{{win_output_dir}}\\sub"
|
||||||
- "subdir"
|
register: recursive_copy_result_again
|
||||||
- "subdir/subdir2"
|
|
||||||
- "subdir/subdir2/subdir3"
|
|
||||||
- "subdir/subdir2/subdir3/subdir4"
|
|
||||||
|
|
||||||
# can't check file mode on windows so commenting this one out.
|
- name: assert recursive copy worked
|
||||||
#- name: assert recursive copied directories mode
|
assert:
|
||||||
# assert:
|
that:
|
||||||
# that:
|
- not recursive_copy_result_again|changed
|
||||||
# - "{{item.stat.mode}} == 0700"
|
|
||||||
# with_items: "{{dir_stats.results}}"
|
|
||||||
|
|
||||||
|
- name: create file with content
|
||||||
|
win_copy:
|
||||||
|
content: abc
|
||||||
|
dest: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: content_result
|
||||||
|
|
||||||
# errors on this aren't presently ignored so this test is commented out. But it would be nice to fix.
|
- name: get stat on creating file with content
|
||||||
#
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: content_stat
|
||||||
|
|
||||||
# content param not available in win_copy
|
- name: assert content copy worked
|
||||||
#- name: overwrite the file again using the content system, also passing along file params
|
assert:
|
||||||
# win_copy: content="modified" dest={{output_file}}
|
that:
|
||||||
# register: copy_result4
|
- content_result|changed
|
||||||
|
- content_stat.stat.exists == True
|
||||||
|
- content_stat.stat.checksum == 'a9993e364706816aba3e25717850c26c9cd0d89d'
|
||||||
|
|
||||||
#- name: assert invalid copy input location fails
|
- name: create file with content again
|
||||||
# win_copy: src=invalid_file_location_does_not_exist dest={{win_output_dir}}/file.txt
|
win_copy:
|
||||||
# ignore_errors: True
|
content: abc
|
||||||
# register: failed_copy
|
dest: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: content_result_again
|
||||||
|
|
||||||
# owner not available in win_copy, commenting out
|
- name: assert content copy again didn't change
|
||||||
#- name: copy already copied directory again
|
assert:
|
||||||
# win_copy: src=subdir dest={{output_subdir | expanduser}} owner={{ansible_ssh_user}}
|
that:
|
||||||
# register: copy_result5
|
- not content_result_again|changed
|
||||||
|
|
||||||
#- name: assert that the directory was not changed
|
- name: copy file with different content
|
||||||
# assert:
|
win_copy:
|
||||||
# that:
|
content: 123
|
||||||
# - "not copy_result5|changed"
|
dest: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: content_different_result
|
||||||
|
|
||||||
# content not available in win_copy, commenting out.
|
- name: get stat on file with different content
|
||||||
# issue 8394
|
win_stat:
|
||||||
#- name: create a file with content and a literal multiline block
|
path: "{{win_output_dir}}\\content.txt"
|
||||||
# win_copy: |
|
register: content_different_stat
|
||||||
# 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 different content copy worked
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- content_different_result|changed
|
||||||
|
- content_different_stat.stat.checksum == '40bd001563085fc35165329ea1ff5c5ecbdbbeef'
|
||||||
|
|
||||||
#- name: assert the multiline file was created correctly
|
- name: copy remote file
|
||||||
# assert:
|
win_copy:
|
||||||
# that:
|
src: "{{win_output_dir}}\\foo.txt"
|
||||||
# - "copy_result6.changed"
|
dest: "{{win_output_dir}}\\foobar.txt"
|
||||||
# - "copy_result6.dest == '{{win_output_dir|expanduser}}/multiline.txt'"
|
remote_src: True
|
||||||
# - "copy_result6.checksum == '1627d51e7e607c92cf1a502bf0c6cce3'"
|
register: remote_file_result
|
||||||
|
|
||||||
# test overwriting a file as an unprivileged user (pull request #8624)
|
- name: get stat on new remote file
|
||||||
# this can't be relative to {{win_output_dir}} as ~root usually has mode 700
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\foobar.txt"
|
||||||
|
register: remote_file_stat
|
||||||
|
|
||||||
#- name: create world writable directory
|
- name: assert remote copy worked
|
||||||
#win_file: dest=/tmp/worldwritable state=directory mode=0777
|
assert:
|
||||||
|
that:
|
||||||
|
- remote_file_result|changed
|
||||||
|
- remote_file_result.size == 8
|
||||||
|
- remote_file_result.src == '{{win_output_dir|regex_replace('\\', '\\\\')}}\\foo.txt'
|
||||||
|
- remote_file_result.dest == '{{win_output_dir|regex_replace('\\', '\\\\')}}\\foobar.txt'
|
||||||
|
- remote_file_result.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
|
||||||
|
- remote_file_result.operation == 'file_copy'
|
||||||
|
- remote_file_result.original_basename == 'foo.txt'
|
||||||
|
- remote_file_stat.stat.exists == True
|
||||||
|
- remote_file_stat.stat.checksum == 'c79a6506c1c948be0d456ab5104d5e753ab2f3e6'
|
||||||
|
|
||||||
#- name: create world writable file
|
- name: copy remote file again
|
||||||
# win_copy: dest=/tmp/worldwritable/file.txt content="bar" mode=0666
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\foo.txt"
|
||||||
|
dest: "{{win_output_dir}}\\foobar.txt"
|
||||||
|
remote_src: True
|
||||||
|
register: remote_file_result_again
|
||||||
|
|
||||||
#- name: overwrite the file as user nobody
|
- name: assert remote copy again did not change
|
||||||
# win_copy: dest=/tmp/worldwritable/file.txt content="baz"
|
assert:
|
||||||
# become: yes
|
that:
|
||||||
# become_user: nobody
|
- not remote_file_result_again|changed
|
||||||
# register: copy_result7
|
|
||||||
|
|
||||||
#- name: assert the file was overwritten
|
- name: copy remote folder
|
||||||
# assert:
|
win_copy:
|
||||||
# that:
|
src: "{{win_output_dir}}\\sub"
|
||||||
# - "copy_result7.changed"
|
dest: "{{win_output_dir}}\\sub2"
|
||||||
# - "copy_result7.dest == '/tmp/worldwritable/file.txt'"
|
remote_src: True
|
||||||
# - "copy_result7.checksum == '73feffa4b7f6bb68e44cf984c85f6e88'"
|
register: remote_folder_result
|
||||||
|
|
||||||
#- name: clean up
|
- name: get stat on new remote folder contents
|
||||||
# win_file: dest=/tmp/worldwritable state=absent
|
win_find:
|
||||||
|
paths: "{{win_output_dir}}\\sub2"
|
||||||
|
recurse: True
|
||||||
|
register: remote_folder_stat
|
||||||
|
|
||||||
# test overwritting a link using "follow=yes" so that the link
|
- name: assert remote copy worked
|
||||||
# is preserved and the link target is updated
|
assert:
|
||||||
|
that:
|
||||||
|
- remote_folder_result|changed
|
||||||
|
- remote_folder_result.size == 11
|
||||||
|
- remote_folder_result.src == '{{win_output_dir|regex_replace('\\', '\\\\')}}\\sub'
|
||||||
|
- remote_folder_result.dest == '{{win_output_dir|regex_replace('\\', '\\\\')}}\\sub2'
|
||||||
|
- remote_folder_result.operation == 'folder_copy'
|
||||||
|
- remote_folder_stat.examined == 8 # 5 folders 3 files
|
||||||
|
|
||||||
#- name: create a test file to symlink to
|
- name: copy remote folder again
|
||||||
# win_copy: dest={{win_output_dir}}/follow_test content="this is the follow test file\n"
|
win_copy:
|
||||||
#
|
src: "{{win_output_dir}}\\sub"
|
||||||
#- name: create a symlink to the test file
|
dest: "{{win_output_dir}}\\sub2"
|
||||||
# win_file: path={{win_output_dir}}/follow_link src='./follow_test' state=link
|
remote_src: True
|
||||||
#
|
register: remote_folder_result_again
|
||||||
#- 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
|
- name: assert remote copy again did not change
|
||||||
# win_stat: path={{win_output_dir}}/follow_link
|
assert:
|
||||||
# register: stat_link_result
|
that:
|
||||||
#
|
- not remote_folder_result_again|changed
|
||||||
#- 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
|
- name: fail to copy when source doesn't exist
|
||||||
# assert:
|
win_copy:
|
||||||
# that:
|
src: false-file
|
||||||
# - replace_follow_result.checksum == target_file_result.stdout
|
dest: "{{win_output_dir}}\\fale-file.txt"
|
||||||
|
register: fail_missing_source
|
||||||
|
failed_when: fail_missing_source.msg != "Unable to find 'false-file' in expected paths."
|
||||||
|
|
||||||
- name: clean up sub
|
- name: fail when copying remote src file when src doesn't exist
|
||||||
win_file: path={{win_output_dir}}/sub state=absent
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\fake.txt"
|
||||||
|
dest: "{{win_output_dir}}\\real.txt"
|
||||||
|
remote_src: True
|
||||||
|
register: fail_remote_missing_src
|
||||||
|
failed_when: "fail_remote_missing_src.msg != 'Cannot copy src file: ' + win_output_dir + '\\\\fake.txt as it does not exist'"
|
||||||
|
|
||||||
- name: clean up foo.txt
|
- name: fail when copying remote src folder to file
|
||||||
win_file: path={{win_output_dir}}/foo.txt state=absent
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\sub"
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
remote_src: True
|
||||||
|
register: fail_remote_folder_to_file
|
||||||
|
failed_when: "'If src is a folder, dest must also be a folder. src' not in fail_remote_folder_to_file.msg"
|
||||||
|
|
||||||
|
- name: fail when copying remote src file to folder
|
||||||
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\foo.txt"
|
||||||
|
dest: "{{win_output_dir}}\\sub"
|
||||||
|
remote_src: True
|
||||||
|
register: fail_remote_file_to_folder
|
||||||
|
failed_when: "'If src is a file, dest must also be a file. src' not in fail_remote_file_to_folder.msg"
|
||||||
|
|
||||||
|
- name: run check mode copy new file
|
||||||
|
win_copy:
|
||||||
|
src: foo.txt
|
||||||
|
dest: "{{win_output_dir}}\\foo-check.txt"
|
||||||
|
register: check_copy_file
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on new file
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\foo-check.txt"
|
||||||
|
register: check_stat_file
|
||||||
|
|
||||||
|
- name: assert check would change but file doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_copy_file|changed
|
||||||
|
- check_stat_file.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy existing file
|
||||||
|
win_copy:
|
||||||
|
src: foo.txt
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
register: check_copy_file_existing
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check wouldn't change existing file
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_copy_file_existing|changed
|
||||||
|
|
||||||
|
- name: run check mode copy existing file with force False
|
||||||
|
win_copy:
|
||||||
|
src: empty.txt
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
force: False
|
||||||
|
register: check_copy_existing_no_force
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check wouldn't change existing file
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_copy_existing_no_force|changed
|
||||||
|
|
||||||
|
- name: run check mode copy new file with force False
|
||||||
|
win_copy:
|
||||||
|
src: empty.txt
|
||||||
|
dest: "{{win_output_dir}}\\no-force-check.txt"
|
||||||
|
force: False
|
||||||
|
register: check_copy_no_force
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on new file
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\no-force-check.txt"
|
||||||
|
register: check_copy_no_force_stat
|
||||||
|
|
||||||
|
- name: assert check wouldn't create file but change registered
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_copy_no_force|changed
|
||||||
|
- check_copy_no_force_stat.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy new folder
|
||||||
|
win_copy:
|
||||||
|
src: subdir
|
||||||
|
dest: "{{win_output_dir}}\\sub-check"
|
||||||
|
register: check_copy_folder
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on new folder
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\sub-check"
|
||||||
|
register: check_stat_folder
|
||||||
|
|
||||||
|
- name: assert check would change but folder doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_copy_folder|changed
|
||||||
|
- check_stat_folder.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy existing folder
|
||||||
|
win_copy:
|
||||||
|
src: subdir
|
||||||
|
dest: "{{win_output_dir}}\\sub"
|
||||||
|
register: check_copy_folder_existing
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check wouldn't change existing file
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_copy_folder_existing|changed
|
||||||
|
|
||||||
|
- name: run check mode copy new contents
|
||||||
|
win_copy:
|
||||||
|
content: abc
|
||||||
|
dest: "{{win_output_dir}}\\content-check.txt"
|
||||||
|
register: check_content_file
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on content file
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\content-check.txt"
|
||||||
|
register: check_stat_content
|
||||||
|
|
||||||
|
- name: assert check would change but content file doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_content_file|changed
|
||||||
|
- check_stat_content.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy existing contents
|
||||||
|
win_copy:
|
||||||
|
content: 123
|
||||||
|
dest: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: check_content_file_existing
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check wouldn't change exisitng content file
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_content_file_existing|changed
|
||||||
|
|
||||||
|
- name: run check mode copy new contents
|
||||||
|
win_copy:
|
||||||
|
content: abc
|
||||||
|
dest: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: check_different_content_file
|
||||||
|
|
||||||
|
- name: get stat on check mode file with different content
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\content.txt"
|
||||||
|
register: check_different_content_stat
|
||||||
|
|
||||||
|
- name: assert check content changed but file wasn't touched
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_different_content_file|changed
|
||||||
|
|
||||||
|
- name: run check mode copy new file remote src
|
||||||
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\foo.txt"
|
||||||
|
dest: "{{win_output_dir}}\\foo-check.txt"
|
||||||
|
remote_src: True
|
||||||
|
register: check_copy_file_remote
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on new file
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\foo-check.txt"
|
||||||
|
register: check_stat_file_remote
|
||||||
|
|
||||||
|
- name: assert check would change but file doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_copy_file_remote|changed
|
||||||
|
- check_stat_file_remote.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy existing file remote src
|
||||||
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\foo.txt"
|
||||||
|
dest: "{{win_output_dir}}\\foo.txt"
|
||||||
|
remote_src: True
|
||||||
|
register: check_copy_file_remote_existing
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check would change but file doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_copy_file_remote_existing|changed
|
||||||
|
|
||||||
|
- name: run check mode copy new folder remote src
|
||||||
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\sub"
|
||||||
|
dest: "{{win_output_dir}}\\sub-check"
|
||||||
|
remote_src: True
|
||||||
|
register: check_copy_folder_remote
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: get stat on new file
|
||||||
|
win_stat:
|
||||||
|
path: "{{win_output_dir}}\\sub-check"
|
||||||
|
register: check_stat_folder_remote
|
||||||
|
|
||||||
|
- name: assert check would change but folder doesn't exist
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- check_copy_folder_remote|changed
|
||||||
|
- check_stat_folder_remote.stat.exists == False
|
||||||
|
|
||||||
|
- name: run check mode copy existing folder remote src
|
||||||
|
win_copy:
|
||||||
|
src: "{{win_output_dir}}\\sub"
|
||||||
|
dest: "{{win_output_dir}}\\sub2"
|
||||||
|
remote_src: True
|
||||||
|
register: check_copy_folder_remote_existing
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- name: assert check wouldn't change existing folder
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not check_copy_folder_remote_existing|changed
|
||||||
|
|
||||||
|
- name: cleanup output dir
|
||||||
|
win_file:
|
||||||
|
path: "{{win_output_dir}}"
|
||||||
|
state: absent
|
||||||
|
|
Loading…
Reference in a new issue