bk-id-check.yaml
Overview
The bk-id-check.yaml file is an automation playbook designed to validate configuration integrity for a group of hosts identified as "block keepers" (BKs). Its primary function is to ensure that each block keeper host defines a unique and non-empty identifier, NODE_ID. This validation is critical in distributed systems or clusters where unique identifiers prevent ambiguity and conflicts during operations.
Two main validation steps are performed:
Presence Check: Confirms that every block keeper has a
NODE_IDdefined and that it is not empty.Uniqueness Check: Ensures that all
NODE_IDvalues across block keepers are unique.
If either check fails, the playbook will abort execution with a descriptive error message.
Playbook Tasks
The playbook consists of two sequential tasks executed against the inventory group block_keepers.
Task 1: Verify NODE_ID presence for each BK
Purpose: To ensure that every host in the
block_keepersgroup has aNODE_IDvariable defined and that it is neithernullnor an empty string.Mechanism:
Extracts the
NODE_IDfrom each host's variables.Checks for any
NODE_IDthat isnone(undefined).Converts all
NODE_IDvalues to strings, trims whitespace, and checks for empty strings.If any undefined or empty
NODE_IDis found, the playbook fails with a message.
Failure Message:
"Each block keeper must define a non-empty NODE_ID (number or string)"
Example usage snippet:
- name: Verify NODE_ID presence for each BK
fail:
msg: "Each block keeper must define a non-empty NODE_ID (number or string)"
when: >
(
groups['block_keepers']
| map('extract', hostvars, 'NODE_ID')
| select('equalto', none)
| list
| length
) > 0
or
(
groups['block_keepers']
| map('extract', hostvars, 'NODE_ID')
| map('string')
| map('trim')
| select('equalto', '')
| list
| length
) > 0
Task 2: Verify NODE_ID uniqueness for each BK
Purpose: To ensure that all
NODE_IDs assigned to block keepers are unique, preventing conflicts.Mechanism:
Extracts all
NODE_IDs from theblock_keepers.Converts them to strings and trims whitespace.
Uses the
uniquefilter to remove duplicates.Compares the count of unique
NODE_IDs with the total number of block keepers.If counts differ, it indicates duplicate
NODE_IDs, and the playbook fails.
Failure Message:
"Duplicate NODE_ID found. Each block keeper must have a unique NODE_ID"
Example usage snippet:
- name: Verify NODE_ID uniqueness for each BK
fail:
msg: "Duplicate NODE_ID found. Each block keeper must have a unique NODE_ID"
when: >
(
groups['block_keepers']
| map('extract', hostvars, 'NODE_ID')
| map('string')
| map('trim')
| unique
| list
| length
)
!=
(groups['block_keepers'] | length)
Implementation Details
Inventory Group Dependency: The playbook operates exclusively on hosts in the
block_keepersgroup, leveraging Ansible's inventory grouping.Variable Extraction: Uses the
map('extract', hostvars, 'NODE_ID')filter to gather theNODE_IDvariable from each host's variables.Data Normalization: Applies
map('string')andmap('trim')to ensure uniform string comparison, handling cases whereNODE_IDmight be numeric or contain extraneous whitespace.Conditional Failure: Utilizes Ansible's
failmodule with conditionalwhenclauses to abort execution if validation fails.Logical Expressions: Combines multiple filters and expressions to precisely check presence and uniqueness conditions.
Interaction with Other System Components
Host Inventory: Relies on the inventory configuration where block keeper hosts are grouped under
block_keepers.Host Variables: Depends on the presence of the
NODE_IDvariable in host variables (hostvars), which must be assigned prior to running this playbook.Downstream Tasks/Playbooks: This playbook is typically run as a prerequisite validation step before tasks that require uniquely identified block keepers, such as cluster configuration, deployment orchestration, or network topology setup.
Error Handling Workflow: By failing early on misconfiguration, it prevents execution of dependent tasks that could cause inconsistent states or runtime errors.
Visual Diagram
flowchart TD
A[Start: block_keepers group]
A --> B[Extract NODE_IDs from hostvars]
B --> C{Check NODE_ID presence}
C -->|Any missing or empty| D[Fail: "Each block keeper must define a non-empty NODE_ID"]
C -->|All present| E{Check NODE_ID uniqueness}
E -->|Duplicates found| F[Fail: "Duplicate NODE_ID found"]
E -->|All unique| G[Validation Passed]
This file focuses exclusively on validating NODE_ID configuration for block keeper hosts, ensuring reliable identification within the system. For details on host grouping, variable management, and failure handling mechanisms, see the topics on Ansible Inventory Management and Error Handling in Automation.