Running Ansible playbook requires first ensuring that the structure is correct and the environment is prepared. 1. Write a playbook file, including hosts, tasks, etc.; 2. Ensure that the target host is in the inventory and can be connected through SSH, and can be tested by the ansible ping module; 3. Use the ansible-playbook command to run, and add -i to specify the inventory path; 4. You can use -v, --check, --limit, --tags and other parameters to debug or control execution; 5. Pay attention to common error points such as YAML indent, module parameters, permissions, and inventory content. Using --check and -v will help troubleshoot.
Running an Ansible playbook is actually not difficult, the key is to understand the basic structure and commands. Playbook is the core way for Ansible to implement automated tasks. It is written in YAML format and describes the state you want the target host to reach. As long as you have basic Linux operation experience, you can get started quickly.
Get your Playbook files ready
Before running, first make sure you have written the playbook or have a available .yml
file. Playbooks usually include hosts (target host), tasks (task list), and possible vars (variables), handlers (triggers), and other parts.
To give a simple example:
--- - name: Install and start Nginx hosts: webservers became: yes tasks: - name: Install Nginx apt: name: nginx state: present - name: Start Nginx service service: name: nginx state: started
The purpose of this playbook is to install and start Nginx in webservers
group. You can modify the content according to your needs.
Ensure that Ansible can connect to the target host
Ansible uses SSH protocol by default to communicate with remote hosts, so you need to confirm the following points before running:
- The target host has been added to your inventory file (default is
/etc/ansible/hosts
) - SSH can be connected to these hosts normally (can be tested with the
ping
module)
For example, you can run first:
ansible webservers -m ping
If you return pong
, it means there is no problem with the connection; if there is an error, you need to check SSH configuration, key or network problems.
Run with the ansible-playbook command
This is the standard way to run a playbook. The basic format is as follows:
ansible-playbook your_playbook.yml
If your inventory file is not in the default location, you can add the -i
parameter to specify the path:
ansible-playbook -i /path/to/inventory your_playbook.yml
You can also add some common options to debug or control the execution process:
-
-v
or--verbose
: Show more detailed output information -
--check
or-C
: Only do "simulation runs" and will not really change the system state -
--limit
: Restrict execution only on certain host groups or hosts -
--tags
: Run tasks with specific tags only
For example, the following command will simulate running and only perform tasks with setup
tags for server1.example.com
:
ansible-playbook -i inventory.ini site.yml --check --limit server1.example.com --tags setup
Pay attention to some error-prone places
- Indentation error : YAML is very sensitive to indentation, it is recommended to use two spaces as first-level indentation
- Incorrect module parameters : For example, writing
name
aspkg_name
will cause task failure - Permissions issue : Some tasks require root permissions, remember to set
become: yes
- Incorrect inventory content : if the group name is written incorrectly or the IP address is wrong, the playbook cannot find the target host
If you are not sure what is wrong, you can use --check
and -v
to view the detailed output first, which can help you locate the problem most of the time.
Basically that's it. By mastering these steps, you can run Ansible playbook smoothly.
The above is the detailed content of How to run an Ansible playbook. 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)

Hot Topics

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
