ansible.builtin.include_vars (v2.4.2.0-1) — module

Load variables from files, dynamically within a task.

| "added in version" 1.4 of ansible.builtin"

Authors: Allen Sanabria (@linuxdynasty)

stableinterface | supported by core

Install Ansible via pip

Install with pip install ansible==2.4.2.0.post1

Description

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.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Include vars of stuff.yml into the 'stuff' variable (2.2).
  include_vars:
    file: stuff.yml
    name: stuff
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2)
  include_vars:
    file: contingency_plan.yml
    name: plans
  when: x == 0
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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 }}.yml"
    - "{{ ansible_os_family }}.yml"
    - "default.yml"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: bare include (free-form)
  include_vars: myvars.yml
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
  include_vars:
    dir: 'vars/all'
    extensions:
        - json
        - jsn
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Include default extension files in vars/services (2.2)
  include_vars:
    dir: 'vars/services'
    depth: 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Include only files matching bastion.yml (2.2)
  include_vars:
    dir: 'vars'
    files_matching: 'bastion.yml'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Include all .yml files except bastion.yml (2.3)
  include_vars:
    dir: 'vars'
    ignore_files: 'bastion.yml'
    extensions: ['yml']

Inputs

    
dir:
    default: null
    description:
    - 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.
    version_added: '2.2'
    version_added_collection: ansible.builtin

file:
    description:
    - 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.
    version_added: '2.2'
    version_added_collection: ansible.builtin

name:
    default: null
    description:
    - The name of a variable into which assign the included vars, if omitted (null) they
      will be made top level vars.
    version_added: '2.2'
    version_added_collection: ansible.builtin

depth:
    default: 0
    description:
    - When using C(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.
    version_added: '2.2'
    version_added_collection: ansible.builtin

free-form:
    description:
    - This module allows you to specify the 'file' option directly w/o any other options.
      There is no 'free-form' option, this is just an indicator, see example below.

extensions:
    default:
    - yaml
    - yml
    - json
    description:
    - List of file extensions to read when using C(dir).
    required: false
    version_added: '2.3'
    version_added_collection: ansible.builtin

ignore_files:
    default: null
    description:
    - List of file names to ignore.
    version_added: '2.2'
    version_added_collection: ansible.builtin

files_matching:
    default: null
    description:
    - Limit the files that are loaded within any directory to this regular expression.
    version_added: '2.2'
    version_added_collection: ansible.builtin

Outputs

ansible_facts:
  description: Variables that were included and their values
  returned: success
  sample:
    variable: value
  type: dict
ansible_included_var_files:
  description: A list of files that were successfully included
  returned: success
  sample:
  - /path/to/file.yml
  - /path/to/file.json
  type: list
  version_added: 2.4
  version_added_collection: ansible.builtin