


What is Ansible and what are its main components (playbook, inventory, role)?
Jul 05, 2025 am 12:18 AMAnsible's core components include playbook, inventory, and role. Playbook is a task list written in YAML to define operations performed on a remote host, supporting multiple "plays", conditional judgment, loops and idempotence. The Inventory file is used to define hosts and host groups managed by Ansible. It can specify variables, IP ranges, nested groups, and supports dynamic acquisition of host lists. Role is a reusable unit that organizes tasks, templates, files and variables. It has a standard directory structure to improve code reusability and collaboration efficiency. When using it, specify the target host through inventory, define operation tasks through playbook, and use role modular logic to achieve efficient management.
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It's popular because it's simple to use, agentless (no need to install software on the target machines), and powerful enough to handle complex workflows.
At its core, Ansible helps you manage multiple systems efficiently by defining tasks once and applying them across many machines. Let's break down the main components you'll work with when using Ansible: playbook, inventory, and role.
What is a Playbook in Ansible?
A playbook is like a recipe or script that defines a set of tasks to be executed on remote hosts. It's written in YAML format and is meant to be human-readable.
Playbooks are where you define what you want Ansible to do — whether it's installing packages, restarting services, or deploying applications.
For example, a basic playbook might look like this:
- name: Install Apache hosts: webservers became: yes tasks: - name: Install Apache package apt: name: apache2 state: present
This playbook targets the "webservers" group, uses elevated privileges ( become: yes
), and runs a single task to ensure Apache is installed.
Key points about playbooks:
- They can contain one or more “plays” — each targeting different hosts and doing different things.
- They support conditions, loops, handlers, and more advanced logic.
- They are idealpotent, meaning running the same playbook multiple times won't cause unintended side effects.
What is an Inventory File?
The inventory is where you define the hosts and groups that Ansible will manage. Think of it as a list of servers grouped logically.
By default, Ansible reads from /etc/ansible/hosts
, but you can specify custom inventory files when running commands or playbooks.
Here's a basic example:
[webservers] web1.example.com web2.example.com [databases] db1.example.com
In this case, you have two groups: webservers
and databases
. When writing a playbook, you can specify which group to target under the hosts:
section.
Advanced inventory features include:
- Assigning variables to hosts or groups
- Using IP ranges
- Organizing into nested groups
- Dynamic inventory scripts (eg, pulling host lists from cloud providers)
Inventory helps keep your playbooks generic and reusable across different environments.
What is a Role in Ansible?
A role is a way to organize tasks, templates, files, and variables into reusable units. Roles help structure your playbooks better, especially as they grow in complexity.
Each role typically has a standard directory structure:
roles/ common/ tasks/ handlers/ templates/ files/ vars/ defaults/ meta/
You can create a role for setting up a web server, another for configuring a database, and then reuse them across projects.
To use a role in a playbook:
- name: Configure all servers hosts: all roles: - common - webserver
Benefits of using roles:
- Promote code reuse
- Improve readability and maintenance
- Make collaboration easier
- Encourage best practices through consistent structure
Roles are especially useful when sharing configurations or working in teams.
Putting it all together, Ansible works by combining these components:
- Use an inventory to define which hosts to target.
- Create playbooks to describe what actions to perform.
- Organize and modularize your logic using roles .
That's how most Ansible setups start and scale. It keeps things manageable without adding unnecessary complexity.
Basically that's it.
The above is the detailed content of What is Ansible and what are its main components (playbook, inventory, role)?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The working principle of Ansible can be understood from the above figure: the management end supports three methods of local, ssh, and zeromq to connect to the managed end. The default is to use the ssh-based connection. This part corresponds to the connection module in the above architecture diagram; you can press the application type HostInventory (host list) classification is carried out in other ways. The management node implements corresponding operations through various modules. A single module and batch execution of a single command can be called ad-hoc; the management node can implement a collection of multiple tasks through playbooks. Implement a type of functions, such as installation and deployment of web services, batch backup of database servers, etc. We can simply understand playbooks as, the system passes

After receiving feedback from the project, the customer encountered difficulties when deploying the product using the tools we provided, and encountered problems in the host addition step, which prevented the implementation team from continuing to advance the work, so they asked us for help. Environment information: kylin10 architecture: arm has begun to take shape. During the system deployment process, for batch operations of hosts, we used ansible scripts during development. Recently, I encountered a problem with the execution process being stuck. It was initially suspected that ansible was blocked during execution. To verify this, I have sent a command to the field for testing. localhost$date2024-02-19 Sunday 17:30:41CSTlocalhost$ansibleall-i

Official documentation: https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html Introduction Ad-hoc command is a command that is temporarily entered and executed, usually used for testing and debugging. They do not need to be saved permanently. Simply put, ad-hoc is "instant command". Commonly used modules 1. command module (default module) The default module is not as powerful as the shell. Basically, the shell module can support the functions of the command module. 【1】Help ansible-doccommand# It is recommended to use the following ansible-doccomm

Linux remote management tools you don’t know: five recommended recommendations In the modern information technology field, the Linux operating system has become one of the preferred operating systems for many businesses and individuals. For Linux system administrators, remote management tools are very important, helping them monitor and manage remote servers quickly and efficiently. This article will introduce five Linux remote management tools that you may not know and provide specific code examples. I hope it will be helpful to you in Linux remote management. tmu

As a powerful operating system, Linux's remote management tools are widely used in server management, network monitoring, etc. In our daily work, we often need to use some specialized tools to remotely manage Linux servers. This article will introduce five practical Linux remote management tools and provide specific code examples to demonstrate their usage. 1.SSHSSH (SecureShell) is an encrypted network protocol used to securely log in and execute commands remotely. Via SSH, users can

Ansible is an open source automated configuration management and deployment tool that helps administrators automate tasks on multiple servers. In Ansible, playbooks are YAML files used to describe automation tasks. Using variables is an important part of Playbook functionality, which can make your Playbook more flexible, reusable, and easier to maintain. The following are some basic uses of variables in Ansible Playbook: Define variables: Variables can be defined in the playbook, inventory file, vars file, or on the command line using the -e parameter. Example: Define variables in Playbook: ----na

How to configure automated deployment tools (such as Ansible) on Linux Introduction: In the process of software development and operation and maintenance, we often encounter situations where applications need to be deployed to multiple servers. Manual deployment is undoubtedly inefficient and error-prone, so configuring an automated deployment tool is essential. This article will introduce how to configure Ansible, a commonly used automated deployment tool, on Linux to achieve fast and reliable application deployment. 1. Install Ansible. Open the terminal and use the following command.

Java docks with the ansible automatic operation and maintenance platform to realize file collection and distribution. This docking mainly has the following two functions: file collection (batch operations on files, including batch collection of common files such as log files from multiple hosts) file distribution (batch operations on files) Perform batch operations, including batch distribution of common files (such as log files) from multiple hosts) Scenario description and ansibleyum installation. Since ansible does not have a Windows installation package, in order to facilitate testing, a Linux environment was built for subsequent work. This time, the yum method is used to install. After using the yum method to install Ansible, first install the EPEL source. yuminstall-yhttp://dl.fedor
