Ansible detailed explanation (8) - Ansible playbook variable

Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the definition and use of variables in Ansible's playbook.

First, the command input variable

A simple and typical Ansible Playbook playbook looks like this:

---
- hosts: exp
  remote_user: root
  tasks:
  - name: Install Apache by YUM
    yum: name={
    
    {
    
     bl }} state=installed

In the above Playbook, a variable is used in the yum module. When the variable is defined, it is enclosed in two curly brackets, and there is a space on both sides of the variable name.
After the introduction of variables, Ansible's Playbook's flexibility has been greatly improved, which can make our Playbook more suitable for use. So how do we assign values ​​to the above variables? First, we introduce a way to assign values ​​when calling a Playbook from the Ansible command line.
Excuting an order:

ansible-playbook tmp.yml -e "bl=tree"

As shown above, when executing the ansible-playbook command, use the -e parameter to assign the variable, and the tree can be assigned to the bl variable in the Playbook. The execution result of the above command is as follows:
insert image description here

2. Define variables in the hosts file

In addition to Ansible's ability to assign values ​​when calling the ansible-palybook command, Ansible also supports defining variables in the hosts file. The variables of the specified module can be set in the hosts file, and different variables can be set for each different module, which further enhances the flexibility of Ansible Playbook. If we also use the above Playbook, then we can define variables in the hosts file as follows:

[exp]
192.168.136.11
[exp:vars]
bl=tree

In this way, after the above variables are configured, we can directly execute the Playbook, and the execution results are as follows:
insert image description here

3. Define variables in the playbook file

In addition to defining variables in the Ansible hosts file, we can also define variables in the playbook. We can modify the above Playbook as follows:

---
- hosts: exp
  vars:
   bl: tree
  remote_user: root
  tasks:
  - name: Install Apache by YUM
    yum: name={
    
    {
    
     bl }} state=installed

It can be seen that in the above Playbook, the bl variable is defined. When defining variables in this way, be sure to pay attention to the YAML-compliant format. After the above Playbook is completed, the execution result is as follows:
insert image description here

Fourth, the setup module obtains variables

Every time during the execution of Ansible Playbook, we can see the information as shown in the following figure.
insert image description here
During the above Gathering Facts process, Ansible collects the information of the controlled end. In this process, Ansible can Some information on the terminal is collected locally and saved in the form of variables. So, when we're writing an Ansible Playbook, we can use these variables.
There are many variables that Ansible gets through setup, and we can execute the command:

ansible exp -m setup

To call the setup module to obtain the information of the controlled device, the result is as follows:
insert image description here
A typical Playbook that uses the above method to define variables is as follows:

---
- hosts: exp
  vars:
   bl: tree
  remote_user: root
  tasks:
  - name: Install Apache by YUM
    file: name=/root/{
    
    {
    
     ansible_all_ipv4_addresses }} state=touch

After the above Playbook is executed, a file named after the IP address of the device can be created on the controlled device.

Five, independent variable file

In addition to the above methods, one of the most commonly used variable declaration methods in Ansible is to store variables as independent variable files. We can create a variable file var.yml ending in .yml with the following contents:

bl: tree

Note that there must be a space after the colon in the above configuration, so as to conform to the YAML format.
Modify the Playbook file as follows:

---
- hosts: exp
  vars_files:
   - ./var.yml
  remote_user: root
  tasks:
  - name: Install Apache by YUM
    yum: name={
    
    {
    
     bl }} state=installed

In the above configuration, we introduced the variables in the var.yml file where we just defined the variables by referencing vars_files. The execution result of the above Playbook is as follows:
insert image description here
It can be seen that the variable definition through the variable file is also successful!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123504990