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