Powershells Copy-Item in Ansible
ansible copy example
ansible copy with_items
ansible copy as root
I have a problem with ansible. I need to copy something from one computer in network onto host machine (windows) using ansible task.
I had tested many diffrent approach, including powerscript file as ansible task, and using
win_shell to directly include powershells CMD-lets.
In every case, when i call Copy-Item directly on host machine in Powershell window, everything works fine. But after calling it by Ansible i got
Copy-Item : Cannot find path '//[ip]/[path]/[file]' because it does not exist.
When im logging to host machine i'm using the same user both in ansible and in remote desktop connection.
Has anyone idea what can going on?
- name: Install 7Zip win_shell: | $path = '//[ip]/[path]/[file]' Copy-Item $path -Destination "C:/[Location]" tags: - 7zip
[location] are just mocks i put on SO to avoid bothering you all with Polish folder names and my intranet IP.
In powershell command in exactly the same as in ansible:
PS C:\Users\ansibler> $path = '//[ip]/[path]/[file]' PS C:\Users\ansibler> Copy-Item $path -Destination "[location]"
Ok, i solve it.
double hop was a problem. Solution was to use "ansible_become" and re-authenticate user in host machine:
vars: ansible_become: yes ansible_become_method: runas ansible_become_flags: logon_type=new_credentials logon_flags=netcredentials_only ansible_become_user: [login] ansible_become_pass: [password]
win_copy, Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied (including the source folder name) We can use another Ansible module called win_copy that allows copying files either from the local Ansible server or from a remote share. You could use the following code in an Ansible playbook to copy a PowerShell script housed in a local Ansible server folder (root/test/resources as in the example).
You are trying to use UNC path, UNC path uses backslash.
- name: Install 7Zip win_shell: | $path = '\\[ip]\[path]\[file]' Copy-Item $path -Destination "C:/[Location]" tags: - 7zip
the best way to copy file is to use
- name: Install 7Zip win_copy: src: '\\[ip]\[path]\[file]' dest: "C:\[Location]" remote_src: True
The error you got is because of the double hop here. I can show you a workaround by mapping a drive before attempting a copy, this work arounds the double hopping issues.
- name: Install 7Zip win_shell: | net use \\[ip]\[path] password /user:username 2>&1 $path = '\\[ip]\[path]\[file]' Copy-Item $path -Destination "C:/[Location]" tags: - 7zip
win_copy – Copies files to remote locations on windows hosts , For non-Windows targets, use the copy module instead. No backup is taken when remote_src=False and multiple files are being copied. the source is on the remote host win_copy: src: C:\Temp dest: C:\ansible remote_src: yes - name: Set Synopsis ¶. The only option when using ‘winrm’ or ‘psrp’ as a connection plugin. Can also be used when using ‘ssh’ as a connection plugin and the DefaultShell has been configured to PowerShell.
For me, it worked in this way:
- name: Run Shell Copy Cmd win_shell: Copy-Item '\\remote\public\foo\*' C:\Windows\Temp\bar become: yes become_method: runas become_user: vagrant vars: ansible_become_password: "pass"
I tried the same with the
win_copy module, but could never make it work
Unable to execute powershell copy/robocopy/xcopy command on , Bug Report ANSIBLE VERSION ansible --version ansible 184.108.40.206 config file Unable to execute powershell copy/robocopy/xcopy command on windows \r\n ient:String) [Copy-Item], UnauthorizedAccessException\r\n + Ansible, Windows and PowerShell: the Basics – Introduction Back in June of this year I presented a session at PowerShell Southampton on using Ansible and PowerShell together. There was a fair amount of interest in this topic both at the event and online afterwards, so I decided to follow it up here with a series of posts looking at the basics
How to do stuff on the remote Windows node from the command , Use the Ansible "script:" module to copy, run and delete a Powershell module del or PowerShell cmdlets like New-Item, Copy-Item and so on Powershell Scripts in Ansible. Ask Question Asked 4 years, 2 months ago. Active 4 years, 2 months ago. Powershells Copy-Item in Ansible. Hot Network Questions
Using Windows PowerShell with Ansible – 4sysops, We can use another Ansible module called win_copy that allows copying files either from the local Ansible server or from a remote share. You The copy module copies a file from the local or remote machine to a location on the remote machine. The attributes the resulting file or directory should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr.
Copy-Item, The particular items that the cmdlet can copy depend on the PowerShell provider that exposes the item. For instance, it can copy files and directories in a file There are windows modules (written in powershell, no less) that allow ansible to do a lot of things to windows systems. Take a look at an ansible playbook for updating a host, joining it to domain, and rebooting it (about 10 short lines of yaml) compared to a powershell script.
- what are
[location]? are they variables, do you use that litteraly ? You shoudl also provide the command you use directly that work for comparison. Please edit the question to add those information.
- @BaptisteMille-Mathias done
- A further step to debug would be to register the task and debug the output and add this to the question.
- Please feel free to mark your own answer as accepted.
- @Tom - I'll do that, but it can be done after two day from posting self-answer ;).
- the output is the same, despite of direction of slashes
- did you try
win_copy?. When accessing from remote share, PowerShell will have to handle double hop, hence
- Yes, i have try. Effect is the same:
"module_stderr": "Exception calling \"Run\" with \"1\" argument(s): \"Exception calling \"Invoke\" with \r\n\"0\" argument(s): \"The running command stopped because the preference variable \"\r\nErrorActionPreference\" or common parameter is set to Stop: Access is denied\"\"\r\nAt line:65 char:5\r\n+ $output = $entrypoint.Run($payload)\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : NotSpecified: (:) , ParentContainsErrorRecordE \r\n xception\r\n + FullyQualifiedErrorId : ScriptMethodRuntimeException\r\n \r\n"
- Could you register the command you posted in the question and debug it and attach the tasks execution in the question ?
- Never worked that way, even with UNC. I'm posting my working example. Thx to all for the references