Reputation: 24298
There seem to be several solutions which all act in multiple steps e.g. * copy and delete * use a native wincommand or pwoershell
But isn't there just a rename as win_module? Or an option on copy to delete the source after a copy?
Upvotes: 3
Views: 14039
Reputation: 603
Another option is to use win_shell
module and Rename-Item
powershell command to achieve it. And we can use Force
option to handle in case if file is in use etc as well!.
win_shell:
Rename-Item -Path "C:\Path\To\file\foo.bar" -NewName "foo_new.bar" -Force
Upvotes: 0
Reputation: 1
Old post but it can be useful for future readers.
You could use win_robocopy module, which provides options such as /MOVE for directories and /MOV for files. Robocopy is installed by default on windows machines for years
Ansible Robocopy module documentation https://docs.ansible.com/ansible/latest/collections/community/windows/win_robocopy_module.html
Nice feature run-through of robocopy
Upvotes: 0
Reputation: 411
MBushveld, I see that your windows "rename" command does the trick nicely in this case. But in general Powershell commands cover a broader range of circumstances and have more switch/flag options for unique cases. For example check out the Powershell "Rename-Item" command here. So if your Windows command comes up short next time you can write a short Powershell script and call it from Ansible with whatever command line arguments you need. At the bottom of this post is a powershell script I wrote to verify that the contents of 2 text files were exactly the same. I used the "script:" command in Ansible to call the script with arguments as follows.
- name: verify the file contents match
script: filesAreSame.ps1 "C:/Temp/" "file1.txt" "file2.txt"
register: result
- set_fact: filesMatch="{{result.stdout_lines.4 | bool}}"
Ansible will move the script to the remote host, execute it and then delete it. If needed you can use the "register:" command in Ansible as I did to capture any values returned by the script. Here are the contents of the "filesAreSame.ps1" Powershell script.
# verifys that the specified files contain the same text
param(
[string]$uncPath,
[string]$uncFile1,
[string]$uncFile2
)
$uncFullFileName1 = $uncPath + $uncFile1
$uncFullFileName2 = $uncPath + $uncFile2
$filetext1=[System.IO.File]::ReadAllText($uncFullFileName1).TrimStart().TrimEnd()
$filetext2=[System.IO.File]::ReadAllText($uncFullFileName2).TrimStart().TrimEnd()
# verify first file is not empty
if ($filetext1 -eq "")
{
return "ERROR: Source file is empty"
}
# case sensitive comparison
if ($filetext1 -cne $filetext2)
{
return "ERROR: Files are not the same"
}
return $TRUE
Upvotes: 0
Reputation: 24298
I found some answers to the question:
First it is to point out that this is for a remote windows host. For Unix systems we have a bunch of answers already in stackoverflow, not so for windows.
There is no win_rename module nor a win_file with the option to rename. You can not use win_copy since the file is already on the remote system. So the simplest way is to use a local windows command.
- name: rename the {{ source_name }} to {{ target_name }}
win_command: "cmd.exe /c rename {{ destination_folder }}\\{{ source_name }} {{ target_name }}"
Upvotes: 10