Include vars updated to work with directories (#17207)

* New features for include_vars

include_vars.py now allows you to include an entire directory and its nested directories of variable files.

Added Features..

* Ignore by default *.md, *.py, and *.pyc
* Ignore any list of files.
* Only include files nested by depth (default=unlimited)
* Match only files matching (valid regex)
* Sort files alphabetically and load in that order.
* Sort directories alphabetically and load in that order.

```
    - include_vars: 'vars/all.yml'

    - name: include all.yml
      include_vars:
        file: 'vars/all.yml'

    - name: include all yml files in vars/all and all nested directories
      include_vars:
        dir: 'vars/all'

    - name: include all yml files in vars/all and all nested directories and save the output in test.
      include_vars:
        dir: 'vars/all'
        name: test

    - name: include all yml files in vars/services
      include_vars:
        dir: 'vars/services'
        depth: 1

    - name: include only bastion.yml files
      include_vars:
        dir: 'vars'
        files_matching: 'bastion.yml'

    - name: include only all yml files exception bastion.yml
      include_vars:
        dir: 'vars'
        ignore_files: 'bastion.yml'
```

* Added whitelist for file extensisions (yaml, yml, json)

* Removed unit tests in favor of integration tests
This commit is contained in:
Allen Sanabria 2016-08-30 14:34:31 -07:00 committed by Toshio Kuratomi
commit 03132041fb
8 changed files with 368 additions and 32 deletions

View file

@ -0,0 +1,3 @@
---
testing: 1
base_dir: defaults

View file

@ -0,0 +1,86 @@
---
- name: verify that the default value is indeed 1
assert:
that:
- "testing == 1"
- "base_dir == 'defaults'"
- name: include the vars/environments/development/all.yml
include_vars:
file: environments/development/all.yml
- name: verify that the default value is indeed 789
assert:
that:
- "testing == 789"
- "base_dir == 'environments/development'"
- name: include the vars/environments/development/all.yml and save results in all
include_vars:
file: environments/development/all.yml
name: all
- name: verify that the values are stored in the all variable
assert:
that:
- "all['testing'] == 789"
- "all['base_dir'] == 'environments/development'"
- name: include the all directory in vars
include_vars:
dir: all
depth: 1
- name: verify that the default value is indeed 123
assert:
that:
- "testing == 123"
- "base_dir == 'all'"
- name: include every directory in vars
include_vars:
dir: vars
- name: verify that the variable overwrite based on alphabetical order
assert:
that:
- "testing == 456"
- "base_dir == 'services'"
- "webapp_containers == 10"
- name: include every directory in vars except files matching webapp.yml
include_vars:
dir: vars
ignore_files:
- webapp.yml
- name: verify that the webapp.yml file was not included
assert:
that:
- "testing == 789"
- "base_dir == 'environments/development'"
- name: include only files matching webapp.yml
include_vars:
dir: environments
files_matching: webapp.yml
- name: verify that only files matching webapp.yml and in the environments directory get loaded.
assert:
that:
- "testing == 101112"
- "base_dir == 'development/services'"
- "webapp_containers == 20"
- name: include only files matching webapp.yml and store results in webapp
include_vars:
dir: environments
files_matching: webapp.yml
name: webapp
- name: verify that only files matching webapp.yml and in the environments directory get loaded into stored variable webapp.
assert:
that:
- "webapp['testing'] == 101112"
- "webapp['base_dir'] == 'development/services'"
- "webapp['webapp_containers'] == 20"

View file

@ -0,0 +1,3 @@
---
testing: 123
base_dir: all

View file

@ -0,0 +1,3 @@
---
testing: 789
base_dir: 'environments/development'

View file

@ -0,0 +1,4 @@
---
testing: 101112
base_dir: 'development/services'
webapp_containers: 20

View file

@ -0,0 +1,4 @@
---
testing: 456
base_dir: services
webapp_containers: 10

View file

@ -0,0 +1,5 @@
---
- hosts: 127.0.0.1
gather_facts: False
roles:
- { role: test_include_vars }