include_vars - Load variables from files, dynamically within a task

New in version 1.4.

Synopsis

  • Loads variables from a YAML/JSON files dynamically from within a file or from a 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.

Options

parameter required default choices comments
depth
(added in 2.2)
no
When using dir, this module will, by default, recursively go through each sub directory and load up the variables. By explicitly setting the depth, this module will only go as deep as the depth.
dir
(added in 2.2)
no
The directory name from which the variables should be loaded.
If the path is relative, it will look for the file in vars/ subdirectory of a role or relative to playbook.
extensions
(added in 2.3)
no [u'yaml', u'yml', u'json']
List of file extensions to read when using dir.
file
(added in 2.2)
no
The file name from which variables should be loaded.
If the path is relative, it will look for the file in vars/ subdirectory of a role or relative to playbook.
files_matching
(added in 2.2)
no
Limit the files that are loaded within any directory to this regular expression.
free-form
no
This module allows you to specify the 'file' option directly without any other options. There is no 'free-form' option, this is just an indicator, see example below.
ignore_files
(added in 2.2)
no
List of file names to ignore.
name
(added in 2.2)
no
The name of a variable into which assign the included vars. If omitted (null) they will be made top level vars.

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: "{{ item }}"
  with_first_found:
    - "{{ 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]

Return Values

Common return values are documented here Return Values, the following are the fields unique to this module:

name description returned type sample
ansible_facts
Variables that were included and their values
success dict {'variable': 'value'}
ansible_included_var_files
A list of files that were successfully included
success list ['/path/to/file.yaml', '/path/to/file.json']

Notes

Note

  • This module is also supported for Windows targets.

Status

This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.

Maintenance Info

For more information about Red Hat’s this support of this module, please refer to this knowledge base article<https://access.redhat.com/articles/rhel-top-support-policies>

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.4/include_vars_module.html