Ansbile copy files from a windows share

We are using Ansible to automate installation of software that our system depends upon in deployment environments. One of the tasks required that we copy a set of files from a windows share. The target operating system is CentOS box. We used smbclient to copy the files from the windows share to the CentOS box.

First, we need to prompt the admin to enter the username and password to access the windows share

---
- hosts: all

  # See note below.
  vars_prompt:
    - name: smb_username
      prompt: "Enter samba share username"
    - name: smb_password
      prompt: "Enter samba share password"
      private: yes

Then, using Ansible, we would have to install smbclient and the libraries that it requires viz

– samba
– samba-client
– samba-common
– cifs-utils

After this, you are good to use the smbclient command to copy the files from a windows share like this.

 - name: Copy archive from samba_share.
    command:
      smbclient //hostname/samba_share/ {{ smb_password }} -U {{ smb_username }} -W "WORKGROUP" -c "recurse;lcd /local/path;get archive.zip"
      creates=/local/path/archive.zip

We faced one small hiccup though. The smbclient was prompting the user to confirm copy. As we were copying a large set of files, confirming each and every copy manually was a tedious process. Hence we added the prompt command to smbclient to not ask for a confirmation before copying.

smbclient //hostname/samba_share/ {{ smb_password }} -U {{ smb_username }} -W "WORKGROUP" -c "recurse;lcd /local/path;prompt;get archive.zip"

Reference

1. https://servercheck.in/blog/getting-file-samba-server-ansible-playbook

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s