dushyantp
dushyantp

Reputation: 4718

Ansible yml copy file from network location

So I have an ansible playbook as follows:

#WINDOWS#
---
- hosts: windows   tasks:

  - name:  copy file
    raw: '"net use M: "\\somemachinename\someLocation" /user:username password"'
    raw: '"xcopy M:\isntaller.exe C:\installerlocation /Y"'
    raw: '"net use M: /delete /y"'

The file does exist at the network location and the username password are valid. The task doesn't report any errors. But the file never gets copied across.

Does anyone know if I am doing the playbook syntax wrong? Or is there a better way to get files across network location in ansible setup?

P.S. I do not have access to the ansible server. Although I know it is a Red Hat Linux server.

Upvotes: 1

Views: 3705

Answers (3)

jrbe228
jrbe228

Reputation: 578

The following worked for me:

---
- name: Map network drive Z with credentials and copy to C:\autodeploy
  block:
    - name: Save the network credentials required for the mapped drive
      community.windows.win_credential:
        name: network-cred
        type: domain_password
        username: '{{ ansible_user }}'
        secret: '{{ ansible_password }}'
        state: present

    - name: Create a mapped drive Z
      community.windows.win_mapped_drive:
        letter: Z
        path: '\\10.0.0.169\SmbShare'
        username: '{{ ansible_user }}'
        password: '{{ ansible_password }}'
        state: present

    - name: Ensure drive Z is persistent
      ansible.windows.win_command:
        cmd: 'net use Z: \\10.0.0.169\SmbShare /persistent:yes'

    #Copy contents from Z:\installers to C:\autodeploy
    - name: Ensure C:\autodeploy exists
      ansible.windows.win_file:
        path: C:\autodeploy
        state: directory

    - name: Copy all contents from UNC path to C:\autodeploy
      ansible.windows.win_copy:
        src: \\10.0.0.169\SmbShare\installers\
        dest: C:/autodeploy
        remote_src: true
        recurse: true

  vars:
    ansible_become: yes
    ansible_become_method: runas
    ansible_become_user: '{{ ansible_user }}'
    ansible_become_pass: '{{ ansible_password }}'

Upvotes: 0

dushyantp
dushyantp

Reputation: 4718

Having tried many combinations, this is what finally worked for me. Seems hacky, but possibly helpful to others who may face the same situation.

I added a batch file at a http location where ansible can get it and execute on the machine.

REM @echo off

set Source=%1%
set Destination=%2%

net use M: \\nwb-somemachinename /user:username password
xcopy /F M:\%Source% %Destination% /Y
net use M: /delete /y

And the yml file looks as follows:

#WINDOWS#
---
- hosts: windows
  tasks:

  - name: get batch file
    raw: httptool.exe http://somelocation/ourbatchfile.bat

  - name: run batch file
    raw: c:\location\ourbatchfile.bat location_remote_machine\installer.exe c:\location

Upvotes: 0

Eric Citaire
Eric Citaire

Reputation: 4513

The third raw is overwriting the first and the second ones because they are in the same task. See YAML syntax overview.

Split this into 3 separate tasks:

#WINDOWS#
---
- hosts: windows
  tasks:

  - name: mount M
    raw: '"net use M: "\\somemachinename\someLocation" /user:username password"'

  - name: copy file
    raw: '"xcopy M:\isntaller.exe C:\installerlocation /Y"'

  - name: unmount M
    raw: '"net use M: /delete /y"'

Also, I'm not sure about quotes and double-quotes. You might have too much of it.

Upvotes: 1

Related Questions