1. Basics of Playbook
5.1 What is a Playbook?
A Playbook is Ansible’s configuration, deployment, and orchestration language, written in YAML format, containing a collection of one or more plays.
5.2 Basic Syntax Structure
---
- name: Deploy Web Application
hosts: webservers
become: yes
vars:
http_port: 80
app_version: "1.0.0"
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Start Nginx Service
service:
name: nginx
state: started
enabled: yes
2. Core Elements of Playbook
5.3 Explanation of Basic Components
hosts: Target Hosts
- hosts: webservers # Host group
- hosts: db1.example.com # Single host
- hosts: webservers:dbservers # Multiple host groups
tasks: Task List
tasks:
- name: Create Application Directory
file:
path: /opt/myapp
state: directory
owner: appuser
group: appuser
handlers: Triggers
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
tasks:
- name: Update Nginx Configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
3. Variable Management
6.1 Variable Definition Methods
Defined in Playbook
vars:
app_name: "myapp"
app_port: 8080
db_host: "db.example.com"
Using Variable Files
vars_files:
- vars/main.yml
- vars/secrets.yml
Host Inventory Variables
[webservers]
web1.example.com http_port=80
web2.example.com http_port=8080
[webservers:vars]
nginx_worker_processes=4
6.2 System Variables (Facts)
- name: Display System Information
debug:
msg: "Host {{ ansible_hostname }} has {{ ansible_memtotal_mb }}MB of memory"
6.3 Registered Variables
tasks:
- name: Check Service Status
command: systemctl is-active nginx
register: nginx_status
- name: Display Status
debug:
var: nginx_status.stdout
4. Conditional Statements and Loops
7.1 Conditional Statements with when
tasks:
- name: Install EPEL Repository (CentOS)
yum:
name: epel-release
state: present
when: ansible_os_family == "RedHat"
- name: Restart Service (only if configuration changes)
service:
name: nginx
state: restarted
when: nginx_config.changed
7.2 Loops with with_items
tasks:
- name: Install Multiple Packages
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- mysql
- php
- name: Create Multiple Users
user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
with_items:
- { name: 'user1', groups: 'wheel' }
- { name: 'user2', groups: 'docker' }
5. Template System
8.1 Basics of Jinja2 Templates
# nginx.conf.j2
server {
listen {{ http_port }};
server_name {{ server_name }};
location / {
proxy_pass http://{{ backend_host }}:{{ backend_port }};
}
# Conditional Statements
{% if ssl_enabled %}
ssl_certificate {{ ssl_cert_path }};
ssl_certificate_key {{ ssl_key_path }};
{% endif %}
}
8.2 Using Templates
tasks:
- name: Generate Nginx Configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/conf.d/app.conf
owner: root
group: root
mode: 0644
notify: reload nginx
6. Tag Management
9.1 Tag Definition
tasks:
- name: Install Package
yum:
name: nginx
state: present
tags:
- install
- nginx
- name: Configure Firewall
firewalld:
port: "{{ http_port }}/tcp"
state: enabled
permanent: yes
tags: firewall
9.2 Using Tags
# Execute Specific Tags
ansible-playbook site.yml --tags "install,nginx"
# Skip Specific Tags
ansible-playbook site.yml --skip-tags "firewall"
# List All Tags
ansible-playbook site.yml --list-tags
7. Error Handling
10.1 Ignoring Errors
tasks:
- name: Attempt Potentially Failing Operation
command: /usr/bin/might-fail
ignore_errors: yes
register: result
- name: Display Result
debug:
var: result
10.2 Failure Handling
tasks:
- name: Check Dependency Service
uri:
url: "http://{{ db_host }}:3306"
status_code: 200
register: health_check
failed_when: health_check.status != 200
- name: Deploy Application
command: /opt/deploy.sh
when: health_check.status == 200
8. Practical Case Studies
11.1 Complete Web Application Deployment
---
- name: Deploy Web Application Stack
hosts: webservers
become: yes
vars:
app_name: "myapp"
app_port: 8080
db_host: "db.example.com"
tasks:
- name: Install Dependencies
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- python3
- git
tags: install
- name: Create Application User
user:
name: "{{ app_name }}"
system: yes
home: "/opt/{{ app_name }}"
tags: setup
- name: Deploy Application Code
git:
repo: "https://github.com/user/{{ app_name }}.git"
dest: "/opt/{{ app_name }}"
version: "main"
tags: deploy
- name: Configure Nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/conf.d/{{ app_name }}.conf
notify: reload nginx
tags: config
handlers:
- name: reload nginx
service:
name: nginx
state: reloaded