What is Ansible?
Introduction
Ansible is an open-source automation tool used for IT tasks
such as configuration management, application deployment, and orchestration. It
was created by Michael DeHaan in 2012, and since then it has become a popular
choice among DevOps engineers and system administrators.
In this article, we will cover the basics of Ansible and
provide some examples to help you get started.
What is Ansible?
Ansible is a configuration management tool that allows users
to automate repetitive tasks. It uses a simple, easy-to-learn language called
YAML (yet another markup language or recursively YAML Ain't Markup Language) to
describe configurations and tasks. Ansible doesn't require any agents or
daemons on the managed nodes, which makes it lightweight and easy to use.
Ansible Architecture
The Ansible architecture consists of the following
components:
- Control
Node: This is the machine from which Ansible is run. It contains the
Ansible installation and the inventory of managed nodes.
- Managed
Nodes: These are the machines that Ansible manages. Ansible can manage a
wide range of machines, including physical servers, virtual machines, and
cloud instances.
- Inventory:
This is a list of managed nodes that Ansible can control. It can be a simple
text file or a dynamic inventory that pulls information from a cloud
provider.
- Playbooks:
Playbooks are Ansible's configuration files that describe the desired
state of the managed nodes. They contain a set of tasks that Ansible
executes on the managed nodes.
- Modules:
Modules are pre-built scripts that Ansible uses to perform specific tasks
on the managed nodes. Ansible comes with a large library of modules that
cover a wide range of tasks.
Examples
Now, let's look at some examples of how to use Ansible.
- Installing
Packages
One of the most common tasks in system administration is
installing software packages. With Ansible, you can install packages on
multiple nodes simultaneously.
To install a package, you need to create a playbook that specifies the package name and the nodes on which it should be installed. Here's an example playbook that installs the Apache web server on two nodes:
---
- name: Install Apache on CentOS
hosts: web
become: true
tasks:
- name: Install Apache
yum:
name: httpd
state: present
In this playbook, we specify the name of the package to
install (httpd) and the state (present) to ensure that the package is
installed.
- Managing
Services
Once you've installed a package, you need to start and
manage the associated service. Ansible can help you do that too.
Here's an example playbook that starts the Apache web server
and ensures that it starts automatically on boot:
---
- name: Start Apache on CentOS
hosts: web
become: true
tasks:
- name: Start Apache
service:
name: httpd
state: started
enabled: true
In this playbook, we use the service module to start the
Apache service and set it to start automatically on boot (enabled: true).
- Copying
Files
Copying files between machines is another common task in
system administration. Ansible makes it easy to copy files between nodes.
Here's an example playbook that copies a file from the
control node to a managed node:
---
- name: Copy file to CentOS
hosts: web
become: true
tasks:
- name: Copy file
copy:
src: /path/to/local/file
dest: /path/to/remote/file
In this playbook, we use the copy module to copy a file from
the control node to a managed node. We specify the source file (/path/to/local/file)
and the destination file (/path/to/remote/file).
- Managing
Users
Managing user accounts is another task that Ansible can
handle. You can create and delete user accounts, set passwords, and manage SSH
keys.
Here's an example playbook that creates a new user account
and adds an SSH key:
---
- name: Create user on CentOS
hosts: web
become: true
tasks:
- name: Create user
user:
name: john
password: "{{ 'password' | password_hash('sha512') }}"
- name: Add SSH key
authorized_key:
user: john
key: "{{ lookup('file', '/path/to/public/key') }}"
In this playbook, we use the user module to create a new
user account (name: john) and set the password using the password_hash filter.
We then use the authorized_key module to add an SSH key to the user's authorized_keys
file.
5. Sample
inventory, yaml and ansible playbook command for installing Apache on 2 servers
First, we need to create an inventory file that lists the
servers we want to manage:
[web-servers]
server1
server2
Here we created a group called "web-servers" and
added server1 and server2 to it.
2. Next,
we create a playbook file that will describe the tasks we want to perform on
the servers. Let's call it apache.yml:
---
- name: Install Apache
hosts: web-servers
become: true
tasks:
- name: Install Apache
yum:
name: httpd
state: latest
- name: Start Apache
service:
name: httpd
state: started
This playbook will:
- Install the latest version of Apache using the yum package manager.
- Start the Apache service using the service module.
3. Finally,
we can run the playbook using the ansible-playbook command:
ansible-playbook
apache.yml
This will connect to each server in the web-servers group, and execute the tasks specified in the playbook.
PLAY [Install Apache]
*****************************************************************
TASK [Gathering Facts]
****************************************************************
ok: [server1]
ok: [server2]
TASK [Install Apache] ******************************************************************
changed: [server1]
changed: [server2]
TASK [Start Apache]
********************************************************************
changed: [server1]
changed: [server2]
PLAY RECAP
****************************************************************************
server1
: ok=3 changed=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
server2
: ok=3 changed=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
conclusion
Ansible is a powerful automation tool that can help you
streamline your IT tasks. It is easy to learn and use, and it doesn't require
any agents or daemons on the managed nodes. With Ansible, you can automate
tasks like installing software packages, managing services, copying files, and
managing user accounts. These examples are just the tip of the iceberg –
Ansible can handle a wide range of tasks and scenarios. If you're looking to
streamline your IT operations, Ansible is definitely worth considering.
Comments
Post a Comment