almaops.vars

DEPRECATION WARNING

This Ansible role is no longer being supported and has been replaced by the almaops.common collection.

almaops.vars

This role handles variables from a specific folder.

Idea

While building CI/CD for a large project, we discovered it was helpful to keep variables organized by each environment in separate folders. We also realized the advantage of separating credentials and common variables into different folders. 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 define in a file under group_vars where to load the variables from (as shown in the example below).

This method assures that the included variables always have the highest priority (except for --extra-vars), making it clear where to find your environment parameters.

Role Variables

  • vars_root: the root directory where environment folders are stored
  • vars_group: the folder for environment variables, with the path relative to vars_root
  • vars_source: if set, the role will only load variables from this specific folder

Role Defaults

  • vars_source: "{{ vars_root }}/{{ vars_group }}"

Example Playbook

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

- hosts: group-containing-all-hosts-for-dev-env
  roles:
    - role: almaops.vars
      vars_root: "{{ playbook_dir }}/vars"
      vars_group: "dev"

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

- hosts: group-containing-all-hosts-for-dev-env
  roles:
    - role: almaops.vars
      vars_source: "/home/user/my-env"

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

--- playbook.yml ---
- hosts: group-containing-all-hosts-for-dev-env
  roles:
    # load credentials
    - role: almaops.vars
      vars_root: "{{ playbook_dir }}/creds"
      vars_group: "{{ credentials_library }}"
    # load variables for this environment group (group of similar environments)
    - role: almaops.vars
      vars_root: "{{ playbook_dir }}/common-vars"
      vars_group: "{{ env_group_name }}"
    # load environment-specific variables
    - role: almaops.vars
      vars_root: "{{ playbook_dir }}/env-vars"
      vars_group: "{{ 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

Informazioni sul progetto

Loading ansible variables from files

Installa
ansible-galaxy install almaops.vars
Licenza
Unknown
Download
221
Proprietario
Premium IT Consulting