Ansible Basics
Introduction
Ansible is an open-source automation tool that helps in configuration management, application deployment, task automation, and multi-node orchestration. It uses a simple language called YAML (Yet Another Markup Language) to describe automation tasks.
Key Concepts
- **Agentless**: Ansible does not require any agent to be installed on the target machines.
- **Idempotency**: Ansible ensures that the operations are idempotent, meaning running the same playbook multiple times won't change the system state if it's already in the desired state.
- **Modules**: Ansible comes with a wide range of built-in modules that can interact with the underlying operating system and other services.
- **Playbooks**: The files where Ansible code is written, defining the desired state of the system.
Installation
Ansible can be installed on various platforms. Below are the instructions for a few common environments:
On Ubuntu
sudo apt update
sudo apt install ansible
On CentOS
sudo yum install epel-release
sudo yum install ansible
On macOS
brew install ansible
Inventory Management
Inventory is a file where you define the hosts and groups of hosts that Ansible will manage. The default inventory file is located at /etc/ansible/hosts.
Example Inventory File
[webservers]
192.168.1.10
192.168.1.11
[databases]
192.168.1.20
Playbooks
Playbooks are the heart of Ansible's configuration management capabilities. They are written in YAML format.
Basic Playbook Example
- name: Install Apache
hosts: webservers
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: Start httpd service
service:
name: httpd
state: started
Roles
Roles allow you to group related tasks and variables together. They enable better organization and reuse of your playbooks.
Creating a Simple Role
ansible-galaxy init myrole
This will create a directory structure for your role, which you can populate with tasks, handlers, and other files.
Best Practices
- Use version control for your playbooks.
- Organize playbooks and roles logically.
- Utilize Ansible Vault for sensitive data.
- Test playbooks in a staging environment before production.
FAQ
What is Ansible used for?
Ansible is primarily used for configuration management, application deployment, and orchestration of tasks across multiple servers.
Is Ansible agentless?
Yes, Ansible does not require any agent to be installed on the target machines. It uses SSH for communication.
Can Ansible manage Windows servers?
Yes, Ansible can manage Windows servers using WinRM or PowerShell remoting.