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
7,556 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 by Kalgi
• 41,990 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
436 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 1,209 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
815 views
0 votes
1 answer
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
205 views