Ansible For Everyone(Practical) - Part 2
Tags: devops, webdev, productivity, opensource
Published: 2022-06-10 02:10:56

## DAY 33- Ansible For Everyone(Practical) - Part 2 - Day Thirty Three

[![Image tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49arpnjjyf762k9w8vuz.png)![Image cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5aqcyrl43zd3523mwd5.png)](https://twitter.com/intent/tweet?text=Ansible%20For%20Everyone(Practical)%20-%20Part%202%20SETUP%20YOUR%20ANSIBLE%20%20INFRASTRUCTURE%2C%20a%20blog%20by%20%40anuvindhs%20%0Ahttps%3A%2F%2Fdev.to%2Faws-builders%2Fansible-for-everyonepractical-part-2-58a5%20)

[100 days of Cloud on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz) - [Read on Dev.to](https://dev.to/anuvindhs)

------

## 📘 Please read Part one to understand bits and basics of ANSIBLE.

{% embed https://dev.to/aws-builders/ansible-for-everyone-part-1-5998 %}

-----

## ☁️ Lets setup the environment in AWS
- We will spin up one EC2 as slave and one EC2 as the Master Node or the control center for ansible.
- Then we use this Master node to connect the other EC2 instance (Slave).

##🪛 Prerequsite 

![Image image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67zsy2zhtg45j3pcv7dz.png)
 
 
 


- Access and privilages to launch EC2 with Ubuntu 
- [Putty](https://www.putty.org/) or [any other SSH tools](https://www.solarwinds.com/free-tools/solar-putty)
- Security group with ports ssh open (Port 22)

## Setup environment

### Lets launch 2 EC2 instances

- Use AWS EC2 console or CLI. i am using two ubuntu instances here for this lab.

```bash
aws ec2 run-instances \
--image-id <ami-id> \
--instance-type <instance-type> \
--subnet-id <subnet-id> \
--security-group-ids <security-group-id>\
--key-name <ec2-key-pair-name> \
--count 2
```

### Set up Ansible Control Center

- Rename one EC2 to **Ansible Control Center** on console

- Now lets SSH into Ansible Control Center EC2
 - Rename the server 
   ```bash
   sudo hostnamectl set-hostname Ansible-Control-Center
   ```
-  Now lets update and install ANSIBLE

Run these commands
```bash
sudo apt-get update
sudo apt install software-properties-common -y
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y 
```
### Set up Ansible Slaves - Your ProdServer

- Rename one EC2 to **Ansible Slaves - Your ProdServer** on console
- Rename the server 
   ```bash
   sudo hostnamectl set-hostname AnsibleSlaves-YourProdServer
   ```
- Install Python
  ```bash
  sudo apt-get update
  sudo apt-get install python3 -y
  ```

### Configure SSH access 

#### On Ansible Control Center
- Genarate a rsa key.
```
cd .ssh
ssh-keygen
```
- Press enter ,we donot require any changes.
![Image rsa key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2ejause8mcjcgoht69q.png)
 
- Now if we `ls` we will be able to see ` id_rsa` & `id_rsa.pub`, lets cat id_rsa.pub.

```
cat id_rsa.pub
``` 

_Note down this key_

#### On Ansible Slaves

-  ssh into your slave server then,
```
cd .ssh
sudo nano authorized_keys
```

- Now on next line paste the key we previously noted & save it.

### Now its play time.

![Image its play time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40jbtfq607ssz7485051.png)

#### Goto your Ansible Control Center EC2.
- lets ssh into ansible slave, 
 ![Image ssh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xebhywuncyhxht98nmxe.gif)
 

{% embed https://dev.to/aws-builders/ansible-for-everyone-ansible-playbooks-part-3-2dgj %}
{% embed https://dev.to/aws-builders/ansible-for-everyone-part-1-5998 %}


----------

**✅Connect with me on [Twitter](https://twitter.com/intent/follow?ref_src=anuvindhs&screen_name=anuvindhs)
 🤝🏽Connect with me on [Linkedin](https://www.linkedin.com/in/anuvindhs)
🧑🏼‍🤝‍🧑🏻 Read more post on [dev.to](https://dev.to/anuvindhs) or  [iCTPro.co.nz](https://ictpro.co.nz)
💻 Connect with me on [GitHub](https://github.com/anuvindhs)**

----------
{% user anuvindhs %}