How to copy a file from localhost (Ansible Tower server) to remote host in Ansible playbook?

54 views
Skip to first unread message

Saravanan

unread,
Nov 19, 2018, 9:15:39 AM11/19/18
to Ansible Project
Hi Ansible Experts,
Need to copy a file from Ansible tower server to remote server using copy module. Can you please help to achieve this using copy module or any other module in Ansible?

---
- name: Copy file to remote server
 
gather_facts: no
 
hosts: 127.0.0.1

  tasks
:
   - name: Copy JSON file to remote server
      copy:
        dest: /apps/web/scripts/json
        src: /tmp/report.json
        remote_src: no
        owner: user
        group: group
        mode: 0644
      delegate_to: remote_server
      become_user: awx
      become: no



Saravanan

unread,
Nov 19, 2018, 4:25:48 PM11/19/18
to Ansible Project
I am getting source not found error while copying a file from Ansible Tower server to remote server. Actually file is available in the local path of Ansible Tower server. Any help is much appreciated.

Jonathan Lozada De La Matta

unread,
Nov 19, 2018, 4:35:53 PM11/19/18
to ansible...@googlegroups.com
do you have the file in all the nodes?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/993a7441-8ea0-4181-9e87-4bfb715876ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Jonathan lozada de la matta

AUTOMATION PRACTICE



 

Saravanan

unread,
Nov 19, 2018, 4:42:22 PM11/19/18
to Ansible Project
I have a temporary file created on Ansible Tower and able to send it via email. Additionally I would like to copy this file to remote server on some path. Can you please help me with this requirement?

Saravanan

unread,
Nov 20, 2018, 7:33:57 AM11/20/18
to Ansible Project
Hi Jon,
The file is exist on Ansible tower server and able to lookup file, attach to email. However, using copy module, unable to copy this file from Ansible Tower server to remote server path.
- hosts: remoteserver
  tasks
:
 
- name: Ansible copy file to remote server
    copy
:
      src
: /tmp/somefile.txt
      dest
: /opt/remote/server/path

Getting strange error. saying unable to find src file. On the other hand able to lookup file and attach to email.



On Monday, 19 November 2018 16:35:53 UTC-5, Jonathan Lozada De La Matta wrote:

Saravanan

unread,
Nov 24, 2018, 2:48:20 PM11/24/18
to Ansible Project
Found the solution for copying a file from Ansible Tower server to a remote server when the Play is delegated to Tower server.

---
- name: Copy file to remote server
 
gather_facts: no
 
hosts: 127.0.0.1

  tasks
:

 
- set_fact:
      jsonVariable: "{{ lookup('file', '/tmp/myfile.json') }}"
      cacheable: true

 
- name: Create empty file on remote server
   
file:
      path: "{{ item }}"
      state: touch
     
mode: 0750
   
delegate_to: remoteServer
   
with_items:
      - /opt/json/myfile.json

 
- name: Copy file to remote server
   
lineinfile:
      dest: /opt/json/myfile.json
      line: "{{ jsonVariable }}"
      insertafter: EOF
   
delegate_to: remoteServer
Reply all
Reply to author
Forward
0 new messages