How to copy file from localhost to remote host in Ansible playbook? - ansible

I have a directory:
/users/rolando/myfile
I want to copy "myfile" to hostname "targetserver" in directory:
/home/rolando/myfile
What is the syntax in the playbook to do this? Examples I found with the copy command look like it's more about copying a file from a source directory on a remote server to a target directory on the same remote server.
The line in my playbook .yml I tried that failed:
- copy:
src='/users/rolando/myfile'
dest='rolando#targetserver:/home/rolando/myfile'
What am I doing wrong?

From copy synopsis:
The copy module copies a file on the local box to remote locations.
- hosts: targetserver
tasks:
- copy:
src: /users/rolando/myfile
dest: /users/rolando/myfile

Here is the updated answer. Above answer helps copy files in local machine itself.
This should be easy using remote_src parameter available in copy module
- name: Copy a "sudoers" file on the remote machine for editing
copy:
src: /users/rolando/myfile
dest: /home/rolando/myfile
remote_src: yes

Related

How can I specify remote destination server when using copy module in ansible

Copying file to second server
Here is a task that I want to execute in order to copy dump file from prod server to test server.
How can I explicitly explicitly tell what the destination is? Or what's the correct approach.
I have already created a destination dir on the test server (/home/{{sys_user}}/prod_db_copy/).
Here is the task that I tried:
- name: "Copy dump file to test server"
when: inventory_hostname in groups['prod']
copy:
src: "/home/{{ sys_user }}/db_copy/{{ db_name }}.dump.gz"
dest: "{{ inventory_hostname=='test' }} /home/{{sys_user}}/prod_db_copy/"
remote_src: yes
The copy module copies files from the ansible controller to a remote machine or files on the remote machine, but what you are trying to do is copying files between two remote machines, so copy won't do the job. You need to use the synchronize module with delegate_to.
Depending on your network and users on the machines, you might need to add some options or add ssh-keys to some servers, but in general, it can look like this:
- hosts: prod
tasks:
- name: Transfer file from prod machines to backup-server
synchronize:
src: "/home/{{ sys_user }}/db_copy/{{ db_name }}.dump.gz"
dest: "/home/{{sys_user}}/prod_db_copy/"
mode: pull
delegate_to: backup-server
This uses the pull mode. You can use push as well, but then you need to run the task on the backup-server and delegate it to the prod machine.

Is it possible to add a play inside a play that will use copy module on a define host vi ansible?

Hi I have a playbook that fetch information from remote server and place it local server, is it possible to add a play that will copy that file in local and place it on a specific host? I plan to code it below or you have any recommendation what would be best approach? although the server 1 is not in the inventory file that the playbook used.
- name: Get compliance reporting from remote
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_{{ '%y%m%d%H%M%S' | strftime }}
flat: yes
- name: Copy the fetch file
host: server1
copy:
src: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_*
dst: /tmp/
The fetch module will copy the file(s) from remote host to Ansible control machine at <dest>/ansible_hostname.
E.g. for host1.example.co and dest: /home/ansible/linuxpatchingv2:
/home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
So in your playbook, you will have two plays. First play will fetch the file to Ansible control machine, second will copy the file fetched in first play to remote machine.
- name: Fetch the file from host1.example.co
hosts: host1.example.co
tasks:
- name: Fetch the file
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/
- name: Copy the file to remote host server1
hosts: server1
tasks:
- name: Copy report to remote path
copy:
src: /home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
dest: /tmp/

How to copy file from Network path to controller machine in ansible

Let's assume I have a textfile in some network path
EX: \cr-ampd-a01.abcd.loc\BulkFolder\textfile.txt
How to copy that file to my controller machine in ansible
Note: I can access it by (WIN+R and that path) then it pops up for credentials to access.
This is the sample code with win_copy
- name: copy from network to controller
win_copy:
src: \\cr-ampd-a01.abcd.loc\BulkFolder\textfile.txt
dest: C:\Temp\OTW\Mastapps
remote_src: yes
become: yes
vars:
ansible_become_user: XXX\Uxxxxxx
ansible_become_pass: PasswordHere
Error with this code is: Cannot copy src file as it does not exit
Another with net_get
- name: copy from network to controller
net_get:
src: \\cr-ampd-a01.abcd.loc\BulkFolder\textfile.txt
dest: C:\Temp\OTW\Mastapps
ansible_network_os: "eos"
remote_src: True
become: True
vars:
ansible_become_user: XXX\Uxxxxxx
ansible_become_pass: PasswordHere
Error here: ansible_network_os must be specified on this host
How can I achieve this?
You try to copy a file from a CIFS resource. I'm relative sure that this isn't working with copy or win_copy because they are used to copy file on that (physical) machine. Because CIFS is a protocol like HTTP and FTP you need a client to get the file from the remote machine. As you also wrote, you need to identify yourself with some credentials, the win_copy task doesnt have that option.
One option could be - mount the network device to -for example- N:/ or something like that and use then win_copy with N:/source.txt - in that case the N:-drive is like C:/ or D:/ a known path on the machine. Have a look at win_share - https://docs.ansible.com/ansible/latest/modules/win_share_module.html
Another option is to call a local CIFS client via command like command: copy //server/path/file c:/file or robocopy, but that isn't easy to be idempotent. See Copy files to network computers on windows command line
net_get is useful to copy files from "network devices" - please have a look at https://docs.ansible.com/ansible/latest/network/user_guide/platform_index.html#platform-options for a list of supported platforms. As of the list - I would say coping from a CIFS-share is not supported by net_get.

Copy to remote shared path using ansible

I am trying to copy some files to a remote shared path.
---
- hosts: localhost
tasks:
- name: Test
copy:
src: /tmp/log/test.csv
dest: \\xyz_prod.com\public\app\
The playbook ran fine and it displayed changed=1 for the first run. When I ran it again, still it is successful and changed=0. But if I navigate to the shared location manually under the folder the test.csv file is not present. Can anyone please suggest what is wrong here?
dest must not include URL. Quoting from copy
Remote absolute path where the file should be copied to.
Try the play below
- hosts: xyz_prod.com
tasks:
- name: Test
copy:
src: /tmp/log/test.csv
dest: /public/app
For Windows remote hosts use win_copy which "Copies files to remote locations on windows hosts".
To copy from a remote server
use fetch – Fetch files from remote nodes.
See: Ansible - fetch files from one remote node to another.

how to copy files and folders one folder to another on remote machine by using ansible-playbook?

i am new to ansible. i am trying to copy the files recursively ,got an error.
it does not support recursive.
host.ini
[web]
server1
server2
server3
ansible-playbook1:
tasks:
- name: copy files on remote machine
copy:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
remote_src: yes
i tried like this.but got error recursive problem.
tasks:
- name: copy files on remote machine
synchronize:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
deligate_to: {{ ansible_hostname }}
i also tried like this . but it copied folders and files on one of the server.
help me how to solve this plroblem/

Resources