Lesson 5 of 6 · Config Management with Ansible

Roles, Collections, and Vault

Reusable roles, collections of modules for databases and clouds, keeping secrets with Ansible Vault, and a typical project layout.

Beginner 5 sections 4 min read

Roles: reusable building blocks

As playbooks grow, teams split them into roles: packaged, reusable units such as "OS baseline", "Oracle install", or "Oracle database". Roles use modules from collections, Ansible's way of distributing modules, and secrets are kept encrypted with Ansible Vault. Together, they turn a pile of playbooks into a maintainable automation library.

A role is a folder with a standard layout. Ansible knows where to find each part, so you just name the role and it's all loaded:

roles/oracle_install/
├── defaults/main.yml     # default values, easy to override: oracle_version, oracle_base
├── vars/main.yml         # fixed values the role needs
├── tasks/main.yml        # the tasks, in order
├── handlers/main.yml     # tasks that run when notified, such as restarting a listener
├── templates/            # files with variables, such as db_install.rsp.j2
├── files/                # files copied as they are
└── meta/main.yml         # description and dependencies on other roles

ansible-galaxy role init oracle_install creates this skeleton for you. The playbook that uses roles becomes short and readable:

# provision_oracle.yml
- name: Build an Oracle database server
  hosts: oracle
  become: true
  roles:
    - os_baseline         # server team: patches, hardening, kernel settings
    - oracle_install      # DBA team: Oracle software
    - oracle_db           # DBA team: database, parameters, users, backups

This is the same structure the server and DBA teams shared in Git in Practice: each team owns its roles, and one playbook brings them together.

Collections

A collection is a package of modules, roles, and plugins, distributed through Ansible Galaxy or Red Hat's Automation Hub. That's why modules have three-part names: community.postgresql.postgresql_user is the postgresql_user module from the community.postgresql collection.

Playbook what to build, where role: os_baselinerole: oracle_installrole: oracle_db COLLECTIONS OF MODULES ansible.builtinansible.posixcommunity.generalcommunity.postgresqlcommunity.mysqloracle.oci files, packageskernel, mountslimits, morePostgreSQLMySQLOCI cloud
CollectionUseful for DBAs
ansible.builtinIncluded with Ansible: files, packages, users, services, templates, commands
ansible.posix, community.generalKernel parameters, mounts, limits, and hundreds of utilities
community.postgresql, community.mysqlIdempotent database, user, privilege, and setting modules
microsoft.sqlInstalling and configuring SQL Server on Linux
oracle.ociOracle's own collection for OCI resources, including databases, and the OCI dynamic inventory
Community Oracle collectionsProjects such as opitzconsulting.ansible_oracle provide roles and modules for installing, creating, and patching Oracle Database

List the collections a project needs in a requirements.yml file, with versions, so everyone and every pipeline installs the same ones:

# collections/requirements.yml
collections:
  - name: ansible.posix
    version: ">=1.5.0"
  - name: community.postgresql
    version: ">=3.0.0"
  - name: oracle.oci
$ ansible-galaxy collection install -r collections/requirements.yml

Ansible Vault: keeping secrets safe

Playbooks need passwords: for the SYS account, application users, and backup encryption. They must never be stored as plain text in Git. Ansible Vault encrypts them:

# create an encrypted variables file, and edit it later
$ ansible-vault create group_vars/prod/vault.yml
$ ansible-vault edit group_vars/prod/vault.yml

# run a playbook that uses it, giving the vault password
$ ansible-playbook provision_oracle.yml --ask-vault-pass

The file is stored encrypted in Git, so only people and pipelines with the vault password can read it. A common convention keeps secret variables, prefixed vault_, separate from ordinary ones:

# group_vars/prod/vars.yml: readable, refers to the secret by name
oracle_sys_password: "{{ vault_oracle_sys_password }}"

# group_vars/prod/vault.yml: encrypted with ansible-vault
vault_oracle_sys_password: "..."

Many companies go a step further and keep secrets in a central vault, such as HashiCorp Vault, CyberArk, or a cloud secret manager, which Ansible reads at run time through lookup plugins. Mark tasks that handle secrets with no_log: true, so passwords never appear in output or logs.

A typical project layout

db-automation/
├── ansible.cfg
├── inventory/
│   ├── prod/hosts.yml  and  prod/group_vars/
│   └── test/hosts.yml  and  test/group_vars/
├── collections/requirements.yml
├── roles/
│   ├── os_baseline/
│   ├── oracle_install/
│   └── oracle_db/
├── playbooks/
│   ├── provision_oracle.yml
│   ├── patch_oracle.yml
│   └── health_check.yml
└── README.md

Keeping a separate inventory per environment, as with Terraform's separate folders, makes it much harder to run a production change by accident.

Key takeaways

  • Roles package tasks, defaults, templates, and handlers in a standard layout; playbooks combine roles.
  • Collections distribute modules and roles; pin them in requirements.yml.
  • Useful collections include ansible.posix, community.postgresql, microsoft.sql, oracle.oci, and community Oracle collections.
  • Ansible Vault encrypts secrets in Git; central vaults and no_log: true add further protection.
  • Separate inventories per environment reduce the risk of running against production by mistake.