Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ansible-- organization variable

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

There are many ways to define variables in ansible, we don't need to pay too much attention to them, we just need to master a few commonly used variable definitions and application methods. This article records the definition of variables in external files, and then introduces the variables in these external files.

There are two ways to introduce files that hold variables: include_vars and vars_files. In addition, you can use the "- e" or "--extra-vars" options on the command line to introduce.

1 、 vars_files

Vars_files is an play-level instruction that can be used to introduce one or more external files that hold variables in the truncation of parsing playbook.

For example, the pb.yml file is as follows:

-name: play1 hosts: node gather_facts: false vars_files:-varfile1.yml-varfile2.yml tasks:-debug: msg: "var in varfile1: {{var1}}"-debug: msg: "var in varfile2: {{var2}}"

The pb.yml file introduces two variable files through vars_files. The syntax format of the variable file is as follows:

[root@ansible roles] # cat varfile1.yml # the first variable file is as follows-var1: "value1" var11: "value11" [root@ansible roles] # cat varfile2.yml # the second variable file is as follows-- var2: "value2" var22: "value22"

Note: the vars_files instruction is at the play level and is loaded and parsed when parsing the playbook, so the variables introduced are available within the play scope and cannot be used by other play.

2 、 include_vars

The include_vars directive can also be used to introduce external variable files, unlike vars_files. On the one hand, include_vars is a function provided by the module, it is a real task, so variables are not created until the task is executed. On the other hand, since include_vars is a task, it can be controlled by some task-level instructions, such as when instructions.

The chestnuts are as follows:

[root@ansible roles] # cat include_vars.yml-name: play1 hosts: localhost gather_facts: false tasks:-name: varfile1.yml when: 3 > 2-debug: msg: "var in varfile1: {{var1}}"

The variable file in the chestnut above can be introduced by directly specifying the file name, include_vars: varfile1.yml, or you can explicitly use the file parameter to specify the path, as follows:

-name: include vars from files include_vars: file: varfile1.yml

If you want to bring in multiple files, you can use a loop, such as:

-name: include vars from files include_vars: file: "{{item}}" loop:-varfile1.yml-varfile2.yml

It should be noted that include_vars requires that the file already exists when it is introduced. If there are multiple possible files but are not sure whether the file already exists, you can use the with_first_found instruction or lookup's first_found plug-in, both of which are used to find out the existing files from the file list and stop as soon as they are found.

The chestnuts are as follows:

Tasks:-name: include vars from files include_vars: file: "{{item}}" with_first_found:-varfile1.yml-varfile2.yml-default.yml# is equivalent to tasks:-name: include vars from files include_vars: file: "{lookup ('first_found' Any_files)} "vars: any_files:-varfile1.yml-varfile2.yml-default.yml

In addition, include_vars can import multiple files from a directory, which is recursive to subdirectories by default, such as:

-name: include vars from files include_vars: dir: vars/all3,-- extra-vars option

The-e option or-- extra-vars option of the ansible-playbook command can also be used to define variables or to introduce variable files

Chestnut:

# define a single variable ansible-playbook-e 'var1= "value1"' xxx.yml# define multiple variables ansible-playbook-e 'var1= "value1" var2= "value2"' xxx.yml# introduce a single variable file ansible-playbook-e'@ varfile1.yml' xxx.yml# introduce multiple variable files ansible-playbook-e'@ varfile1.yml'-e'@ varfile2.yml' xxx.yml

Because variables are defined by options, the variables it defines are global and valid for all play.

In general, the-e option is not recommended because it is neither transparent nor friendly, and requires us to remember which variables to define.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report