How to copy directory and sub dirs s and files from source to remote using ansible playbook

0 votes
How to copy directory and sub dirs's and files from source to remote using ansible playbook.
Apr 2, 2019 in Ansible by Nishant
• 160 points
29,845 views

1 answer to this question.

0 votes

Hey @Nishant, you can use the copy module provided by Ansible to copy files and folders from the local server to the remote servers, all you've to do is change those file's/folder's permissions. 

Example:

- hosts: hosts
  tasks:
  - name: Ansible copy file to remote server
    copy:
      src: ~/sample.txt
      dest: /tmp 

If you wish to copy the files from the remote server to the local server then use the fetch module.

There's yet another module Template provided by Ansible which lets you use variables which can be modified like the IP address and then copied to another server. 

answered Apr 3, 2019 by Kalgi
• 52,360 points
Thanks Kalgi for response however my understanding is copy module copies only the files however I am looking for recursive directories copy let me know the code which can do this.
Kalgi can you help with the above request?
As far as I know, you use the copy module itself for copying recursive directories.

Or you can try something like this:

- hosts: remote-server-name
  gather_facts: no
  vars:
    src_path: "/path/to/source/"
    des_path: "/path/to/dest/"
  tasks:
  - name: Ansible copy files remote to remote
    shell: 'cp -r {{ src_path }}/. {{ des_path }}'
Job is getting passed however the files and dir not getting transferred/created on remote

below is the code as suggested by you

- hosts: Testservers

  gather_facts: no

  vars:

    src_path: "/home/ubuntu/devopscert"

    des_path: "/home/ubuntu/ansiblecopy"

  tasks:

  - name: Ansible copy files remote to remote

    shell: 'cp -r {{ src_path }}/. {{ des_path }}'

below is the tree path on the Jenkins server (ansible is on ame machine)

└── devopscert

    └── website

        ├── config.php

        ├── content

        │   ├── 404.php

        │   ├── about-us.php

        │   ├── contact.php

        │   ├── home.php

        │   └── products.php

        ├── functions.php

        ├── index.php

        ├── readme.md

        └── template

            └── template.php

Below is the tree path on the destination/remote server

├── jenkins

└── ubuntu

    ├── ansiblecopy

    ├── devopscert

    └── dockertest

        └── Dockerfile

Below is the output of the Jenkins job

Building in workspace /var/lib/jenkins/workspace/Ansible_Copy2

[Ansible_Copy2] $ /usr/bin/ansible-playbook /etc/ansible/ansiblecopy2.yml -i /etc/ansible/hosts -f 5 --private-key /tmp/ssh7705887690036058683.key -u jenkins

[DEPRECATION WARNING]: DEFAULT_SUDO option, In favor of Ansible Become, which

is a generic framework , use become instead. This feature will be removed in

version 2.8. Deprecation warnings can be disabled by setting

deprecation_warnings=False in ansible.cfg.

[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become,

which is a generic framework. See become_user. , use become instead. This

feature will be removed in version 2.8. Deprecation warnings can be disabled by

 setting deprecation_warnings=False in ansible.cfg.

[DEPRECATION WARNING]: The sudo command line option has been deprecated in

favor of the "become" command line arguments. This feature will be removed in

version 2.9. Deprecation warnings can be disabled by setting

deprecation_warnings=False in ansible.cfg.

PLAY [Testservers] *************************************************************

TASK [Ansible copy files remote to remote] *************************************

changed: [webservers]

PLAY RECAP *********************************************************************

webservers                 : ok=1    changed=1    unreachable=0    failed=0   

Finished: SUCCESS
Try changing the directory permissions. Those files should be accessed by the remote system.
changed the directory permissions to RWX to all still same result - files/directories not getting copied
Hi - Can you please help here?

Sorry @Nishant, for the delayed response. I tried the exact same thing on my system and it seems to be working fine. 

As mentioned earlier, change the file permissions so that they are accessible by Ansible. 

chmod -R 755 

Related Questions In Ansible

0 votes
1 answer

How to copy multiple files to remote system using Ansible Playbook?

Hi@akhtar, You can use the copy module in ...READ MORE

answered Sep 16, 2020 in Ansible by MD
• 95,440 points
10,209 views
0 votes
1 answer

How to copy files in remote system using Ansible?

Hi@akhtar, You need to use the copy module ...READ MORE

answered Jul 30, 2020 in Ansible by MD
• 95,440 points
2,016 views
0 votes
2 answers

How to create a directory in remote nodes using Ansible-Playbook?

You can read install and setup apache ...READ MORE

answered Aug 23, 2020 in Ansible by anonymous
• 160 points
3,564 views
0 votes
1 answer

Ansible playbbok to copy files from one node to another remote node

You can create a playbook for copying ...READ MORE

answered Jan 16, 2019 in Ansible by Danica
3,775 views
+1 vote
1 answer
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,458 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP