Automating Server Configuration with Ansible

Introduction

Ansible is an open-source IT automation tool written in Python. It's widely used for configuration management, making complex tasks simpler by automating system setup, software deployment, and more.

Ansible is agentless, meaning it doesn't require an agent to communicate with other machines. It supports a wide range of operating systems, platforms, and devices, from Ubuntu and VMware to CentOS, Windows, Azure, AWS, and network devices like Cisco and Juniper. This design increases Ansible's usability because you don't need to install and maintain agents on hosts. This is a significant advantage over similar tools like Chef, SaltStack, and Puppet.

Why Choose Ansible?

Here are some reasons to choose Ansible over other configuration management tools:

  • Open Source: It's free to use.
  • Uses SSH: Easily connects directly to servers.
  • Lightweight: Easy to set up and doesn't consume many resources.
  • Readable Syntax: Uses YAML for scripts, making them easy to read and understand.
  • Large Community: A big user community makes it easy to get help and learn.

Applications of Ansible

Ansible is mainly used for software deployment and system administration, including:

  • Provisioning: Creating multiple VMs and containers on the cloud using APIs like OpenStack, AWS, Google Cloud, and Azure.
  • Configuration Management: Centrally managing service configurations without manually editing each server.
  • Application Deployment: Deploying applications in bulk, efficiently managing their lifecycle from development to production.

Basic Concepts in Ansible

  • Controller Machine: The machine where Ansible is installed. It manages, controls, and sends tasks to the machines that need to be managed.
  • Inventory: A file containing information about the servers to be managed.
  • Playbook: A YAML file with configuration tasks. The controller machine uses this playbook to execute the corresponding tasks on the managed machines.
  • Tasks: Smaller tasks that need to be performed on the server.
  • Module: Ansible supports various modules for different tasks, such as System, Commands, Files, Database, Cloud, Windows, etc.
  • Variables: Defined variables used throughout the playbook.

Installing Ansible

To install Ansible, visit this page and follow the instructions based on your operating system

If you're using Ubuntu, execute the following commands:

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible


Practicing with Ansible

To proceed with the next steps, you'll need a server that Ansible can SSH into to perform automation tasks. This server can be a local VM instance or a Cloud VM that is set up to allow SSH connections. If you need help creating a Google Cloud VM instance and configuring it for SSH access, check out this article.


Create a file named `inventory.yml` with the following content:

all:
hosts:
34.142.244.104:
# 172.16.61.21:

This file is used to define the hosts you need to connect to.


After that, you can use the following command to SSH into the server and execute commands directly:

ansible all -i {inventory file path}.yml -m "command" -u {username}

# ex
ansible all -i inventory.yml -m ping -u user1


The results will be as follows:


Next, when working with the playbook, create a file named `playbook.yml` with the following content:

- name: Simple Example
hosts: all
remote_user: username
become: true
tasks:
- name: testing
shell: /usr/bin/whoami
register: testing

- name: show the result
debug:
msg: "{{ testing.stdout }}"

Explanation:

  • name: The name of the playbook
  • hosts: Uses the host label defined in `inventory.yml`
  • remote_user: The username of the account created on the server
  • become: true means commands will execute with sudo privileges
  • tasks: Includes tasks that will be executed on the server


To execute the playbook, use the following command:

ansible-playbook -i {inventory file path}.yml {playbook file path}.yml

# ex
ansible-playbook -i inventory.yml playbook.yml



Don't hesitate to leave your thoughts in the comments section, and remember to like, share, and follow for more insightful content in the future!

Comments

Popular posts from this blog

Kubernetes Practice Series

NodeJS Practice Series

Docker Practice Series

React Practice Series

Sitemap

Setting up Kubernetes Dashboard with Kind

Deploying a NodeJS Server on Google Kubernetes Engine

DevOps Practice Series

Create API Gateway with fast-gateway

A Handy Guide to Using Dynamic Import in JavaScript