Smily
Smily

Reputation: 2568

Download a file from a windows share using Ansible

I have a file in windows share. I need to download this file using ansible.
Playbook

- name: Copy installer 
  get_url:
     url: file:'\\Winserver\Share_Binary\Installer-v6.tar.gz'
     dest: /tmp

Error Output:

    "mode": "01777",
    "msg": "Request failed: <urlopen error [Errno 2] No such file or directory: \"'\\\\\\\\Winserver\\\\Share_Binary\\\\Installer-v6.tar.gz'\">",
    "owner": "root",
    "size": 4096,
    "state": "directory",
    "uid": 0,
    "url": "file:'\\\\Winserver\\Share_Binary\\Installer-v6.tar.gz'"

The file exists. When I paste \\Winserver\Share_Binary\Installer-v6.tar.gz in the file explorer in my system I could see the file. Please advice.

Upvotes: 1

Views: 6131

Answers (2)

jconnell
jconnell

Reputation: 111

This can be accomplished with a credential file and 3 Ansible tasks.

First, create a credential file (e.g. /home/youruser/smbshare.cred) containing the username and password of a service account with permissions to mount the CIFS share:

username=your_service_account_name
password=your_service_account_password

Make sure your remote ansible user owns the file (or root if you're using become) and it has 0400 permissions. You might consider generating this credential file with Ansible and/or encrypting it with Ansible Vault in the future as well.

Task 1: Use the mount module to mount the SMB share on the target.

- name: Mount SMB share
  mount:
    path: /mnt/smbshare
    src: '\\\\Winserver\\Share_Binary'
    fstype: cifs
    opts: 'credentials=/home/youruser/smbshare.cred'
    state: mounted

Task 2: Use the copy module to copy the file wherever you actually want it.

- name: Copy installer tarball to target
  copy:
    src: /mnt/smbshare/Installer-v6.tar.gz
    dest: /some/local/path/Installer-v6.tar.gz
    owner: foo
    group: foo
    mode: 0640

Task 3: Use the mount module to unmount the SMB share.

- name: Unmount SMB share
  mount:
    path: /mnt/smbshare
    state: unmounted

Note: Depending on your environment, you might need to add more mount options (refer to mount.cifs(8) man page) to the opts: parameter in task 1.

Upvotes: 1

itiic
itiic

Reputation: 3712

not sure if get_url is capable to do that. Pls try this one:

  - name: Get file from smb.
    command:
      smbclient //Winserver/Share_Binary/ <pass> -U <user> -c "get Installer-v6.tar.gz"
      creates=/tmp/Installer-v6.tar.gz

Of course, you have to install smbclient first

Upvotes: 0

Related Questions