Ansible for DevOps and Cloud

Ansible for DevOps and Cloud

What ?

Ansible is an open-source automation tool that is widely used for configuration management, application deployment, task automation, and IT orchestration. Ansible is designed to be simple, agentless, and easily extensible.

Why ?

  • Agentless Architecture

  • Simple and Human-Readable Syntax: Ansible uses YAML

  • Extensibility and Modules

  • Integration with Cloud Platforms

  • Open Source and Active Development

  • Community and Ecosystem

How to Install Ansible?

For Debian/ubuntu

sudo apt update
sudo apt install ansible

For RedHat/Rocky/CentOS

sudo yum update
sudo yum install ansible

Usage and Commands:

Configure Ansible SSH-KEY:

Ansible uses SSH Key to login the remote server. let's create the SSH key

  • Create a SSH Key for the server.
#Ansible Server
ssh-keygen

Method 1: Copy SSH Key

Once the Key created, copy the public key using below command

#Ansible Server
cat ~/.ssh/id_rsa.pub

login to Remote client ,here save the key to file called authorized_keys.

#Remote Client
vim ~/.ssh/authorized_keys

<Paste_your_key_here>

Method 2: Copy SSH Key using ssh-copy-id

ssh-copy-id username@IP_address

Once Key copied check the connection, if its working without password authentication? if works good to Go..>

ssh username@<remote_host>

Make Inventory File:

Inventory file is used to store the client servers information and we can add group of servers.

Example: inventory

[server_group1]

192.168.111.1

192.168.111.2

[server_group2]

192.168.111.10

192.168.111.12

Ansible Execution

Execute the simple commands: ls

ansible -i inventory server_group1 -m bash -a "ls"
  • -i --> Inventory group_name

  • -m --> Module

  • -a --> Arguments

ansible 192.168.111.1 -m command -a "date" --user admin  --ask-become-pass
  • -m --> Ansible Module name

  • -a --> Arguments (Linux Command)

  • --user --> Remote client user_name [if you are using different user]

  • --ask-become-pass --> Enter for password [only --user used]

Install MariaDB using Ansible:

Write a Ansible playbook for installing MariaDB on RedHat/Rocky/Centos

save the file as ansible_mariadb.yml

Note: The user must need root access to perform installation task

---
- name: Install Package - Mariadb
  hosts: server_group1 # mention your host group or IP
  become: yes  # Enable privilege escalation to root
  vars:
    mariadb_version: "10.5"
  tasks:
    - name: Download mariadb_repo_setup script
      ansible.builtin.get_url:
        url: https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
        dest: /tmp/mariadb_repo_setup
        mode: '0755'
    - name: Run mariadb_repo_setup script for version 10.5
      ansible.builtin.command: /tmp/mariadb_repo_setup - mariadb-server-version={{ mariadb_version }}
    - name: Installing Mariadb packages -->
      ansible.builtin.yum:
        name: "{{ item }}"
        state: present
      loop:
        - MariaDB-server
        - MariaDB-compat
        - MariaDB-client
        - MariaDB-shared
        - MariaDB-devel
        - MariaDB-common

Execute Ansible playbook:

Once the playbook created ,Execute the playbook using below

ansible-playbook ansible_mariadb.yml -i inventory

Thanks for reading & Follow for More ✌️