freehck.var_loader

freehck.var_loader

This role helps include variables from a specific directory.

Idea

When we were developing CI/CD for a large project, we realized that storing environment variables in separate folders for each environment is really useful. We also found it helpful to separate credentials and common variables into different directories. The folder structure looks like this:

.
├── common-vars
│   ├── project-A
│   │   └── dev
│   └── project-B
│       └── dev
├── credentials
│   ├── project-A
│   │   ├── dev
│   │   └── prod
│   └── project-B
│       ├── dev
│       └── prod
└── env-vars
    ├── project-A-env01
    ├── project-A-env02
    ├── project-A-prod
    ├── project-B-env01
    ├── project-B-env02
    └── project-B-prod

We group hosts from the same environment together (for example, project-a-env01), and mention in the corresponding file under group_vars which directories to load variables from (as shown in the example below).

The advantage of this method is that included variables always take priority (except for --extra-vars), so it's clear where to find your environment parameters.

Role Variables

  • var_loader_root: the path where environment directories are stored.
  • var_loader_group_name: the directory with environment variables; this path is relative to var_loader_root.
  • var_loader_src: if specified, the role will only load variables from this directory.

Role Defaults

  • var_loader_src: "{{ var_loader_root }}/{{ var_loader_group_name }}"

Example Playbook

This snippet will include all *.yml files from playbook_dir/vars/dev:

- hosts: group-containing-all-hosts-for-dev-env
  roles:
    - role: freehck.var_loader
      var_loader_root: "{{ playbook_dir }}/vars"
      var_loader_group_name: "dev"

This snippet will include all *.yml files from /home/user/my-env:

- hosts: group-containing-all-hosts-for-dev-env
  roles:
    - role: freehck.var_loader
      var_loader_src: "/home/user/my-env"

This snippet will include all *.yml files from: playbook_dir/creds/dev, playbook_dir/env-vars/dev, playbook_dir/common-vars/my-dev-env:

--- playbook.yml ---
- hosts: group-containing-all-hosts-for-dev-env
  roles:
    # load credentials
    - role: freehck.var_loader
      var_loader_root: "{{ playbook_dir }}/creds"
      var_loader_group_name: "{{ credentials_library }}"
    # load variables for this environment group
    - role: freehck.var_loader
      var_loader_root: "{{ playbook_dir }}/common-vars"
      var_loader_group_name: "{{ env_group_name }}"
    # load environment specific variables
    - role: freehck.var_loader
      var_loader_root: "{{ playbook_dir }}/env-vars"
      var_loader_group_name: "{{ env_name }}"
--- group_vars/group-containing-all-hosts-for-dev-env.yml ---
credentials_library: "dev"
env_group_name: "dev"
env_name: "my-dev-env"

License

MIT

Author Information

Dmitrii Kashin, freehck@freehck.ru

Informazioni sul progetto

loader for environment variables

Installa
ansible-galaxy install freehck.var_loader
Licenza
Unknown
Download
2.5k
Proprietario