Module 1: Ansible Fundamentals - Understanding the Basics
What is Ansible?
Agentless Architecture:
YAML Syntax:
Introduction to YAML (YAML Ain't Markup Language) as the human-friendly data serialization standard used by Ansible for defining playbooks, variables, and inventory.
Explanation of basic YAML syntax: key-value pairs, lists, indentation (spaces are crucial!), comments.
Example YAML:
---
- name: Sample Playbook
hosts: webservers
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
Inventory Files:
Explanation of the Ansible inventory file, which lists the managed hosts.
Basic inventory file structure: specifying hostnames or IP addresses.
Example Inventory (/etc/ansible/hosts or a custom file):
[webservers]
webserver01.example.com
webserver02.example.com
[dbservers]
dbserver01.example.com ansible_host=192.168.1.10
[all:vars]
ansible_user=ubuntu
ansible_private_key_file=~/.ssh/id_rsa
Explanation of host-specific variables (e.g., ansible_host for specifying a different connection address, ansible_user for the SSH user).
Hosts Configuration:
all group and custom-defined groups.Command-Line Usage:
ansible command for running ad-hoc tasks and the ansible-playbook command for executing playbooks.ansible command: i (inventory file), m (module), a (arguments), u (remote user), k (prompt for SSH password).Hands-on Lab:
Module 2: Running Ad-Hoc Commands - Quick Automation Tasks
Targeting Specific Hosts:
i flag to specify your inventory file.webservers, dbservers, web*, !webserver02.example.com).Module Usage:
m flag to specify the module to use.a flag (key=value pairs).Common Ad-Hoc Modules:
ping: Verifies connectivity to the target hosts.
ansible all -i inventory.ini -m ping
copy: Copies files from the control node to the target hosts.
ansible webservers -i inventory.ini -m copy -a "src=/path/to/local/file dest=/path/on/remote/host"
apt (for Debian/Ubuntu) / yum (for CentOS/RHEL): Manages packages on the target hosts.
ansible webservers -i inventory.ini -m apt -a "name=nginx state=present"
ansible dbservers -i inventory.ini -m yum -a "name=mariadb-server state=installed"
user: Manages user accounts on the target hosts.
ansible all -i inventory.ini -m user -a "name=deploy useradd=yes state=present"
command: Executes arbitrary commands on the target hosts. Use with caution, as it doesn't guarantee idempotence.
ansible webservers -i inventory.ini -m command -a "ls -l /var/www/html"
file: Manages files and directories (creation, deletion, permissions).
ansible all -i inventory.ini -m file -a "path=/tmp/my_directory state=directory owner=deploy group=deploy mode=0755"
Command-Line Usage: Emphasize the flexibility of ad-hoc commands for quick tasks and troubleshooting.
Hands-on Lab:
htop) on your target hosts using the appropriate package manager module.Module 3: Writing Playbooks - Orchestrating Automation
name (description), module: arguments.notify keyword). Useful for triggering service restarts after configuration changes.when keyword to execute tasks only if a specific condition is met (based on facts or variables).loop keyword to perform the same task multiple times with different parameters.