Deployment Application on Instances

Ansible provides open-source automation that reduces complexity and runs everywhere. Using Ansible lets you automate virtually any task.

Prerequisites:-

One Ansible Control Node

One or more Ansible Hosts Node

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

Ubuntu 22.04

Installing Ansible

Update && upgrade the all Node

Run these commands on all the nodes

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

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 control 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 control 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.

1. 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:

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

2. 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

3. Validate Your Inventory File

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

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

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

Testing connection

After setting up the inventory file to include your servers, it’s time to check if Ansible is able to connect to these servers and run commands via SSH.

From your local machine or Ansible control node, run:

When executing any command with Ansible, it's crucial to always specify the path to the hosts 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

FOR CHECKING SPACES AND STORAGE

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

FOR CHECKING UPTIME OF ALL SERVERS

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

Preparing Playbook

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:

cd /etc/ansible/playbook/

$ vi create_file.yml

Adding a creating a file task to your playbook.

Running Your Playbook For The First Time

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.

Make sure when you run the playbook command go to the playbook directory

cd /etc/ansible/playbook

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

Adding a creating a user task to your playbook.

cd /etc/ansible/playbook

$ vi playbook_user.yml

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

Deploy Docker on Your Worker 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 the creating a playbook for installing docker.

cd /etc/ansible/playbook/

$ vi docker_install.yml

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

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.

Last updated