# Deployment Application on Instances

## **Deployment application on an instance** <a href="#prerequisites" id="prerequisites"></a>

## **Prerequisites:-** <a href="#prerequisites" id="prerequisites"></a>

One Ansible **Master Node**

One or more **Ansible Hosts Node**

{% hint style="info" %}
Make sure all the Nodes create the Same SSH\_KEY
{% endhint %}

Check Ansible Master node is the machine we’ll use to connect to and control the Ansible hosts over SSH.

**Ubuntu 22.04**

### **Update && upgrade the all Node**  <a href="#prerequisites" id="prerequisites"></a>

Run these commands on all the nodes

```
sudo apt-get update -y && apt-get upgrade -y
```

Connect all the host nodes with the Master node by using SSH\_KEY

Navigate to the **.ssh** directory to the master node

```
cd .ssh/
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FcdbARMKtxY4WkShleMWY%2Fimage.png?alt=media&#x26;token=36715098-88d6-4c63-a2b3-00f650840d84" alt=""><figcaption></figcaption></figure>

Create a file like ansible\_key

```
vi ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FziYL0jtB6pTRsZWuxEnH%2Fimage.png?alt=media&#x26;token=d8775a98-99d4-4dd5-a1c3-e92b4f176c73" alt=""><figcaption></figcaption></figure>

In **ansible\_key**, insert the key you downloaded when creating the server.

Open the downloaded key in Notepad and copy/paste it into the ansible\_key file.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F0hfm1k4XXR9FwexCPbvk%2Fimage.png?alt=media&#x26;token=514bce6c-0d64-444e-b587-d91b5c9baf62" alt=""><figcaption></figcaption></figure>

Grant access to the key.

```
chmod 700 ~/.ssh/
```

```
chmod 600 ~/.ssh/ansible_key
```

Access the host node from the **master node**.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FgAEfpPAO2G9km1CMI1nX%2Fimage.png?alt=media&#x26;token=e778c467-84e0-444b-8a8f-ab379eb12b25" alt=""><figcaption></figcaption></figure>

### **Installation of Ansible**&#x20;

Ansible as a means of managing your server infrastructure, you need to install the Ansible software on the machine that will serve as the Ansible master node.

To add the official project's PPA (Personal Package Archive) to your system's list of sources in the control node, execute the following command:

```
sudo apt-add-repository ppa:ansible/ansible
```

Next, refresh your master node

```
sudo apt-get update -y 
```

Following the update you can install the **Ansible** -

```
sudo apt-get install ansible -y
```

## Creating an Ansible Inventory File

To manage your nodes with Ansible, you need to create an inventory file. The inventory file contains information about the hosts you want to manage with Ansible. Here's a quick guide on how to create one.

**Choose Your Inventory Format**

Ansible supports two formats for inventory files: INI and YAML. The INI format is simpler and more commonly used for small projects or examples. The YAML format provides more flexibility and is preferred for more complex configurations.

**INI Format Example:**

```
[webservers]
webserver1.example.com
webserver2.example.com

[dbservers]
dbserver.example.com
```

**YAML Format Example:**

```yaml
all:
  children:
    webservers:
      hosts:
        webserver1.example.com:
        webserver2.example.com:
    dbservers:
      hosts:
        dbserver.example.com:
```

### **Create the Inventory File**

* **For INI Format**: Simply create a new file with a **.ini** extension (e.g., **inventory.ini**) and insert your host information following the INI example format.
* **For YAML Format**: Create a file with a **.yml** or **.yaml** extension (e.g., **inventory.yml**) and structure your host information as shown in the YAML example.

You can choose the format accordingly.

For creating an Inventory file run these commands:-

```
vi /etc/ansible/hosts 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FAPOWqTvTtStPhMOCmYzT%2Fimage.png?alt=media&#x26;token=6799dafd-db00-4df4-9dea-63bd98d1f6a8" alt=""><figcaption></figcaption></figure>

```
[servers]
server1 ansible_host=Host-1_IP_address
server2 ansible_host=Host-2_IP_address

[all:vars]
ansible_python_interpreter=/usr/bin/python3
```

### &#x20;**Validate Your Inventory File**

After creating your inventory file, you can validate it by running the following Ansible command:

{% hint style="info" %}

```
ansible-inventory --list -y -i path/to/your/inventory/file 
```

{% endhint %}

```
ansible-inventory --list -y -i /etc/ansible/hosts 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FM9nDX5qKqbuxvHJxF2mm%2Fimage.png?alt=media&#x26;token=0eda3bf8-535b-429f-83cd-db2f800f1582" alt=""><figcaption></figcaption></figure>

### To check connection between nodes

Once you have added your servers to the inventory file, verify that Ansible can connect to them and execute commands via SSH.

