Ansible (7) conditionals and loops, custom loops plug-in

Source: Internet
Author: User

Ansible's conditionals and loops are really powerful compared with salt-stack. Salt-stack uses a judgment loop. It seems that only the judgment and loop in the template can be used. Ansible itself comes with a judgment loop plug-in for various scenarios ..

Not only that, like loops, we can also write our own plug-ins to meet our actual needs.

Let's talk about conditionals first.

Conditionals is simpler than loops. It is a keyword of when.

It is similar to the condition judgment of other programming languages. The bool value or bool value is added after the when.

The following is a small example to illustrate the usage of when and loops,

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/F8/wKioL1O1FNDTDs_DAAMRkoKo3Ic075.jpg "Title =" loop.png "alt =" wkiol1o1fndtds_daamrko3ic075.jpg "/>

If the result is executed too much, it will not be posted. Paste the script and you can execute it yourself.

---- Hosts: salt-master tasks:-Name: with_items, the parameter is a list, and the list element is a string or dictionary Debug: MSG = '{item. name }}and {item. age }}'with_items:-{Name: 'lixc ', age: 25}-{Name: 'liss', age: 24} When: 1-Name: with_nested, print the nested list. The parameter is the list. The list element is the list. Debug: MSG = '{item [0] }}and {item [1] }}' with_nested: -['lixc ', 'liss']-[1, 2] When: ansible_ OS _family = "Debian"-Name: with_dict, loop dictionary. The parameter is the dictionary Debug: MSG = '{item. key }}and {item. value} 'with_dict: ansible: cmd: CommandLine ply: playbooks salt: cmd: CommandLine ST: State when: ansible_ OS _family = "Debian" and "8" | int> = 6-Name: with_fileglob, traverse the files in the folder, excluding folders. The roles can be used as the relative path Debug: MSG = {item} with_fileglob:-/tmp/test1/*-/tmp/Test2/* When: lixc is not defined-Name: with_sequence, get the integer sequence Debug: MSG = {item} with_sequence: Start = 4 end = 8 stride = 2 when: 1 in [1, 2, 3]


This section lists some usage of conditionals and loops. Of course, this is not only used here. The key is to combine conditionals and loops with variables.


Didn't we say we can customize the loops plug-in above? below, the landlord also plays with the whole plug-in.

The loops plug-in of ansible is under this directory.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/FC/wKiom1O1IG6xg_nfAADVk94mhYM243.jpg "Title =" jieguo.png "alt =" wkiom1o1ig6xg_nfaadvk94mhym243.jpg "/> can I see this directory?

This directory is where all the loops plug-ins of ansible are located. The plugin dir. py is a plug-in developed by the landlord.

What is the main plug-in doing?

We mentioned a plug-in that comes with the fileglob ansible. This plug-in can be used for files in variable folders, but does not include subdirectories and files.

By using this plug-in, you can traverse all the files in the Directory and the files in the subdirectories.

Let's look at the landlord's code.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/38/FC/wKiom1O1ISixW2-VAAIdQ1OkmPk447.jpg "Title =" bianli.png "alt =" wKiom1O1ISixW2-VAAIdQ1OkmPk447.jpg "/>


OK. Let's see what playbooks is about.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/38/FD/wKioL1O1IaTgQB5DAACDLve2xE4732.jpg "Title =" bianli.png "alt =" wkiol1o1iatgqb5daacdlve2xe4732.jpg "/>


Okay. Let's take a look at the execution results.

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


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

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.