include_vars – Load variables from files, dynamically within a task¶
Synopsis¶
- Loads YAML/JSON variables dynamically from a file or directory, recursively, during task runtime.
- If loading a directory, the files are sorted alphabetically before being loaded.
- This module is also supported for Windows targets.
- To assign included variables to a different host than inventory_hostname, usedelegate_toand setdelegate_facts=yes.
Parameters¶
See Also¶
See also
- set_fact – Set host facts from a task
- The official documentation on the set_fact module.
- Delegation, Rolling Updates, and Local Actions
- More information related to task delegation.
Examples¶
- name: Include vars of stuff.yaml into the 'stuff' variable (2.2).
  include_vars:
    file: stuff.yaml
    name: stuff
- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2)
  include_vars:
    file: contingency_plan.yaml
    name: plans
  when: x == 0
- name: Load a variable file based on the OS type, or a default if not found. Using free-form to specify the file.
  include_vars: "{{ lookup('first_found', possible_files) }}"
  vars:
    possible_files:
      - "{{ ansible_distribution }}.yaml"
      - "{{ ansible_os_family }}.yaml"
      - default.yaml
- name: Bare include (free-form)
  include_vars: myvars.yaml
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
  include_vars:
    dir: vars/all
    extensions:
        - json
        - jsn
- name: Include all default extension files in vars/all and all nested directories and save the output in test. (2.2)
  include_vars:
    dir: vars/all
    name: test
- name: Include default extension files in vars/services (2.2)
  include_vars:
    dir: vars/services
    depth: 1
- name: Include only files matching bastion.yaml (2.2)
  include_vars:
    dir: vars
    files_matching: bastion.yaml
- name: Include all .yaml files except bastion.yaml (2.3)
  include_vars:
    dir: vars
    ignore_files: [bastion.yaml]
    extensions: [yaml]
- name: Ignore warnings raised for files with unknown extensions while loading (2.7)
  include_vars:
    dir: vars
    ignore_unknown_extensions: True
    extensions: ['', 'yaml', 'yml', 'json']
Return Values¶
Common return values are documented here, the following are the fields unique to this module:
| Key | Returned | Description | 
|---|---|---|
| ansible_included_var_files 
                  list
                                       added in 2.4 | success | A list of files that were successfully included Sample: ['/path/to/file.json', '/path/to/file.yaml'] | 
Status¶
- This module is guaranteed to have backward compatible interface changes going forward. [stableinterface]
- This module is maintained by the Ansible Core Team. [core]
Red Hat Support¶
More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article.
Authors¶
- Allen Sanabria (@linuxdynasty)
Hint
If you notice any issues in this documentation, you can edit this document to improve it.
