Deployment Application on Instances
Ansible provides open-source automation that reduces complexity and runs everywhere. Using Ansible lets you automate virtually any task.
Deployment application on an instance
Prerequisites:-
One Ansible Master Node
One or more Ansible Hosts Node
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
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/

Create a file like ansible_key
vi ansible_key

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.

Grant access to the key.
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/ansible_key
Access the host node from the master node.

Installation of Ansible
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:
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

[servers]
server1 ansible_host=Host-1_IP_address
server2 ansible_host=Host-2_IP_address
[all:vars]
ansible_python_interpreter=/usr/bin/python3
Validate Your Inventory File
After creating your inventory file, you can validate it by running the following Ansible command:
ansible-inventory --list -y -i /etc/ansible/hosts

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

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

To check the 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:
sudo mkdir -p /etc/ansible/playbook/
cd /etc/ansible/playbook/
vi create_file.yml
Adding a creating a file task to your playbook.

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

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

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

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

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