
In this post, I will show a playbook with different tasks to copy files from Windows CIFS shared folder to a Linux folder.
First, the status and the prerequisites:
- I have 3 machines:
- The Ansible controller ansible_srv01 where the playbook while be launched
- The Windows machine win_srv01
- The Linux machine lin_srv01
- The data to copy is located in win_srv01 in the CIFS shared folder called : \\win_srv01\d$\src_data
- The serveur win_srv01 is member of the Active Directory called mydom.local
- Credentials to connect to the CIFS shared folder are:
- user : cifs_user_readaccess
- password : myS3cretPassword
- The destination folder is located on lin_srv01 folder called /opt/my_dest
The steps to copy my files will be the following:
- Create a temporary folder on lin_srv01 to act as a mountpoint for the CIFS shared folder : /tmp/my_tmp_mountpoint
- Mount the CIFS shared folder
- Copy data from win_srv01 to lin_srv01
- Umount
- Remove the mountpoint
And now the Ansible playbook:
---
- name: My copy playbook
hosts: lin_srv01
gather_facts: no
vars:
mountpath: "/tmp/my_tmp_mountpoint"
tasks:
- name: Create mount point on {{ inventory_hostname }}
file:
path: "{{ mountpath }}"
state: directory
- name: Mount the win_srv01 CIFS volume
mount:
path: "{{ mountpath }}"
src: //win_srv01/d$/src_data
# do not use in the following line in production.
# Protect your credentials using Ansible vaults
# https://docs.ansible.com/ansible/latest/user_guide/vault.html
opts: vers=2.1,user=cifs_user_readaccess,pass=myS3cretPassword,dom=mydom
state: mounted
fstype: cifs
- name: Copy data
copy:
src: "{{ mountpath }}/"
dest: /opt/my_dest
remote_src: yes
- name: Unmount the mounted volume
mount:
path: "{{ mountpath }}"
# absent = umount and remove from fstab
state: absent
- name: Delete temporary mount point on {{ inventory_hostname }}
file:
path: "{{ mountpath }}"
state: absent
Copy files from Windows CIFS share
