translations: [ es/Español ] · [ fr/Français ] · [ de/Deutsch ]
Table of contents
Ansible simplifies server management and configuration by automating tasks across multiple servers. To begin using Ansible, set up essentials on the server and client sides.
Setting up the Server
Installation
For the server, ensure Python is installed, as Ansible operates using Python. To install Ansible:
# Ubuntu
sudo apt install ansible
# Fedora
sudo dnf install ansible
# OpenSUSE
sudo zypper install ansible
Configuration
- Create an Ansible inventory file (
hosts
) to define the servers you want to manage. Example:
[webserver]
192.168.1.101
- Verify server connection using:
ansible all -i hosts -m ping
Setting up the Client
Installation
The client machine only needs SSH access and Python (usually pre-installed on most Linux systems).
Executing Commands
Run arbitrary commands on the client using the ansible
command with the -a
flag and the desired command. For instance:
ansible <client_name> -a "ls -la"
Replace <client_name>
with the host defined in your inventory file.
Playbooks
Ansible uses playbooks (YAML files) to define configurations. They enable you to manage multiple tasks and servers in a single file. Example of a simple playbook (example.yml
):
---
- name: Execute commands on the client
hosts: webserver
tasks:
- name: Execute arbitrary command
command: ls -la
Run this playbook using:
ansible-playbook example.yml
Roles
Roles in Ansible help organize and structure playbooks. They encapsulate functionalities, making playbook management more manageable. Example directory structure for a role:
my_role/
├── tasks/
│ └── main.yml
├── handlers/
├── templates/
├── vars/
└── meta/
Created on: Jan 7, 2024