English translation
10. Playbook Structure and Syntax in Ansible
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous section, we covered foundational Ansible concepts—particularly variables and facts. This section delves into the structure and syntax of Ansible Playbooks, laying the groundwork for defining and managing tasks in subsequent lessons.
What Is a Playbook?
An Ansible Playbook is a set of instructions describing how to configure and manage remote hosts. Written in YAML format, Playbooks are highly readable and easy to maintain. They are organized into plays, each specifying which hosts to target and what tasks to execute on them.
Basic Structure of a Playbook
A minimal Playbook typically includes the following components:
---
- hosts: webservers
remote_user: ansible
tasks:
- name: Install nginx
yum:
name: nginx
state: present
Key Components
- File header: Begins with
---, indicating a YAML document. hosts: Specifies the host group (or individual host) to which the play applies—referencing groups defined in your inventory file.remote_user: Defines the user account used to connect to target hosts.tasks: Lists the operations to be performed; this is the core of any play.name: A descriptive label for the task, improving readability and maintainability.- Modules: Reusable Ansible units of logic—for example,
yum,apt,copy,template, etc.
Syntax Details
- Playbooks use YAML, where indentation is strictly enforced. Indentation must be done with spaces—not tabs.
- A single Playbook file may contain multiple plays, each independently defined as needed.
Example: Multiple Plays
---
- hosts: webservers
tasks:
- name: Install nginx
yum:
name: nginx
state: present
- hosts: dbservers
tasks:
- name: Install MySQL
yum:
name: mysql
state: present
Here, two distinct plays are defined: one targeting the webservers group and another targeting dbservers.
Variables in Playbooks
Variables enable dynamic, reusable task definitions. They can be declared at various scopes—including playbook-level, host-level, or task-level.
Variable Usage Example
---
- hosts: webservers
vars:
package_name: nginx
tasks:
- name: Install web server
yum:
name: "{{ package_name }}"
state: present
In this example, the variable package_name is defined and then referenced within the task using Jinja2 templating syntax ({{ ... }}). This approach enhances flexibility and reusability.
Conditional Execution
Playbooks support conditional logic via the when clause, allowing tasks to run only when specific criteria are met.
Conditional Execution Example
---
- hosts: webservers
tasks:
- name: Install nginx
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
Here, the Install nginx task executes only if the target host’s operating system belongs to the RedHat family (e.g., RHEL, CentOS, Fedora).
Loops
Ansible supports iterative execution using loops—commonly implemented with with_items.
Loop Execution Example
---
- hosts: webservers
tasks:
- name: Install multiple packages
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- git
- vim
This task installs each listed package (nginx, git, vim) sequentially on the target hosts.
Summary
This section introduced the fundamental structure and syntax of Ansible Playbooks—including how to declare hosts, define tasks, and invoke modules. We also explored variable usage, conditional execution with when, and basic looping with with_items. These core concepts form the essential foundation for the next topic: Defining and Managing Tasks in Playbooks.
If you’ve mastered this material, feel free to proceed to the next lesson: Writing Playbooks — Defining and Managing Tasks.
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after 10. Playbook Structure and Syntax in Ansible?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue