Table of Contents
The following are some basic uses of variables in Ansible Playbook:
Define variables:
Use variables:
Register variables:
Use special variables:
Variable priority:
Home Computer Tutorials Computer Knowledge Ansible playbook variables (basic usage of variables)

Ansible playbook variables (basic usage of variables)

Feb 18, 2024 pm 04:45 PM
ansible playbook Brighten

Ansible is an open source automated configuration management and deployment tool that helps administrators automate tasks on multiple servers.

In Ansible, Playbook is a YAML file used to describe automation tasks.

Using variables (variables) is an important part of Playbook functionality, which can make your Playbook more flexible, reusable and easy to maintain.

Ansible playbook变量(变量基本用法)

The following are some basic uses of variables in Ansible Playbook:

Define variables:

Variables can be defined in Playbooks, inventory files, vars files, or on the command line using the -e parameter.

example:

Define variables in Playbook:

---
- name: Set up web server
hosts: webservers
vars:
http_port: 80
max_clients: 200
Copy after login

Define variables in the inventory file:

[webservers]
web1.example.com http_port=80 max_clients=200
web2.example.com http_port=8080 max_clients=100
Copy after login

Use variables:

Variables can be used in tasks, templates, conditional statements, etc.

example:

- name: Ensure Apache is running
ansible.builtin.service:
name: httpd
state: started
enabled: yes
when: http_port == 80
Copy after login

Register variables:

After a task is executed, the result can be saved to a variable through the register keyword.

example:

- name: Check if package is installed
ansible.builtin.package_facts:
register: package_info

- name: Display package information
ansible.builtin.debug:
var: package_info
Copy after login

Use special variables:

Ansible provides some built-in special variables, such as inventory_hostname, ansible_facts, etc.

example:

- name: Display the current host's name
ansible.builtin.debug:
var: inventory_hostname
Copy after login

Variable priority:

In Ansible, the priority of variables from low to high is: default value, variables defined in the inventory file, variables in the Playbook, and command line parameters. Variable values ​​with higher priority override variable values ​​with lower priority.

After understanding the basic concepts and usage of Ansible Playbook variables, you can flexibly use variables according to actual needs to optimize your automation tasks.

The above is the detailed content of Ansible playbook variables (basic usage of variables). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of PS1, PS2, PS3, PS4 variables under Linux Detailed explanation of PS1, PS2, PS3, PS4 variables under Linux Mar 22, 2024 am 09:00 AM

In the Linux system, PS1, PS2, PS3 and PS4 are specific environment variables, each of which plays a different role in controlling prompts and menu prompt information. These PS variables can provide users with a flexible way to customize and control the appearance and behavior of the command line in Linux systems, thereby improving the user's interaction experience with the system. Note: If you want to permanently modify the prompt for a long time, you can add the command to modify the prompt to the $HOME/.bashrc or $HOME/.bash_profile file. 1. PS1 variable is mainly used to set the shell command prompt. Whenever you enter a command in the terminal, the string displayed is controlled by the PS1 variable. can be based on your own

Detailed explanation of how Ansible works Detailed explanation of how Ansible works Feb 18, 2024 pm 05:40 PM

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

Ansible Ad-Hoc (peer-to-peer mode) Ansible Ad-Hoc (peer-to-peer mode) Feb 18, 2024 pm 04:48 PM

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

How to solve the problem of slow execution speed of ansible How to solve the problem of slow execution speed of ansible Mar 05, 2024 pm 05:34 PM

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

Linux remote management tools: five recommended tools Linux remote management tools: five recommended tools Feb 24, 2024 pm 11:18 PM

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

Exploring the Linux remote management artifact: five practical tools recommended Exploring the Linux remote management artifact: five practical tools recommended Feb 23, 2024 pm 01:00 PM

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 playbook variables (basic usage of variables) Ansible playbook variables (basic usage of variables) Feb 18, 2024 pm 04:45 PM

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 does Java interface with the Ansible automatic operation and maintenance platform? How does Java interface with the Ansible automatic operation and maintenance platform? Apr 20, 2023 pm 04:40 PM

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

See all articles