Ansible (5) variables (1) Definition

Source: Internet
Author: User
Tags vars

Speaking of variables, the variables in ansible are almost the same as those in the pillar + grains in the salt-stack.

The main sources of variables in ansible are as follows:

  1. Inventory (host vars, group vars)

  2. Playbook

  3. Command Line

  4. The Playbook generates results during task execution. It can be register and used as a variable for the following task.

  5. Roles

  6. Facts from nodes

These types of variables, 1-4, are generally defined by users, while 5facts is mainly pulled by ansible from nodes. Of course, facts can also be defined by users.

Let's take a look at how these variables work?

Let's take a look at the variables in inventory. I wrote them in the second article.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/0A/wKioL1OyOnXg2xBFAAHrJW6zNzE525.jpg "Title =" jieguo.png "alt =" wkiol1oyonxg2xbfaahr%6znze525.jpg "/>

Let's take a look at the variables in playbooks.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/0B/wKiom1OyOYfANSmPAACvKb0tTeg294.jpg "Title =" jieguo.png "alt =" wkiom1oyoyfansmpaacvkb0tteg294.jpg "/>

OK. You can also see that variables can be defined in playbooks, or variables in external files can be imported.

Okay, let's look at the variables in the command line.

There are three methods to pass variables to playbooks through the command line.

Let's look at the 1st methods, which is relatively simple.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/0D/wKiom1OySC2wrcIoAAKsYn3qb5s087.jpg "Title =" jieguo.png "alt =" wkiom1oysc2wrcioaaksyn3qb5s087.jpg "/>

You can use the following 2nd methods to transfer variables in JSON format.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/38/0C/wKioL1OySQnBWSrOAAHenL-EzXM119.jpg "Title =" jieguo.png "alt =" wKioL1OySQnBWSrOAAHenL-EzXM119.jpg "/>

3rd methods to input a JSON file. However, the second and third methods are generally used when the form of variables is complex. However, it is generally seldom used, because it is not necessary to use it when the command line is spread, or the first command line transmission method is a bit common.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/0D/wKiom1OySfjh98iOAAI2bf-giK0021.jpg "Title =" jieguo.png "alt =" wKiom1OySfjh98iOAAI2bf-giK0021.jpg "/>

OK. Method 4: Result of register task, used as a variable for the following task

Let's take a look at a small example. This is relatively simple and the results will not be printed.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/19/wKiom1Oyo2SAZSeRAACUxR9Oc9I874.jpg "Title =" jieguo.png "alt =" wkiom1oyo2sazseraacuxr9oc9i874.jpg "/>


OK. The fifth method for defining variables is in roles.

This is not much to say. I have just written the previous article.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/0F/wKiom1OyS0rAetcRAAD1PyhWe0Y083.jpg "Title =" fruit .png "alt =" wkiom1oys0raetcraad1pyhwe0y083.jpg "/>

The sixth method is to obtain the grains from nodes, which is similar to those in salt-stack. It is mainly a system information, BIOS information, network information, and hard disk information of nodes .. However, the author observed the information obtained by facts compared to grains in salt-stack. More.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/38/0F/wKiom1OyTPmTjRomAACSQ5taLp8855.jpg "Title =" jieguo.png "alt =" wkiom1oytpmtjromaacsq5talp8855.jpg "/>


OK. Here we will talk about how to get facts from nodes. We didn't talk about it before. Can we define facts by ourselves?

There are multiple methods to customize facts. If you are competent, you can directly modify and set this module. But we should not change the official stuff.

The second method is defined on nodes, which is defined in/etc/ansible/fact. d/* fact by default.

We define the file format here. There are three formats: ini format, JSON format, or executable file. However, they must return JSON format.

Let's look at an example.

Let's take a look at the three files defined in JSON, INI, and executable scripts.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/38/14/wKiom1OyZjPyXdyMAAGA_cFxbZQ734.jpg "Title =" json.png "alt =" wkiom1oyzjpyxdymaaga_cfxbzq734.jpg "/>

OK. Create a folder on the node and transfer the fact file for testing. Modify the properties of the lss. fact file to the executable permissions of other groups. Otherwise, the file cannot be executed. The landlord tried the-S parameter and cannot execute it.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/15/wKiom1OybHjiC4b5AAJl5VuIerU467.jpg "Title =" json.png "alt =" wkiom1oybhjic4b5aajl5vuieru467.jpg "/>


Well, this custom method is even finished. There is also a custom method, which is actually a custom module, but this module returns a result in the facts format.

Okay, let's customize a folder in the module of ansible, put our module, and then drop it into the custom module.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/15/wKioL1Oyc_3DOsLfAAI2dFz6ahw112.jpg "Title =" module.png "alt =" wkiol1oyc_3doslfaai2dfz6ahw112.jpg "/>


OK, see it. Here we get an output from the chengge module, But what should we do?

In fact, we want to use the facts returned by the module. Obviously, we must first execute the following custom module before using it.

OK. Let's take a look at this example.

First look at playbooks

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/15/wKioL1OydVWTWNuuAACVUUgiIhc254.jpg "Title =" fact.png "alt =" wkiol1oydvwtwnuuaacvuugiihc254.jpg "/>

Let's look at the execution result.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/38/15/wKioL1OydZiR2Sv-AAHVPuAM0dU904.jpg "Title =" jieguo.png "alt =" wKioL1OydZiR2Sv-AAHVPuAM0dU904.jpg "/>



This article is from the "West Wind" blog, please be sure to keep this source http://lixcto.blog.51cto.com/4834175/1433138

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.