Ansible Copy Command

We will learn how to use the copy module, one of the most crucial Ansible modules, in this guide. We will put the various Ansible copy module scenarios into practice. We keep things simple so that the users could comprehend the copy module idea.

The copy module is one of the important modules of Ansible which is the built-in module in the Ansible tool. The directories and files are replicated by utilizing the Ansible copy modules. The copy module has sufficient flexibility. It is employed to copy the information and files from a local system to the target remote hosts. The copy module can transfer the files that have had parameters substituted for them, such as the configuration files with IP address modifications. You can carry out numerous intricate activities in the Ansible copy module.

Different Parameters of Ansible Copy Module

Considering the following are the several variables that we employ while putting the examples into practice.

Required Parameters:
To replicate the directories and files on the target remote host, the copy module requires specific arguments.

Dest: The document or directories’ final destination path. If the original path is a directory and the target directory is not specified, the destination directory is created automatically; however, if the original route is a document and the destination path is not specified as a document, an exception is thrown. The exact relative pathway should bring you to the targeted point.

Optional Parameters:
Although there are additional possible optional parameters for the Ansible Copy command, we only focus on a few of them. We may be required to completely comprehend this component.

src: The directories and documents’ original path. Depending on the context, this pathway may be either relative or absolute. The documents and subdirectories are copied iteratively if the originating location is the directory. It replicates the information of the directory if the originating address is a dictionary and ends with /. Alternatively, it duplicates the directory that bears the given name to the targeted path.

force: If there have been any document modifications, it modifies the document and its default mode to Yes. If the option is No, the documents have been transferred. However, they are not present on the desired route.

Prerequisites of the Ansible Copy Command

Similar to all other modules of Ansible, the copy module has a set of requirements that must be met before it can be implemented in an Ansible playbook. These requirements are the following:

  • The remote host device that has an SSH connection must be active. If it is not active, you have to build the inventory file to make the SSH connection.
  • The credentials that are set up for Ansible must be entered.

Example 1: Duplicating the Document on the Target Remote Host Device

We implement the very first scenario of the copy module. We duplicate the document from the Ansible controller to the targeted local host location. To do this, we first create the playbook to write the script and mention the path of both the controller and the local host in it. The following is the command to build the Ansible playbook:

[root@master ansible]# nano copy_module.yml

After creating the copy_module playbook, it now automatically opens a new terminal screen. First, we pass the small description of the example that we implement in the playbook so that the user can easily understand the example in the “name” parameter. Then, we provide a “false” value to the “gather_fact” variable so that we can receive the extra information from the remote host. Now, we specify the remote host in the “hosts” parameter which is the localhost. We also tell the connection type which is the “local connection”.

Next, we start listing the tasks in the ansible-playbook by writing the name of the task first. Then, we write the “copy” variable. We then pass the “src” and “dest” so that we can specify the controller path to duplicate the document and paste it into the localhost specified location.

Quit the copy_module.yml playbook. In this section, we will not make the inventory file to make the connection because we are using the local host here. To display the results, we write the following statement:

[root@master ansible]# ansible-playbook copy_module.yml

The following screenshot is the output of the previously-executed playbook. In this output, it shows us the “ok” and the “changed” signals which tell the user that the tasks and commands in the “copy_module” playbook are executed successfully.

Example 2: Duplicating a Document to a Directory that Doesn’t Exist in the Local Host

In this example, we duplicate the document to the location where the directory is not available in the local host device. For example, if we have a document that we want to duplicate to the new directory with the exact name as the original document, the copy module creates the new directory if the target directory does not exist on the target device. First, we open the copy_module.yml playbook. Then, we make changes according to the example requirements.

[root@master ansible]# nano copy_module.yml

In the playbook, we change the destination path in the “dest” parameter.

Now, we run the playbook to get the desired output in the terminal by writing the following statement:

[root@master ansible]# ansible-playbook copy_module.yml

Example 3: Duplicating a Document with the Permission of the Owner

Here is the third scenario of the Ansible copy module where we copy the document with the permission of the owner to the target remote host. In the playbook, we additionally add the owner, group, and mode parameters.

[root@master ansible]# nano copy_module.yml

Now, the Playbook is opened after writing the previous statement:

To get the results, we execute the following statement:

[root@master ansible]# ansible-playbook copy_module.yml

Here is the outcome after running the previous playbook command:

Example 4: Duplicating a Document to a Remote Location

In this illustration, we want to duplicate the document to the remote location using the copy module of Ansible.

[root@master ansible]# nano copy_module.yml

After opening the playbook, we modify the commands.

We want to duplicate the document to the remote host so we build the inventory file to make the connection between the Ansible controller and the remote host. Now, we create the inventory file through the following command:

[root@master ansible]# ansible-playbook copy_module.yml

After creating the inventory file, we build the connection.

      ansible_user: iris
      ansible_password: TpsTps_1
      ansible_connection: ssh
      ansible_port: 22

To get the desired output, run the following command:

[root@master ansible]# ansible-playbook copy_module.yml –i host.yml

Here is the result of performing the aforementioned command:


This article discusses about the Ansible copy module. We now understand the various copy module’s mandatory and optional parameters. Multiple examples are included so that the users can grasp them fully.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content