Documentation

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.

Options

parameter required default choices comments
depth
(added in 2.2)
no
    By default, this module will 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.
      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 variables that are loaded within any directory to this regular expression.
          free-form
          no
            This module allows you to specify the 'file' option directly w/o any other options.
            ignore_files
            (added in 2.2)
            no
              List of file names to ignore. The defaults can not be overridden, but can be extended.
              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

                # Include vars of stuff.yml into the 'stuff' variable (2.2).
                - include_vars:
                    file: stuff.yml
                    name: stuff
                
                # 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
                
                # Load a variable file based on the OS type, or a default if not found.
                - include_vars: "{{ item }}"
                  with_first_found:
                    - "{{ ansible_distribution }}.yml"
                    - "{{ ansible_os_family }}.yml"
                    - "default.yml"
                
                # bare include (free-form)
                - include_vars: myvars.yml
                
                # Include all yml files in vars/all and all nested directories
                - include_vars:
                    dir: 'vars/all'
                
                # Include all yml files in vars/all and all nested directories and save the output in test.
                - include_vars:
                    dir: 'vars/all'
                    name: test
                
                # Include all yml files in vars/services
                - include_vars:
                    dir: 'vars/services'
                    depth: 1
                
                # Include only bastion.yml files
                - include_vars:
                    dir: 'vars'
                    files_matching: 'bastion.yml'
                
                # Include only all yml files exception bastion.yml
                - include_vars:
                    dir: 'vars'
                    ignore_files: 'bastion.yml'
                

                Status

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

                Support

                This module is maintained by those with core commit privileges

                For more information on what this means please read Module Support

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