Skip to content

Centralize database configuration into a single reusable list#550

Merged
ehelms merged 2 commits into
theforeman:masterfrom
ehelms:centralize-database-configuration
Jun 11, 2026
Merged

Centralize database configuration into a single reusable list#550
ehelms merged 2 commits into
theforeman:masterfrom
ehelms:centralize-database-configuration

Conversation

@ehelms

@ehelms ehelms commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary

Database configuration was duplicated across multiple consumers — each playbook and role that needed the list of databases would manually enumerate them with conditional logic for features like IOP. Adding a new database service meant editing every consumer.

This PR:

  • Defines a canonical databases list in database.yml where each entry is tagged with a feature (e.g. foreman, katello, iop)
  • Adds a computed all_databases variable that filters databases by enabled_features
  • Adds to_postgresql_databases and to_postgresql_users custom Jinja2 filters that derive the postgresql role's input lists from all_databases
  • Merges database_iop.yml into database.yml — one file for all database configuration
  • Removes the pre_tasks merge block from deploy.yaml (no longer needed)
  • Refactors check_database_connection to loop over checks_databases (a namespaced variable passed from the checks role) instead of a hardcoded inline list

Motivation

Every new command that needs database awareness (backup, restore, upgrade, health-check) was forced to repeat the same conditional enumeration pattern: foreman always, candlepin+pulp if katello, 5 IOP databases if iop. This was already duplicated in deploy.yaml, checks.yaml, and check_database_connection, and would only grow worse.

Design

database.yml
  ├── per-service variables (foreman_database_name, etc.)
  ├── databases[]          — canonical list, each entry tagged with feature
  ├── all_databases        — databases filtered by enabled_features
  ├── postgresql_databases — derived via to_postgresql_databases filter
  └── postgresql_users     — derived via to_postgresql_users filter

The databases list captures the full connection info for each database (name, host, port, user, password, ssl_mode, ssl_ca). The feature field maps to enabled_features so the list self-filters. Custom filters project this into the {name, owner} and {name, password} shapes that the postgresql role expects.

Changes

File Change
src/vars/database.yml Merged IOP vars, added databases/all_databases, replaced static postgresql_databases/postgresql_users with filter-derived computed vars
src/vars/database_iop.yml Deleted (merged into database.yml)
src/filter_plugins/foremanctl.py Added to_postgresql_databases and to_postgresql_users filters
src/playbooks/deploy/deploy.yaml Removed IOP merge pre_tasks, pass checks_databases to checks role
src/playbooks/checks/checks.yaml Added flavors vars_file, pass checks_databases to checks role
src/roles/check_database_connection/tasks/main.yaml Replaced hardcoded 3-item loop with checks_databases
src/roles/check_database_connection/tasks/check.yaml Aligned field names (dbnamedatabase, sslmodessl_mode, ca_certssl_ca)
src/roles/checks/defaults/main.yml New — declares checks_databases: [] default
development/playbooks/deploy-dev/deploy-dev.yaml Removed include_vars for deleted database_iop.yml

Test plan

  • cd src; ansible-lint passes
  • cd development; ansible-lint passes
  • Deploy with --tuning development and verify postgresql databases and users are created correctly
  • Deploy with IOP enabled and verify all 8 databases are created
  • Run foremanctl checks with database_mode: external and verify all databases are checked
  • Verify dev deploy (forge deploy-dev) still works with its test DB and SUPERUSER overrides

Define a canonical `databases` list in database.yml with feature tags,
replacing scattered conditional enumeration across consumers. A computed
`all_databases` filters by enabled_features, and custom Jinja2 filters
derive postgresql_databases/postgresql_users from it — eliminating static
list duplication and the merge pre_tasks in deploy.yaml.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ehelms

ehelms commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

@sjha4 Based on our discussions from the backup PR, I extracted the idea into a dedicated change that could serve as a basis for the backup.

@evgeni I'd appreciate your thoughts on this implementation change.

Comment thread src/vars/database.yml
@ehelms ehelms force-pushed the centralize-database-configuration branch from 639ba13 to 3441151 Compare June 10, 2026 14:24
@ehelms ehelms marked this pull request as ready for review June 11, 2026 00:25
Comment thread src/vars/database.yml
ssl_ca: "{{ foreman_database_ssl_ca }}"
feature: foreman
- name: candlepin
database: "{{ candlepin_database_name }}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker but more of a thought: We are gating on features, which means if the katello feature is there then only we will have pulp and candlepin databases, if its missing we don't have those, which is fine by today as we only have katello flavor, but in case of foreman-proxy-content flavor, we only will need pulp db not candlepin and then this becomes a issue,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's being filtered later in:

all_databases: >-
  {{ databases | selectattr('feature', 'in', enabled_features) | list }}

@arvind4501 arvind4501 Jun 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what i meant was enabled_features never know what pulp and candlepin is, only katello. thus no gating on pulp and candlepin

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we have no pulp or candlepin feature (yet), which I think is also something we need/want to tackle for the tests too. Once we introduce those features, we can adjust here.

Comment thread src/filter_plugins/foremanctl.py Outdated

@sjha4 sjha4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested foremanctl check to make sure all DBs are made available to it and it works as advertised..Deploy has worked well with the change deploying all IoP DBs correctly.

Do we want a developer doc change to go with this for devs/agents to follow?

@ehelms ehelms force-pushed the centralize-database-configuration branch from 3441151 to 8c271df Compare June 11, 2026 15:39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to refactor this similarly?

@ehelms

ehelms commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Do we want a developer doc change to go with this for devs/agents to follow?

I do not think so, I believe this is discoverable as a internal mechanism.

The development playbooks load src/vars/database.yml which now uses
custom filters (to_postgresql_databases, to_postgresql_users). Without
this path, playbooks run from the development directory fail with
"No filter named" errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ehelms ehelms force-pushed the centralize-database-configuration branch from 8c271df to 8e4ce37 Compare June 11, 2026 17:29
@ehelms

ehelms commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Ignoring EL10 test results as there is an issue with the VM image affecting all tests right now.

@ehelms ehelms merged commit c9eb27e into theforeman:master Jun 11, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants