Last active
January 24, 2025 01:00
-
-
Save dot-mike/51f03b32b7276009168f62beea800c24 to your computer and use it in GitHub Desktop.
External group definition script for ClusterShell. Reads group definitions from a YAML file and outputs hostnames. This is created so we can store additional metadata with the hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# External group definition script for ClusterShell | |
# Reads group definitions from a YAML file and outputs hostnames | |
# Default YAML file path | |
YAML_FILE="/etc/clustershell/groups.yaml" | |
# Parse command-line options | |
while getopts "f:" opt; do | |
case $opt in | |
f) | |
YAML_FILE=$OPTARG | |
;; | |
*) | |
print_usage | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
function print_usage() { | |
echo "Usage: $0 [-f yaml_file] {map|list} <source> <group>" | |
} | |
case $1 in | |
map) | |
# Ensure source and group are provided | |
if [[ -z $2 || -z $3 ]]; then | |
echo "Error: Source and group must be specified" | |
print_usage | |
exit 1 | |
fi | |
# Output resolved hosts for the specified source and group, including anchor resolution | |
yq -o=json ".${2}.${3}.hosts" "$YAML_FILE" | jq -r 'flatten | .[]' | |
;; | |
list) | |
# If no specific source is provided, list all top-level keys | |
if [[ -z $2 ]]; then | |
yq "keys[]" "$YAML_FILE" | |
else | |
# List all group names in the specified source | |
yq ".${2} | keys[]" "$YAML_FILE" | |
fi | |
;; | |
reverse) | |
# Ensure source and node are provided | |
if [[ -z $2 || -z $3 ]]; then | |
echo "Error: Source and node must be specified" | |
print_usage | |
exit 1 | |
fi | |
# Find groups containing the specified node | |
yq -o=json ".${2}" "$YAML_FILE" | jq -r --arg NODE "$3" \ | |
'to_entries | map(select(.value.hosts | flatten | index($NODE) != null)) | .[].key' | |
;; | |
*) | |
print_usage | |
exit 1 | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ClusterShell node groups main configuration file | |
[Main] | |
# Default group source | |
default: roles | |
confdir: /etc/clustershell/groups.conf.d $CFGDIR/groups.conf.d | |
autodir: /etc/clustershell/groups.d $CFGDIR/groups.d | |
[roles] | |
map: /usr/local/bin/get_nodes_from_yaml.sh -f /etc/clustershell/groups.yaml map $SOURCE $GROUP | |
all: /usr/local/bin/get_nodes_from_yaml.sh -f /etc/clustershell/groups.yaml map $SOURCE all | |
list: /usr/local/bin/get_nodes_from_yaml.sh -f /etc/clustershell/groups.yaml list $SOURCE | |
reverse: /usr/local/bin/get_nodes_from_yaml.sh -f /etc/clustershell/groups.yaml reverse $SOURCE $NODE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
roles: | |
adm: | |
hosts: &adm_hosts | |
- example0 | |
custom_group_value: 123 | |
compute: | |
hosts: &compute_hosts | |
- example1 | |
- example2 | |
custom_group_value: 456 | |
all: | |
hosts: | |
- *adm_hosts | |
- *compute_hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment