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 in Ansible by Nishant
• 160 points
806 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 by Kalgi
• 35,880 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

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

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

answered Jan 16 in Ansible by Danica
49 views
0 votes
1 answer

Ansible playbook for moving files from one node to another remote node

The best way to do this is: copy ...READ MORE

answered Jan 16 in Ansible by Nigya

edited Jan 18 by Vardhan 368 views
0 votes
2 answers

How do you stop Ansible from creating .retry files in the home directory?

[defaults] ... retry_files_enabled = True # Create them ...READ MORE

answered Apr 5 in Ansible by anonymous
449 views
0 votes
1 answer

devops , how to create user using ansible playbook

root doesn't need a password to switch ...READ MORE

answered Jul 16, 2018 in Ansible by Kalgi
• 2,620 points
54 views
0 votes
1 answer
+13 votes
2 answers
0 votes
1 answer

Hey I'm trying to execute a playbook but I get the following error:

Hey, you have a syntax error as ...READ MORE

answered Mar 26 in Ansible by Kalgi
• 35,880 points
9 views
0 votes
1 answer

One loop over multiple Ansible tasks

With_items has unfortunately stopped but it’s coming ...READ MORE

answered Jul 23, 2018 in Ansible by Kalgi
• 35,880 points
449 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.