Ansible: A Complete Guide with Examples

Ansible: A Complete Guide with Examples


Ansible is an open-source automation tool designed to simplify IT tasks such as configuration management, application deployment, and orchestration. It uses simple YAML syntax to define tasks and playbooks, making it both easy to learn and efficient to use. Ansible works over SSH, so there's no need for additional software or agents on target machines, making it lightweight and highly scalable.

Key Features of Ansible

  1. Agentless: Ansible doesn't require any agents or daemons to be installed on managed nodes. It communicates directly over SSH or WinRM for Windows machines.
  2. Declarative Language (YAML): Ansible uses YAML, a human-readable data format, to define playbooks. This makes it easy to write and understand.
  3. Idempotency: Ensures that tasks can be run multiple times without changing the system's state if it is already in the desired state.
  4. Extensibility: Ansible is modular and allows users to create custom modules, plugins, and inventories.
  5. Security: Ansible uses OpenSSH for transport, which ensures a high level of security and flexibility.


Ansible Architecture

  • Control Node: The machine where Ansible is installed and run. It sends commands to managed nodes over SSH or WinRM.
  • Managed Nodes: The machines managed by Ansible. These nodes do not require Ansible to be installed.
  • Inventory: A list of managed nodes, grouped by their roles or other properties. You can define static or dynamic inventories.
  • Modules: Reusable scripts that Ansible uses to perform tasks such as installing packages or configuring services.
  • Playbooks: YAML files where tasks are defined. A playbook can consist of one or more "plays," which are a series of tasks executed on a set of managed nodes.


Getting Started with Ansible

To get started with Ansible, you need to install it on the control node. Installation is simple and supported on most Linux distributions as well as macOS.

Installing Ansible on Ubuntu

bash

sudo apt update sudo apt install ansible -y        


Installing Ansible on CentOS

bash

sudo yum install epel-release -y sudo yum install ansible -y        


Verifying Installation

bash

ansible --version        


You should see the version of Ansible and Python dependencies printed in the output.


Ansible Inventory

Ansible uses an inventory file to define which machines to manage. The inventory file is typically located at /etc/ansible/hosts. You can also specify custom inventory files.

Sample Inventory

[webservers]
192.168.1.10
192.168.1.11

[dbservers]
192.168.1.20
        



In the above example, we have two groups: webservers and dbservers, with their respective IP addresses.


Writing Your First Ansible Playbook

Playbooks are a collection of plays, and plays consist of tasks. Let's start with a simple playbook that installs Nginx on a group of web servers.

Example Playbook: Install Nginx

---
- hosts: webservers
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
        


Running the Playbook

ansible-playbook install-nginx.yml
        

This playbook installs Nginx on all machines in the webservers group. The become: yes directive allows Ansible to run commands with elevated privileges (i.e., using sudo).


Example 2: Creating a User on Multiple Servers

This playbook creates a new user called deploy on all webservers with a specific home directory and shell.

---
- hosts: webservers
  become: yes
  tasks:
    - name: Create a new user
      user:
        name: deploy
        shell: /bin/bash
        home: /home/deploy
        state: present
        

Example 3: Copying Files to Multiple Servers

You can use Ansible to copy files or directories to remote nodes. Here's an example of copying an index.html file to web servers.

---
- hosts: webservers
  tasks:
    - name: Copy index.html to webservers
      copy:
        src: /path/to/local/index.html
        dest: /var/www/html/index.html
        



Example 4: Restarting Services

This playbook restarts the Nginx service on all web servers.

---
- hosts: webservers
  become: yes
  tasks:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted
        



Using Roles for Better Organization

As your playbooks grow, you may want to organize them better. Ansible roles provide a way to group related tasks, variables, and files. Roles are stored in the roles/ directory of your project and are referenced in playbooks.

Example Role Directory Structure

roles/
  webserver/
    tasks/
      main.yml
    files/
      index.html
    templates/
      nginx.conf.j2
    vars/
      main.yml
        


You can invoke a role in a playbook like this:

---
- hosts: webservers
  roles:
    - webserver
        

Conclusion

Ansible is a powerful tool for automating IT tasks. Its simplicity, agentless architecture, and wide range of modules make it suitable for small projects as well as large-scale enterprise environments. With playbooks, inventories, and roles, Ansible allows you to automate everything from server setup to application deployment.

With examples like installing packages, creating users, copying files, and restarting services, you can now start automating your infrastructure and applications with Ansible. Happy automating!

要查看或添加评论,请登录

UlugBeck Nurmatov的更多文章

社区洞察

其他会员也浏览了