From your local machine or Ansible master node, run:

When executing any command with Ansible, it's crucial to always specify the path to the host file. You can observe the effect of this practice in the examples below.

```
ansible all -m ping -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FyORPLErU7zVUh6gUcaaF%2Fimage.png?alt=media&#x26;token=dcca17e8-7ecd-4c98-84d4-304a9b775a2c" alt=""><figcaption></figcaption></figure>

### To check storage and spaces

Run the following command to check the available spaces and storage.

```
ansible all -a "free -h" -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FKU2LoJaEdJk4kVGPb4t9%2Fimage.png?alt=media&#x26;token=124dc8e2-bb24-4bff-acab-6ce86532ae3d" alt=""><figcaption></figcaption></figure>

To check the Uptime of all servers ->

```
ansible servers -a "uptime" -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FUUzKtaqS9ORatT2bqD9Y%2Fimage.png?alt=media&#x26;token=bc29a8b2-ca42-4a4f-a08b-1797157fe5b0" alt=""><figcaption></figcaption></figure>

## Preparing Playbook <a href="#step-2-preparing-your-playbook" id="step-2-preparing-your-playbook"></a>

The playbook.yml file is where all your *tasks* are defined. A task is the smallest unit of action you can automate using an Ansible playbook. But first, create your playbook file using your preferred text editor:

```
sudo mkdir -p /etc/ansible/playbook/
```

```
cd /etc/ansible/playbook/
```

```
vi create_file.yml
```

Adding a creating a file task to your playbook.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F5r9TBQ2P1QFLPmVNjoGn%2Fimage.png?alt=media&#x26;token=de534d5a-8fcb-4537-b26d-77b90abfd647" alt=""><figcaption></figcaption></figure>

```
---
- name: This playbook will create a file
  hosts: all
  become: true
  tasks:
    - name: Create a file
      file:
        path: /home/ubuntu/server_file.txt
        state: touch
```

### ***Running Your Playbook***&#x20;

You’re now ready to run this playbook on one or more servers. Most playbooks are configured to be executed on every server in your inventory by default, but you’ll specify your server this time.

**Before running the playbook command, navigate to the playbook directory.**

```
cd /etc/ansible/playbook
```

```

ansible-playbook create_file.yml -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key

```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fj6Mx9rud3674iXehHCxk%2Fimage.png?alt=media&#x26;token=c25fb963-40a6-412d-a481-49fc6b27a9e3" alt=""><figcaption></figcaption></figure>

Now we are performing another task to add users to all servers.

Adding creating a user task to your playbook.

```
cd /etc/ansible/playbook
```

```
vi playbook_user.yml
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FPSThwjkLXveERE8ah76l%2Fimage.png?alt=media&#x26;token=7d2872d5-ee2b-4a01-9683-ab17b9ab6dee" alt=""><figcaption></figcaption></figure>

```
---
- name: This playbook creates a user
  hosts: all
  become: true
  tasks:
    - name: to create a user name testing_user
      user: name=server_testing_user
```

```
ansible-playbook playbook_user.yml -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FABmPZf4PfdqiVYE65YQb%2Fimage.png?alt=media&#x26;token=ef24cab4-9869-48c6-972b-65dd6f682708" alt=""><figcaption></figcaption></figure>

### **Deploy Docker on Your host nodes Through Ansible**

Deploying Docker on your worker nodes can be efficiently accomplished using Ansible, a powerful automation tool that simplifies complex configuration tasks and repetitive deployment processes. Below are the steps to achieve a seamless Docker installation across your nodes:

1\. Preparation of Inventory File

2\. Install Docker Role

3\. Create Your Playbook

4\. Execute Playbook

Make sure we install the docker by following these steps because docker does not install directly we are adding apt-key-gpg and repository. We are adding apt-key-gpg and a repository inside the playbook.

We have already created the Inventory host file, and now we the creating a playbook for installing docker.

```
cd /etc/ansible/playbook/
```

```
vi docker_install.yml
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F6iJymwHD19svAjnhAera%2Fimage.png?alt=media&#x26;token=651e71c5-03c8-432e-8757-666e574945be" alt=""><figcaption></figcaption></figure>

```
ansible-playbook docker-install.yml -i /etc/ansible/hosts --private-key=~/.ssh/ansible_key
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FRhEzj0TIG25noWb56129%2Fimage.png?alt=media&#x26;token=6be15a0a-54c9-427a-b4b4-52bb85c025b4" alt=""><figcaption></figcaption></figure>

Now check the installed docker and go through individual nodes.

```
docker -v
```

By following these steps, you'll successfully deploy Docker on your worker nodes using Ansible, streamlining your deployment process and ensuring a consistent environment across all nodes.
