ansible-- organizational variables

There are many definitions of variables in the ansible in the way, we do not need to pay attention too much, just need to master a few common variables can be defined and applied way, this article is to record the variables defined in an external file, then go the introduction of these external variables in the file.

Save the file variable is introduced in two ways: include_vars and vars_files. In addition, you can also use the "-e" or "--extra-vars" option on the command line to introducing.

1, vars_files

vars_files level is a play command may be used to introduce one or more truncated parsing playbook external files saved variables.

For example, pb.yml file 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}}"

pb.yml file by file vars_files introduces two variables, variable file syntax is as follows:

[root@ansible roles]# cat varfile1.yml         #第一个变量文件内容如下
---
var1: "value1"
var11: "value11"
[root@ansible roles]# cat varfile2.yml         #第二个变量文件内容如下
---
var2: "value2"
var22: "value22"

Note: vars_files play instruction is an instruction level, and is loaded and parsed when parsing the playbook, the variables introduced within the range of play is available, the other can not use these variables play.

2、include_vars

include_vars instructions can also be used to introduce an external variable file, it vars_files different, on the one hand, is include_vars module provides functionality, it is a real task, it will create a variable after the task execution. On the other hand, since include_vars is a task, he can be a number of task-level control commands, such as when instructions.

Chestnuts as follows:

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

The introduction of variable file chestnuts in the above manner is directly specify the file name, include_vars: varfile1.yml, you can also 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 introduce more than one file, you can use a circular manner, for example:

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

Note that, include_vars requirements already exist at the time of the introduction of the file, if there are multiple possible file but are not sure whether the file already exists, you can use with_first_found instruction or lookup of first_found plug-in, they have the same effect, are used to from a file the list to find the file exists, and stop immediately if found.

Chestnuts as follows:

  tasks:
    - name: include vars from files
      include_vars:
        file: "{{item}}"
      with_first_found:
        - varfile1.yml
        - varfile2.yml
        - default.yml
#等价于

  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 the directory, recursively into subdirectories by default, for example:

    - name: include vars from files
      include_vars:
        dir: vars/all

3, - extra-vars option

-e option or command ansible-playbook --extra-vars option can also be introduced into the variable or variables used to define files

chestnut:

#定义单个变量
ansible-playbook -e 'var1="value1"'  xxx.yml
#定义多个变量
ansible-playbook -e 'var1="value1" var2="value2"'  xxx.yml
#引入单个变量文件
ansible-playbook -e '@varfile1.yml'  xxx.yml
#引入多个变量文件
ansible-playbook -e '@varfile1.yml' -e '@varfile2.yml'  xxx.yml

Because the variables are defined by way of options, so it defines a variable is global, valid for all play.

Generally speaking, the -e option is not recommended, since it is neither transparent nor friendship, ask us to remember which variables to be defined.

Guess you like

Origin blog.51cto.com/14154700/2468386