Configuration Management (ANSIBLE)


Configuration Management refers to the process of using Ansible to manage the configuration of a large number of servers or systems. Ansible is an open-source automation tool that enables you to define and apply configurations to your systems in a consistent and repeatable way.
Ansible Configuration Management typically involves the following steps:
Defining the desired state of the systems using Ansible playbooks. Playbooks are text files written in YAML that define the desired configuration of a system. They can include tasks, handlers, and roles, which are reusable units of configuration.
Running Ansible playbooks against a set of target systems. Ansible uses a push model to apply configurations to the target systems. You can use Ansible to manage systems that are located on-premises or in the cloud.
Verifying the configuration of the target systems. After applying a configuration, you can use Ansible to verify that the systems are in the desired state. Ansible provides a number of modules that you can use to check the configuration of files, services, and other components of the system.
Managing changes to the configuration. As the system evolves, you may need to make changes to the configuration. You can use Ansible to manage changes by modifying the playbooks and running them against the target systems. Ansible provides features such as version control, rollbacks, and diff reporting to help you manage changes in a controlled manner.
What is Ansible
Ansible is an open-source automation tool that enables you to automate the deployment, configuration, and management of infrastructure and applications. Ansible is designed to be simple, powerful, and agentless, which means that it does not require any software installation on the target systems.
Ansible uses a declarative language to define the desired state of a system, and it uses a push model to apply configurations to the target systems. This means that Ansible runs on a central controller node, and it pushes configuration changes to the target systems over SSH or other network protocols.
Ansible provides a number of features that make it a popular choice for automation and Configuration Management:
Simple and easy to learn: Ansible has a simple syntax that is based on YAML, which makes it easy to learn and use.
Agentless architecture: Ansible does not require any software installation on the target systems, which makes it easy to deploy and use.
Modular and reusable: Ansible provides a large number of modules that you can use to perform various tasks, such as managing files, services, packages, and users. These modules are reusable, which makes it easy to create and maintain Ansible playbooks.
Flexible and extensible: Ansible is highly flexible and extensible. You can create custom modules, roles, and plugins to extend Ansible's functionality.
Integrated with cloud and DevOps tools: Ansible integrates with popular cloud and DevOps tools, such as AWS, Azure, Google Cloud, Git, and Jenkins.
Ansible is used in a wide variety of use cases, such as:
Provisioning and configuring infrastructure and applications.
Deploying and managing software and applications.
Managing configuration across large numbers of systems.
Automating repetitive tasks and workflows.
Orchestrating complex deployments and upgrades.
Installing Ansible
Installing Ansible can vary depending on your operating system and package manager. Here are general steps for installing Ansible on an Ubuntu or Debian-based system using the apt
package manager:
Update the package list:
sudo apt update
Install Ansible and any required dependencies:
sudo apt install ansible
This will install Ansible, as well as any additional dependencies such as the
sshpass
package.Verify the installation:
ansible --version
This will display the version of Ansible that was installed.
Here are general steps for installing Ansible on a Red Hat, Fedora, or CentOS-based system using the yum
or dnf
package manager:
Install the EPEL repository:
sudo yum install epel-release
or
sudo dnf install epel-release
Install Ansible:
sudo yum install ansible
or
sudo dnf install ansible
Verify the installation:
ansible --version
This will display the version of Ansible that was installed.
On macOS, you can install Ansible using Homebrew:
Install Homebrew if you haven't already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Ansible using Homebrew:
brew install ansible
Verify the installation:
ansible --versionTesting with First Ansible Command
Testing with First Ansible Commands
After installing Ansible, you can test it with some basic commands. Here are some commands you can try:
Check the version of Ansible:
ansible --version
This will display the version of Ansible and other information about the installation.
Test the connection to a target host:
ansible <hostname> -m ping
Replace
<hostname>
with the hostname or IP address of a target host that you want to test the connection to. Theping
module is a basic module that checks connectivity to the target host using SSH.Run a basic playbook:
Create a file called
hello.yml
with the following contents:--- - hosts: localhost tasks: - name: Print a message debug: msg: Hello, world!
Then run the playbook with the following command:
ansible-playbook hello.yml
This will run the playbook against the localhost and print a message saying "Hello, world!".
List all hosts in the inventory:
ansible all -m ping
This will ping all hosts in the inventory. If you don't have an inventory file yet, Ansible will use the default inventory file
/etc/ansible/hosts
.
Introduction to Play Books
Playbooks are the core of Ansible's automation capabilities. They are written in YAML and define a list of tasks to be executed on a set of target hosts. Playbooks can be used to configure systems, deploy software, manage services, and perform other system administration tasks.
Here is a basic example of a playbook:
---
- hosts: webservers
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
- name: Ensure Apache is running
service:
name: apache2
state: started
- name: Copy the index.html file
copy:
src: /path/to/index.html
dest: /var/www/html/index.html
This playbook defines a set of three tasks that will be executed on the hosts specified in the hosts
field, which in this case is webservers
.
The first task uses the apt
module to install Apache2 on the target hosts. The name
parameter specifies the package to install, and the state
parameter specifies the desired state of the package.
The second task uses the service
module to start the Apache2 service on the target hosts. The name
parameter specifies the name of the service, and the state
parameter specifies the desired state of the service.
The third task uses the copy
module to copy a file called index.html
from the local machine to the web server's document root. The src
parameter specifies the source file, and the dest
parameter specifies the destination file.
Playbooks can be executed using the ansible-playbook
command:
ansible-playbook myplaybook.yml
YML File
YAML (YAML Ain't Markup Language) is a human-readable data serialization language. It is often used as a configuration file format for infrastructure automation tools like Ansible, Terraform, and Kubernetes.
---
- hosts: webservers
vars:
http_port: 80
https_port: 443
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
- name: Ensure Apache is running
service:
name: apache2
state: started
- name: Copy the index.html file
copy:
src: /path/to/index.html
dest: /var/www/html/index.html
- name: Open firewall ports
ufw:
rule: allow
name: "{{ item }}"
state: enabled
with_items:
- "{{ http_port }}"
- "{{ https_port }}"
Writing playbooks in Ansible! Ansible is a powerful automation tool for managing and configuring servers, networks, and applications. Playbooks are a key feature of Ansible, allowing you to define a series of tasks to be executed in a specific order.
A playbook is a YAML file that contains a set of playbooks that define the actions to be taken on a set of hosts. Playbooks are composed of several elements, including:
Hosts: A list of hosts or groups of hosts that the playbook will target.
Tasks: A set of tasks that will be executed on the hosts. Tasks are typically a single command or a series of commands that need to be run.
Handlers: A set of handlers that will be executed after a specific task has completed.
Why Use Ansible Playbooks?
Ansible playbooks offer many benefits, including:
Improved Repeatability: Playbooks allow you to define a set of actions to be taken repeatedly, making it easier to ensure consistency across multiple hosts.
Increased Efficiency: Playbooks enable you to automate complex tasks and reduce the time spent on manual configuration and deployment.
Enhanced Security: Playbooks can improve security by reducing the risk of human error and ensuring that configurations are consistent and secure.
How to Write an Ansible Playbook
To write an Ansible playbook, follow these steps:
Create a new file: Create a new file with a
.yml
extension, such asmy_playbook.yml
.Define the hosts: In the top-level of the file, define the hosts or groups of hosts that the playbook will target, using the
hosts
keyword.Define the tasks: Inside the
hosts
block, define the tasks that will be executed on the hosts. Each task is represented as a block of YAML code, with aname
key specifying the task name and atask
key specifying the actual code to be executed.Use handlers: If a task requires a handler to be executed after completion, define the handler inside the
tasks
block, using thenotify
keyword.
Here's an example playbook:
---
- name: My Playbook
hosts: all
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
enabled: yes
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
notify: restart
Best Practices for Writing Ansible Playbooks
When writing Ansible playbooks, keep the following best practices in mind:
Use descriptive task names: Use descriptive task names to help identify the actions being taken.
Use YAML syntax: Use YAML syntax to define the playbook, and avoid using Python syntax or other languages.
Test and validate: Test and validate your playbook using the
ansible-playbook
command to ensure it runs correctly and produces the desired output.Document your playbook: Document your playbook using comments and descriptive text to help others understand its purpose and behavior.
What is Ansible Tower?
Ansible Tower is a commercial tool that provides a central management console for Ansible playbooks. It allows you to:
Manage Playbooks: Store, organize, and manage your Ansible playbooks in a centralized repository.
Centrally Configure: Define and manage your Ansible configuration, including inventory, variables, and limits.
Automate Tasks: Automate repetitive tasks and workflows using Ansible's built-in automation features.
Manage Users and Roles: Control access to Ansible playbooks and Configure user roles and permissions.
Monitor and Report: Monitor playbook execution and report on results, including errors and successes.
Scale: Support large-scale implementations of Ansible by providing advanced features like load balancing and high availability.
Ansible Tower Components
Ansible Tower consists of several key components:
Web Interface: A web-based interface for managing and monitoring Ansible playbooks.
API: A RESTful API for integrating Ansible Tower with other systems and tools.
Server: The Ansible Tower server, which runs the Ansible Engine and manages playbook execution.
Workers: Worker nodes that run Ansible playbooks and report back to the Ansible Tower server.
Ansible Tower Features
Ansible Tower provides many features that enhance the Ansible experience, including:
Role-Based Access Control (RBAC): Control access to Ansible playbooks and features using role-based access control.
Job Management: Schedule and manage Ansible job executions, including task queues and retries.
Inventory Management: Store and manage Ansible inventory files in the Ansible Tower database.
Variable Management: Define and manage Ansible variables, including encrypted variables.
Notifications: Send custom notifications to users or teams based on playbook execution results.
Compliance Management: Manage compliance checks and scans, including vulnerability assessments and patch management.
Ansible Tower Benefits
Ansible Tower provides several benefits, including:
Scalability: Support large-scale implementations of Ansible by providing advanced features like load balancing and high availability.
Security: Control access to Ansible playbooks and features using role-based access control.
Flexibility: Automate repetitive tasks and workflows using Ansible's built-in automation features.
Monitoring: Monitor playbook execution and report on results, including errors and successes.
Integration: Integrate Ansible Tower with other systems and tools using the RESTful API.
Ansible Tower Pricing
Ansible Tower pricing varies depending on the license type and number of nodes. Here are the general pricing tiers:
Enterprise: Up to 10 nodes, $1,500 per year.
Enterprise Plus: Up to 50 nodes, $3,000 per year.
Cloud: Up to 1,000 nodes, $5,000 per year.
###
Subscribe to my newsletter
Read articles from Bittu Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Bittu Sharma
Bittu Sharma
Hi, This is Bittu Sharma a DevOps & MLOps Engineer, passionate about emerging technologies. I am excited to apply my knowledge and skills to help the organization deliver the best quality software products. β’ π¦πΌπ³π π¦πΈπΆπΉπΉπ ππ²π'π ππΌπ»π»π²π°π I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.