Ansible Copy

Ansible Copy: Ansible gives the usefulness of duplicating the documents and indexes with the use of copy and fetch modules. This copy module is quite flexible.

The copy module is used to duplicate documents and organizers from the nearby machine to the remote servers. Also, the get module duplicates the information from the remote system to the local system.

Suppose you want to duplicate documents in the wake of subbing with variables, such as configuring with IP customization or utilizing the template module, which we have learned in the previous part of this tutorial. You can play out plenty of confounded tasks with the help of this module.

Replicating Files from Local to Remote

The copy module is used to check the record set in the src parameter on the nearby machine. Then, it will duplicate the information to the remote machine indicated in the destpath.

In the following model, we will copy the example.txt document in the remote machine's home directory to the objective is the/tmp index on the remote server. However long we are not indicating any authorization for the record, the remote document's default consent is set as - rw-rw-r-(0664).

 - hosts: blocks
   tasks:
   - name: Ansible duplicate document to a distant server
     copy:
          src: ~/example.txt
          dest:/tmp 

Case 1: If the document is present on the remote machine or server, yet the source record's content is unique, at that point, the destination document will be updated. You can handle this by setting the force parameter. The default is fixed to yes. So, it changes the document naturally.

On the off chance that you don't need the record to be adjusted, on the off chance that the source document is unique, at that point, you can fix it No. The accompanying errand will duplicate the record if the document doesn't exist on the remote machine.

 - hosts: blocks
   tasks:
   - name: Ansible duplicate record force
    copy:
        src: ~/sample.txt
        dest:/tmp
        force: no 

Case 2: If the record didn't find on the local system, the Ansible toss a blunder.

For instance: lethal: [remote-machine-1]: FAILED!=> {"changed": false, "failed": true, "msg": "unfit to discover '~/sample.txt' in expected paths."}

Replicating Directories from Local to Remote

You can also duplicate folders or registries using the Ansible copy module. On the off chance that the 'src' path is a registry, at that point, it will be duplicated recursively. Or on the other hand, the whole index will be replicated.

There are two distinct varieties for this assignment. Contingent upon whether you have the '/' character at the endpoint of the 'src' path or not.

The principal technique will make a directory or registry on the remote server with the src parameter's name. At that point, it will reorder the content of the source folder into that index or registry.

If you need this conduct, it doesn't give the '/' after the way in the src boundary.

The following model will initially make a registry named copy_dir_ex in the/tmp of the remote machine.

 - hosts: blocks
   tasks:
   - name: Ansible copy the registry to the remote server
     copy:
        src:/Users/tutormd/Documents/Ansible/copy_dir_ex
        dest:/Users/tutormd /Documents/Ansible/tmp 

Replicating Files between Directories on Remote Machine

Ansible copy module permits you to duplicate the records starting with one registry then onto the next on the same remote server. Yet, this is just for documents, not for the registries. You can use the remote_src parameter to tell Ansible for your goals.

The following code will duplicate/tmp/check.txt to the client's home catalog (/home/[username]/).

 ---
 - hosts: webservers
   tasks:
   - name: copy the record between catalogs on a remote server
     copy:
         src:/tmp/check.txt
         dest: ~/check.txt
         remote_src: yes 

Return Values 

Normal return esteems archived here; coming up next are the fields remarkable to this module:

keyreturnedDescription
backup_file (string)altered and if backup=yesName of backup file created Sample: /path/to/file.txt.2020-07-13@13:09~
checksum (string)achievedSha1 checksum of the file after executing copy Sample: 486df351vf1v5vdvd3f5v1d5vdfdf5v31f51v13358468
dest (string)achievedDestination file/path Sample: /path/to/file.txt
gid (int)achievedGroup id of the file, after execution Sample: 200
group (string)achievedGroup of the file, after execution Sample: httpd
md5sum (string)when supportedmd5 checksum of the document subsequent to running duplicate Test: 2a5aeecc61dc98c4d780b14b330e3282
mode (string)achievedconsents of the destination, after execution Test: 420
owner (string)achievedproprietor of the document, after execution Test: httpd
size (int)achievedsize of the destination, after execution Test: 1220
src (string)changedsource document utilized for the duplicate on the destination machine Test: /home/httpd/. ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source
state (string)achievedcondition of the destination, after execution Test: document
uid (int)achievedproprietor id of the document, after execution Test: 100