Introduction
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is known for its simplicity and ease of use, leveraging SSH for communication and YAML for configuration files.
Key Concepts
- Playbooks: YAML files that define a series of tasks to be executed on remote hosts.
- Inventory: A list of hosts or groups of hosts where Ansible will run tasks.
- Modules: Reusable, standalone scripts that Ansible runs on your behalf.
- Roles: A way to group multiple tasks, variables, files, templates, and handlers into a single unit.
Setting Up Ansible
Installation
To install Ansible on a Linux system, you can use the package manager. For example, on Ubuntu:
Verifying Installation
Check the installed version to verify the installation:
Basic Ansible Configuration
Inventory File
The inventory file lists the hosts and groups of hosts that Ansible will manage. Create a file named hosts
:
Ansible Configuration File
Ansible's behavior can be customized using the ansible.cfg
file. Create a basic configuration file:
Writing Your First Playbook
Playbook Structure
A playbook is a YAML file that defines one or more plays. Each play specifies a set of tasks to be executed on a group of hosts.
--- - name: Ensure Apache is installed hosts: webservers become: yes tasks: - name: Install Apache apt: name: apache2 state: present - name: Start Apache service service: name: apache2 state: started
Running the Playbook
To run the playbook, use the ansible-playbook
command:
Advanced Playbook Features
Variables
Variables can be defined in the playbook or in separate files. They help in making the playbook more dynamic.
--- - name: Install and configure Apache hosts: webservers become: yes vars: apache_package: apache2 tasks: - name: Install Apache apt: name: "{{ apache_package }}" state: present
Handlers
Handlers are tasks that are triggered by other tasks. They are useful for actions like restarting a service after a configuration change.
--- - name: Install and configure Apache hosts: webservers become: yes tasks: - name: Install Apache apt: name: apache2 state: present notify: - Restart Apache handlers: - name: Restart Apache service: name: apache2 state: restarted
Templates
Templates allow you to create dynamic configuration files using Jinja2 templating language.
Create a template file index.html.j2
:
<html> <head> <title>Welcome to {{ ansible_hostname }}</title> </head> <body> <h1>Hello from {{ ansible_hostname }}</h1> </body> </html>
Use the template in your playbook:
--- - name: Deploy a custom index.html hosts: webservers become: yes tasks: - name: Copy index.html from template template: src: index.html.j2 dest: /var/www/html/index.html
Practical Exercise
Exercise: Automate Apache Installation and Configuration
- Objective: Write an Ansible playbook to install Apache, deploy a custom
index.html
, and ensure the service is running. - Steps:
- Create an inventory file with at least one web server.
- Write a playbook that:
- Installs Apache.
- Deploys a custom
index.html
using a template. - Ensures the Apache service is started.
- Run the playbook and verify the results.
Solution
Inventory File (hosts
):
Playbook (playbook.yml
):
--- - name: Install and configure Apache hosts: webservers become: yes tasks: - name: Install Apache apt: name: apache2 state: present notify: - Restart Apache - name: Deploy custom index.html template: src: index.html.j2 dest: /var/www/html/index.html handlers: - name: Restart Apache service: name: apache2 state: restarted
Template (index.html.j2
):
<html> <head> <title>Welcome to {{ ansible_hostname }}</title> </head> <body> <h1>Hello from {{ ansible_hostname }}</h1> </body> </html>
Run the Playbook:
Conclusion
In this section, you learned the basics of Ansible, including how to set it up, write playbooks, and use advanced features like variables, handlers, and templates. Ansible is a powerful tool that can significantly simplify the management and automation of your infrastructure. In the next module, we will delve into more advanced topics such as Linux Kernel Tuning.
Linux Mastery: From Beginner to Advanced
Module 1: Introduction to Linux
Module 2: Basic Linux Commands
- Introduction to the Command Line
- Navigating the File System
- File and Directory Operations
- Viewing and Editing Files
- File Permissions and Ownership
Module 3: Advanced Command Line Skills
- Using Wildcards and Regular Expressions
- Piping and Redirection
- Process Management
- Scheduling Tasks with Cron
- Networking Commands
Module 4: Shell Scripting
- Introduction to Shell Scripting
- Variables and Data Types
- Control Structures
- Functions and Libraries
- Debugging and Error Handling
Module 5: System Administration
- User and Group Management
- Disk Management
- Package Management
- System Monitoring and Performance Tuning
- Backup and Restore
Module 6: Networking and Security
- Network Configuration
- Firewall and Security
- SSH and Remote Access
- Intrusion Detection Systems
- Securing Linux Systems
Module 7: Advanced Topics
- Virtualization with Linux
- Linux Containers and Docker
- Automating with Ansible
- Linux Kernel Tuning
- High Availability and Load Balancing