ansible - organization variable

There are many ways to define variables in ansible. We don't need to pay much attention to them. We just need to master several 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 bring in.

1,vars_files

Vars file is a play level instruction, which can be used to introduce one or more external files with variables saved in parsing the truncation of 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}}"

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 contents of the first variable file are 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 a play level instruction and is loaded and parsed when parsing the playbook. Therefore, the introduced variables are available within the play range, and other plays cannot use these variables.

2,include_vars

The include? Vars instruction can also be used to introduce external variable files. It is different from vars? Files. On the one hand, include? Vars is a function provided by the module. It is a real task, so variables will be created after 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 instruction.

The chestnuts are 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 way to import variable files into the chestnuts above is to 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 multiple files, you can use a circular approach, such as:

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

It should be noted that the include? Vars requires that the file already exists when it is imported. If there are multiple possible files but you are not sure whether the file already exists, you can use the with? First? Found instruction or the first? Found plug-in of lookup. They have the same function. They are used to find the existing file from the file list and stop immediately after finding it.

The chestnuts are as follows:

  tasks:
    - name: include vars from files
      include_vars:
        file: "{{item}}"
      with_first_found:
        - varfile1.yml
        - varfile2.yml
        - default.yml
#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 also import multiple files from a directory. By default, it recurs to a subdirectory, for example:

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

3. -- extra vars option

The - e option or the - extra vars option of the ansible playbook command can also be used to define variables or import 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
#Import single variable file
ansible-playbook -e '@varfile1.yml'  xxx.yml
#Import multiple variable files
ansible-playbook -e '@varfile1.yml' -e '@varfile2.yml'  xxx.yml

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

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

Keywords: Linux ansible

Added by linuxdream on Sat, 25 Jan 2020 12:24:17 +0200