Ann
Ann

Reputation: 231

Ansible playbook copy failed - msg: could not find src

I am new to ansible and I am trying to clopy a file from one directory to another directory on a remote RH machine using ansible.

---
- hosts: all
  user: root
  sudo: yes
  tasks:

  - name: touch
    file: path=/home/user/test1.txt state=touch

  - name: file
    file: path=/home/user/test1.txt mode=777

  - name: copy
    copy:  src=/home/user/test1.txt dest=/home/user/Desktop/test1.txt

But it throws error as below

[root@nwb-ansible ansible]# ansible-playbook a.yml -i hosts 
SSH password: 

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [auto-0000000190]

TASK: [touch] ***************************************************************** 
changed: [auto-0000000190]

TASK: [file] ****************************************************************** 
ok: [auto-0000000190]

TASK: [copy] ****************************************************************** 
failed: [auto-0000000190] => {"failed": true}
msg: could not find src=/home/user/test1.txt

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/root/a.retry

auto-0000000190            : ok=3    changed=1    unreachable=0    failed=1   

[root@nwb-ansible ansible]# 

The file has created in the directory and both the file and the directory has got permissions 777.

I am getting the same error message if I try to just copy already existing file using ansible.

I have tried as non-root user as well but no success.

Thanks a lot in advance,

Angel

Upvotes: 4

Views: 15237

Answers (3)

Namit Agarwal
Namit Agarwal

Reputation: 81

Luckily this is a simple fix, all you need to do after the copy is add

remote_src: yes

Upvotes: 5

helloV
helloV

Reputation: 52385

What is your ansible version? Newer version of ansible supports what you want. If you cannot upgrade ansible, try cp command for simple file copy. cp -r copies recursively.

  - name: copy
    shell: cp /home/user/test1.txt /home/user/Desktop/test1.txt

Upvotes: 0

Raul Hugo
Raul Hugo

Reputation: 1136

If you have ansible >=2.0 you could use remote_src, like this:

---
- hosts: all
  user: root
  sudo: yes
  tasks:

  - name: touch
    file: path=/home/user/test1.txt state=touch

  - name: file
    file: path=/home/user/test1.txt mode=777

  - name: copy
    copy:  src=/home/user/test1.txt dest=/home/user/Desktop/test1.txt remote_src=yes

This don't support to recursive copy.

Upvotes: 0

Related Questions