Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

Introduction in one sentence: Playbooks are the core of Ansible, describing tasks using YAML, making automation repeatable and maintainable.

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

1. What is a Playbook?

A Playbook is like a “script” for Ansible, defining a series of tasks to be executed in a YAML file and specifying on which hosts to execute them. Its characteristics are:

·Repeatable: Write once, run multiple times

·High readability: Clear structure, explicit task descriptions

·Maintainable: Facilitates version control and team collaboration

Applicable scenarios:

·Application deployment

·System environment configuration

·Multi-step task orchestration

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

2. Basic Syntax of YAML

Ansible’s Playbooks are written using YAML (Yet Another Markup Language), with common rules as follows:

1.Indentation: Use spaces for indentation, do not use Tab (recommended 2 spaces)

2.Lists: Use to indicate

packages: nginx mysql

3.Dictionaries (key-value pairs):

name: nginxstate: present

4.Comments: Use # to indicate

# This is a comment

5.Variable references: Use {{ variable_name }}

msg:“Hello {{ username }}”

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

3. Basic Structure of a Playbook

A minimal Playbook:

hosts: webservers become:yes tasks: name: Install Nginx apt: name: nginx state: present

Structure analysis:

· : File header identifier

·– hosts : Specifies the host group to execute

·become: yes : Privilege escalation (equivalent to sudo)

·tasks : Task list

·name : Task description

·apt : Calls the apt module

·state: present : Install software

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

4. Executing a Playbook

Save as install_nginx.yml, execute:

ansible-playbook install_nginx.yml

Common options:

Parameter

Function

–check

Dry run, does not actually execute

–limit

Limit execution to a specific host

-v/-vv/-vvv

Show more execution details

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

5. Conditional Statements and Loops

1. Conditional Statements (when)

hosts: webservers tasks: name: Install Nginx (only on Debian-based systems) apt: name: nginx state: present when: ansible_os_family == “Debian”

2. Loops (loop)

hosts: webservers tasks: name: Batch install software apt: name:“{{ item }}” state: present loop: nginx mysql-server php

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

6. Using Variables

1. Defining Variables within a Playbook

hosts: webservers vars: http_port:80 tasks: name: Output variable debug: msg:“Web service port is {{ http_port }}”

2. Passing Variables from Outside

ansible-playbook site.yml -e“http_port=8080”

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

7. Practical Case: Installing and Starting Nginx

hosts: webserversbecome:yestasks: name: Install Nginx apt: name: nginx state: present when: ansible_os_family == “Debian” name: Start Nginx service: name: nginx state: started enabled:yes

Execute:

ansible-playbook nginx_setup.yml

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

8. Playbook Debugging Techniques

1.Execute only part of the tasks

ansible-playbook play.yml –start-at-task=“Start Nginx”

2.Add tags

name: Install Nginx apt: name: nginx state: present tags: install

ansible-playbook play.yml –tags“install”

3.Dry-run mode

ansible-playbook play.yml –check

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

9. Common Errors and Troubleshooting

1.Indentation errors

oUse YAML validation tools (like yamllint) to check

2.Undefined variables

oEnsure variables are defined before use

3.Execution failures

oAdd -vvv to see detailed logs

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

10. Summary and Next Preview

In this article, we learned about:

·The concept of Playbooks and the basics of YAML

·The basic structure of Playbooks and execution methods

·Conditional statements, loops, and variable usage

·A simple Nginx deployment case

The next article “Variables, Templates, and File Management” will delve into how to use Jinja2 templates to generate configuration files and achieve multi-environment automated deployment with variables.

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

💡 Suggestion: When starting out, write several simple Playbooks, such as batch installing common tools or creating users, to familiarize yourself with YAML and module invocation before optimizing the structure.

Ansible Series Tutorial (3): Introduction to Playbooks and YAML Basics

Leave a Comment