🤖How To Install Automation With Ansible

Introduction

Ansible is an open-source tool for software setup, configuration management, and application deployment. It can automate the process of server configuration and management, as well as application deployment and updates. Ansible uses a simple, human-readable language called YAML to describe tasks, and can work with a wide variety of systems and technologies. It is popularly used in IT operations, cloud provisioning, and software development.

The main function of Ansible is to automate the process of configuration management and application deployment. It allows you to define and manage server and application configurations in a simple, human-readable language called YAML. Ansible uses this configuration information to ensure that all servers in the network are configured in a consistent and predictable manner, and can also automate the application deployment and update process.

UseCases

  1. Automated configuration management: Ansible can help ensure that all servers in a network are configured in a consistent and predictable manner, reducing the risk of errors and downtime.

  2. Application deployment: Ansible can automate the application deployment and update process, making it easy to deploy new features and bug fixes.

  3. Automated provisioning: Ansible can be used to provision new servers and infrastructure, such as virtual machines or cloud instances, in a repeatable and consistent manner.

  4. Ad-hoc command execution: Ansible makes it possible to execute commands on multiple systems at once, making it easy to perform tasks such as starting or stopping services, or checking system status.

  5. Multi-node orchestration: Ansible allows to orchestrate tasks on multiple nodes, it can also handle a large number of systems at once, suitable for large-scale implementations.

  6. Agentless: Ansible does not require any agent installed on the target machine, using SSH or WinRM to communicate with the target machine, which makes it lightweight and easy to set up.


Toppology

Requirement

To follow this guide, you must prepare the following:

  1. Create a user with sudo privileges on each user.

  2. Have installed SSH on all nodes


Configuration

Debian11- Router, Node1, Node2

Install Sudo

sudo is a command in the Linux command-line. If you have root access, then sudo will execute commands as superuser. The sudo user and the commands they can use are found in the /etc/sudoers configuration file

apt install sudo

Create User

adduser rizwan29

Add to Sudo Group

usermod -aG sudo rizwan29

Switch User

This time we always use the user 'rizwan29' who has received permission from the sudo group

su rizwan29

Install SSH

Install the ssh package for all machines

apt install ssh

SSH Keygen (Debian11- Router)

Ssh-keygen functions to generate private keys and public keys that will be used for authentication when communication occurs between two hosts. The ssh-keygen function is really needed by system admins when accessing the server, with ssh-keygen the system admin doesn't need to bother typing in passwords anymore.

ssh-keygen -t rsa
ssh-copy-id -i /home/rizwan29/.ssh/id_rsa [email protected]
ssh-copy-id -i /home/rizwan29/.ssh/id_rsa [email protected]
ssh-copy-id -i /home/rizwan29/.ssh/id_rsa [email protected]

Debian11- Router

Install Ansible

sudo apt install ansible sshpass curl -y

Create an Inventory Hosts File

After installing Ansible on the Control node, the /etc/hosts file will be created automatically. In this file we can add managed nodes / nodes that will be configured. We can also create our own inventory file in the home directory.

sudo nano ~/.hosts
[node1]
20.1.1.20 ansible_ssh_pass=123 ansible_ssh_user=rizwan29
    
[node2]
20.1.1.30 ansible_ssh_pass=123 ansible_ssh_user=rizwan29

Check Connection Between host & node

sudo ansible -i ~/.hosts -m ping all

After a ping request is sent to the remote host, the module will return a value indicating whether the ping was successful. By default, the ping module will return the string "pong" if successful, and an excpetion (rejection) along with an error message (msg) if it fails.

After carrying out a ping test on the managed node, we will test whether the managed node can receive commands from the control node by checking the available memory on the managed node.

sudo ansible -i ~/.hosts -m shell -a "free -m" all

Cek IP Address Node

sudo ansible -i ~/.hosts -m shell -a "ip address" node1
sudo ansible -i ~/.hosts -m shell -a "ip address" node2

Install the Application

In this tutorial, we will install on managed nodes using the Playbook file. We will install the Apache2 Web Server, and check system-uptime (server startup time) on node 2. For this we create a Playbood file on the Controller (Router).

sudo nano playbook.yaml
---
- hosts: node2
    become: yes
    tasks:
    - name: Install web server apache2 terbaru di Debian 11 node
        apt: name=apache2 state=latest
    - name: Start service apache2
        service:
            name: apache2
            state: started

    - name: Cek uptime dari remote host
        shell: uptime
        register: command_output

    - debug:

                var: command_output.stdout_lines

Run the Script

sudo ansible-playbook -i ~/.hosts playbook.yaml -K

Check Status Apache2 (Node2)

sudo ansible -i ~/.hosts -m shell -a "systemctl status apache2" node2

Check HTPP (Node2)

curl http://20.1.1.30

Last updated