Ansible 1 – Experiment 1: Copying file on remote machine using a simple inventory file and playbook.yaml

This blog is the first in the series where I am attempting to share my experiments on using Ansible. The first experiment that I took upon is copying files on remote machines using ansible inventory file and playbook files.

What exactly is ansible

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. It is one of the most important pieces today in the DevOps world, allowing us to treat infrastructure as a code.

One very good reference site where I have copied the first sentence is

You can actually refer to see loads of information and knowledge.



  • You have a Linux Machine
  • You have installed Ansible on that

If you do not have a linux machine – > Create an account on GCP/Azure/Amazon and get your first ComputeEngine/Azure virtual machines/AWS EC2 instance. I would recommend GCP or Azure for the simplicity and usability compared to AWS EC2 (again the hard core Amazon fans will beat me on this 🙂

I have Ubuntu 16.04 cloud machine.

Installation of Ansible is extremely simple, follow these steps

$ sudo apt update
$ sudo apt install ansible

or refer the following page for installation of Ansible

Let us jump right into action now

Problem statement -> Using ansible copy a simple file on different remote machines (hosts) from the controlling or management host.

Ansible works in the following way:


  • Ansible management node is the controlling node, which controls the entire execution of the Playbook. This is the node where we have to install Ansible on
  • Management node has to have two set of information – a) set of hosts (inventory file) on which we want to perform some actions (playbook)
  • Management node uses ssh connection to the hosts mentioned in the inventory file and perform the actions mentioned in the playbook file.

Sample inventory file code I am using is a normal text file – code is as below:

# Sample Inventory File - inventory.txt

# remote servers which we want to control from a controller machine. We want to copy a file to the machine listed here.

Host1 ansible_host= ansible_connection=ssh ansible_user=<> ansible_ssh_pass=<>
Host2 ansible_host= ansible_connection=ssh ansible_user=<> ansible_ssh_pass=<>
Host3 ansible_host= ansible_connection=ssh ansible_user=<> ansible_ssh_pass=<>
Host4 ansible_host= ansible_connection=ssh ansible_user=<> ansible_ssh_pass=<>

# Groups for ease of management, groups are in square bracket


-- INSERT --                                                                                                                                                                  1,2           All

Sample playbook file – Playbook1.yaml is below:

# sample playbook to copy a file
  name: Copy files on remote servers part of group1
  hosts: Host1
      name: copy inventory file
        src: ~/bhagyashree/testfile.txt
        dest: ~/bhagyashree/testfile1.txt

"playbook1.yaml" [dos] 13L, 260C                                                                                                                                              1,0-1         All


The simple steps that I took to perform the first simple experiment of copying the file on host from the management node are as below


STEP 1: Create an inventory file containing a remote host as a part of a group

The sample inventory file shown above contains a variable ‘Host1’, which defines the host IP address, connection type and credentials required to connect to it. More hosts, which are to be remotely managed, can be listed in similar fashion.

Generally the hosts on which ansible would execute the commands, modules and tasks within a playbook to change the configurations or retrieve the health statistics are listed in a file named as inventory.

The hosts can then be added to a group such as [group1] defined within the inventory file. Several groups can be created depending upon the tasks to be executed upon them. Groups enable logical classification of the hosts within the inventory file.


STEP 2: Create a test file on ansible controller

In the screenshot below, the ansible controller machine shows the testfile.txt that is to be copied on to the remote machine

Before the playbook is executed, the folder contains three files only viz. inventory.txt, playbook1.yaml and testfile.txt.

STEP 3: Create ansible playbook (YAML) to copy the file

In the playbook shown above, the copy module takes src (path of file to be copied) and dest (remote absolute path where the file should be copied to) as inputs to perform the file copy operation. Note that indentation is important while defining the tasks within YAML.

STEP 4: Execute the ansible playbook (YAML file)

After the playbook is been executed, the testfile1.txt is copied to the host machine.


Hope you enjoyed my first attempt at writing a blog. I would like to mention that this blog is supported by Agile Testing Alliance and DevOps++ Alliance community initiative of helping folks like me and many others.

You can reach out to me

and ATA Linked in page


Note from DevOps++ Alliance

If you want to learn practical devops and experiment with DevOps Pipeline – do not miss our CP-DOF programs


Leave a Comment