Saranya Sridharan
Saranya Sridharan

Reputation: 226

Ansible copy from the remote server to ansible host fails

I need to copy the latest log file from remote linux server to the ansible host. This is what I have tried so far.

- hosts: [host]
  remote_user: root
  tasks:
  - name: Copy the file
    command: bash -c  "ls -rt | grep  install | tail -n1"
    register: result
    args:
      chdir: /root
  - name: Copying the file
    copy:
      src: "/root/{{ result.stdout }}"
      dest: /home

But I am getting the following error .

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok

TASK [Copy the file] **********************************************************************************************************************************************************************************************
changed: => {"changed": true, "cmd": ["bash", "-c", "ls -rt | grep  install | tail -n1"], "delta": "0:00:00.011388", "end": "2017-06-14 07:53:26.475344", "rc": 0, "start": "2017-06-14 07:53:26.463956", "stderr": "", "stdout": "install.20170614-051027.log", "stdout_lines": ["install.20170614-051027.log"], "warnings": []}

TASK [Copying the file] *******************************************************************************************************************************************************************************************
fatal: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find 'install.20170614-051027.log' in expected paths."}


PLAY RECAP ********************************************************************************************************************************************************************************************************
            : ok=2    changed=1    unreachable=0    failed=1

But that file is right there.Please help me resolve this issue.

Upvotes: 0

Views: 435

Answers (2)

Saranya Sridharan
Saranya Sridharan

Reputation: 226

This one works , i have to use fetch instead of copy to get the file from remote .

 - name: Copy the file
    command: bash -c  "ls -rt | grep  install | tail -n1"
    register: result
    args:
      chdir: /root
  - name: Copying the file
    fetch:
      src: "/root/{{ result.stdout }}"
      dest: /home
      flat: yes

Upvotes: 0

hewo
hewo

Reputation: 816

Ansible Copy copies files from ansible host to remote host. Use Ansible fetch instead. http://docs.ansible.com/ansible/fetch_module.html

Upvotes: 1

Related Questions