Rolando
Rolando

Reputation: 62624

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

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?

Upvotes: 24

Views: 114904

Answers (3)

kosciej16
kosciej16

Reputation: 7128

From Ansible 2.10 the core modules was reorganized into the namespaces and the documentation suggests to use ansible.builtin.copy:

- hosts: targetserver
  tasks:
    - ansible.builtin.copy:
        src: /users/rolando/myfile
        dest: /users/rolando/myfile

Upvotes: 0

Arvin
Arvin

Reputation: 395

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 

Upvotes: 5

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

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

Upvotes: 28

Related Questions