-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgenerate-ansible-inventory.sh
More file actions
executable file
·126 lines (109 loc) · 4.33 KB
/
Copy pathgenerate-ansible-inventory.sh
File metadata and controls
executable file
·126 lines (109 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# Generate Ansible inventory from validator-config.yaml
# This script reads validator-config.yaml and generates hosts.yml for Ansible
set -e
if [ $# -lt 2 ]; then
echo "Usage: $0 <validator-config.yaml> <output-hosts.yml>"
exit 1
fi
VALIDATOR_CONFIG="$1"
OUTPUT_FILE="$2"
# Check if yq is installed
if ! command -v yq &> /dev/null; then
echo "Error: yq is required but not installed. Please install yq first."
echo "On macOS: brew install yq"
echo "On Linux: https://github.com/mikefarah/yq#install"
exit 1
fi
# Check if validator config exists
if [ ! -f "$VALIDATOR_CONFIG" ]; then
echo "Error: Validator config file not found: $VALIDATOR_CONFIG"
exit 1
fi
# Create output directory if it doesn't exist
OUTPUT_DIR=$(dirname "$OUTPUT_FILE")
mkdir -p "$OUTPUT_DIR"
# Start generating the inventory file
cat > "$OUTPUT_FILE" << 'EOF'
---
# Ansible Inventory for Lean Quickstart
# Auto-generated from validator-config.yaml
# DO NOT EDIT MANUALLY - This file is auto-generated
all:
children:
local:
hosts:
localhost:
ansible_connection: local
ansible_python_interpreter: auto_silent
bootnodes:
hosts: {}
zeam_nodes:
hosts: {}
ream_nodes:
hosts: {}
qlean_nodes:
hosts: {}
lantern_nodes:
hosts: {}
lighthouse_nodes:
hosts: {}
grandine_nodes:
hosts: {}
ethlambda_nodes:
hosts: {}
EOF
# Extract node information from validator-config.yaml
nodes=($(yq eval '.validators[].name' "$VALIDATOR_CONFIG"))
# Process each node and generate inventory entries
for node_name in "${nodes[@]}"; do
# Extract client type (zeam, ream, qlean, lantern, lighthouse, grandine, ethlambda)
IFS='_' read -r -a elements <<< "$node_name"
client_type="${elements[0]}"
group_name="${client_type}_nodes"
# Extract node-specific information
node_ip=$(yq eval ".validators[] | select(.name == \"$node_name\") | .enrFields.ip // \"127.0.0.1\"" "$VALIDATOR_CONFIG")
node_quic=$(yq eval ".validators[] | select(.name == \"$node_name\") | .enrFields.quic // \"9000\"" "$VALIDATOR_CONFIG")
# Check if this is a remote deployment (IP is not localhost/127.0.0.1)
is_remote=false
if [[ "$node_ip" != "127.0.0.1" ]] && [[ "$node_ip" != "localhost" ]]; then
is_remote=true
fi
# Add node to the appropriate group
if [ "$is_remote" = true ]; then
# Remote deployment
yq eval -i ".all.children.$group_name.hosts.$node_name.ansible_host = \"$node_ip\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.node_name = \"$node_name\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.client_type = \"$client_type\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.quic_port = $node_quic" "$OUTPUT_FILE"
else
# Local deployment
yq eval -i ".all.children.$group_name.hosts.$node_name.ansible_host = \"localhost\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.ansible_connection = \"local\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.node_name = \"$node_name\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.client_type = \"$client_type\"" "$OUTPUT_FILE"
yq eval -i ".all.children.$group_name.hosts.$node_name.quic_port = $node_quic" "$OUTPUT_FILE"
fi
done
# One inventory host per remote IP for prepare.yml — avoids N parallel SSH/apt sessions
# to the same machine when validator-config lists zeam_0..zeam_4 on one IP.
PREPARE_FILE="${OUTPUT_DIR}/hosts-prepare.yml"
cat > "$PREPARE_FILE" << 'EOF'
---
# Deduplicated inventory for prepare.yml only (generated; do not edit manually).
all:
children:
prepare_hosts:
hosts: {}
EOF
while IFS= read -r ip; do
[ -z "$ip" ] || [ "$ip" = "null" ] && continue
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "localhost" ]]; then
continue
fi
inv_id="prep_${ip//./_}"
yq eval -i ".all.children.prepare_hosts.hosts.\"$inv_id\".ansible_host = \"$ip\"" "$PREPARE_FILE"
done < <(yq eval '.validators[].enrFields.ip' "$VALIDATOR_CONFIG" | sort -u)
echo "✅ Generated Ansible inventory at: $OUTPUT_FILE"
echo "✅ Generated prepare inventory (one host per IP) at: $PREPARE_FILE"
echo " Processed ${#nodes[@]} node(s): ${nodes[*]}"