Setting up Ansible for Automating tasks
Tags: ansible, automation, learning, linux
Published: 2023-11-18 06:19:07

## Setting up Ansible

Hey Fellas, in this article, we will be installing Ansible and creating a simple task of automation using Ansible and showing you all the processes involved.

![Ansible](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvhd3ynpxdx3vder9565.png)

> We need to create 3 Amazon EC2 instances, 1 will be the Master while the other 2 will be the targets.

After creating the Instances, we can name one of them Ansible_Master while the other two Ansible_Target1 and Ansible_Target2 to simulate.

### Step 1: Install Ansible 
 
With the following command, we can install ansible

```sudo yum install ansible```

Create the ec2-key.pem file and paste your key into this file

It should look something like this if you run the `cat ec2-file.pem`

![Key description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gzrmoedtsvulsqpkqdj.png)

Run chmod on the key file to ensure it is not accessible to anyone and only the root user can read the file.

```
chmod 400 ec2-file.pem
```

The image below shows how to use Ansible to call different servers all at the same time


![Asn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e44sakhlz767n7yw0cu8.png)

### Step 2: Create Inventory File

Use the nano program to create the inventory.txt file

```
nano inventory.txt
```

```
ansible-target-1 ansible_host=16.170.230.179 ansible_connection=ssh ansible_user=ec2-user
ansible-target-2 ansible_host=51.20.116.72 ansible_connection=ssh ansible_user=ec2-user
```

Escape and save the file. To run the file, we can use the following

```
ansible ansible-target-1 -m ping -i inventory.txt
```

We should get this error and i will explain in a sec why that is

![Ansible Error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53sufzorvap2oonmsgfu.png)

In the inventory.txt file the mode of connection we used is SSH and we need the SSH key for the servers we are trying to connect to, that is why we are receiving this error resposne from the server.

We need to assign the SSH key we copied at the beginning when installing our instances to the ssh-agent using the command below

```
ssh-agent bash
cp ec2-key.pem ~/.ssh/
ssh-add ~/.ssh/ec2-key.pem
```

![Ansible](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f1myr8g140pd21rogrt.png)

Running this command again

```
ansible ansible-target-1 -m ping -i inventory.txt
```

![Ansible1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vcpev6b52v9yeepjlwj.png)

```
ansible ansible-target-2 -m ping -i inventory.txt
```

![Ansible2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zlf1dl172a98smug4nl.png)

To ensure that we can connect to our servers (instances) at all times, we need to configure the ansible configuration file to allow us to do that without interruption.


![Ansile1-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwvrrx8wqb6h9atxbpgh.png)

If the ansible config file does not exist in the `/etc/ansible/ansible.cfg` we can create it by running the following

```
ansible-config init --disabled -f ini > /tmp/ansible.cfg
```

The config file will be sent to the `/tmp/ansible.cfg` (tmp folder) and then it can be moved to the main folder /etc/ansible

As a refresher, we can use 
```
sudo cp /tmp/ansible.cfg /etc/ansible
```
to copy the ansible config file into the main directory where it can be read.

Let's now try the command once again for both targets

```
ansible ansible-target-1 -m ping -i inventory.txt 
```

```
ansible ansible-target-2 -m ping -i inventory.txt 
```

Assuming we are having a hundred or more targets, do we keep changing the targets number using the above command? NO

Here is a better way to ensure we can communicate to all the targets at once:

Let's add a tag-name `servers` to the inventory.txt file we created earlier. 

Your inventory.txt file should now look like this

![Inventory file updated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vxhlzkzd3odhol3to3w.png)

Run this command to see the magic all targets can now be connected to at the same time with a single command

```
ansible servers -m ping -i inventory.txt
```

![Ansible description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2lrmdimjkxp2ng0nb9yc.png)