Ansible (6) variables (2) Use

Source: Internet
Author: User

In the previous article, we summarized the variables in ansible, where did this come from. The examples given by the landlord may be more idiotic, But they focus on the principles and processes. When you have actual needs, you can have a great deal of fun.

As you said, where does variables come from? How should variables be used?

It can be used in a simple way or in a complex way. Simply using variables, you can use {Variable} to enclose them in braces. For advanced use, it is necessary to make more flexible use of the variable, can be used in combination with cycle, Condition Selection and other process control methods.


When it comes to variables, it's almost used in two places, playbooks and template.

Mark the official jinja2 documents

Http://jinja.pocoo.org/docs/templates/#builtin-filters

The following is an example of no practical function. Test the function.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/18/wKioL1OyoRvyghXPAAH5-7QlvFE096.jpg "Title =" jieguo.png "alt =" wKioL1OyoRvyghXPAAH5-7QlvFE096.jpg "/>

Briefly,

19 lines, deliberately executing an incorrect command

Rows 25, 29, 30, and 34 are all passed |, and the variable is processed. | It is the filter in jinja2, which means our

Variable | method, use | the following method to process the variable to get a new result.

Row 25 determines whether the result is failed or success, and returns true when falied

The 29 rows are collected from the list1 and list2 lists.

30 rows. Check whether the value of DIC ['dic1'] matches "li *". If the value matches, true is returned. Otherwise, false is returned.

34 rows, compare the size, and judge whether DIC ['dic3'] is equal to 10. If it is equal to true, false is returned.


There are many similar filters, so we will not list them here. If you are fine, you can go to the official website or jinja2 official website.


Let's take a look at the content in the template.

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

Lines 1, 2, and 10 use facts.

9 rows, used. Facts returned by the custom module. Default (0) of 9 rows is the default value. If the variable chengge does not exist, the result of this expression is 0.

The five rows use the variables defined in playbooks and the filter function of the template.

Eight and ten rows, two of the three built-in variables in ansible are used, and the three are groups, group_names, and hostvars.


That's all. Check the results.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/18/wKioL1Oypxfib3waAAMmrTdMPZU623.jpg "Title =" jieguo.png "alt =" wkiol1oypxfiber 3waaammrtdmpzu623.jpg "/>

OK, go home!



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

Related Article

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.