You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Shell脚本替换ConfigMap.yaml指定Key值并自动化部署

Absolutely Feasible! Here's How to Build Your Automated Script

First, let's tackle that YAML parsing error you're stuck on (error: error parsing yamlfiles/configMap.yaml: error converting YAML to JSON: yaml: line 22: did not find the expected key): this almost always comes from invalid YAML syntax—like missing spaces after colons, inconsistent indentation, or malformed key-value pairs. When building our script, we'll make sure to preserve valid YAML structure throughout the replacement process.

Step 1: Prepare Your ConfigMap Template

Start by modifying your configMap.yaml to use placeholder values for the keys you want to update. Use a consistent, easy-to-spot pattern (e.g., __KEY_NAME__) to avoid accidentally overwriting other content. Example snippet:

apiVersion: v1
kind: ConfigMap
metadata:
  name: your-app-config
data:
  SECRET_KEY: __SECRET_KEY__
  JWT_KEY: __JWT_KEY__
  SFTP_PWD: __SFTP_PWD__
  # Keep your other existing keys here...

Double-check indentation here—YAML is strict about this! Make sure all entries under data: are indented consistently (usually 2 spaces).

Step 2: Write the Shell Script

Create a script (e.g., deploy-configmap.sh) that handles user input, replaces placeholders, validates the YAML, and deploys the ConfigMap. Here's a robust, ready-to-use example:

#!/bin/bash

# Define file paths
CONFIG_TEMPLATE="yamlfiles/configMap.yaml"
TEMP_CONFIG="yamlfiles/configMap.tmp.yaml"

# Prompt user for values with basic validation
read -p "Enter SECRET_KEY: " SECRET_KEY
if [[ -z "$SECRET_KEY" ]]; then
  echo "Error: SECRET_KEY can't be empty!"
  exit 1
fi

read -p "Enter JWT_KEY: " JWT_KEY
if [[ -z "$JWT_KEY" ]]; then
  echo "Error: JWT_KEY can't be empty!"
  exit 1
fi

read -p "Enter SFTP_PWD: " SFTP_PWD
if [[ -z "$SFTP_PWD" ]]; then
  echo "Error: SFTP_PWD can't be empty!"
  exit 1
fi

# Copy template to temp file (so we don't modify the original)
cp "$CONFIG_TEMPLATE" "$TEMP_CONFIG"

# Replace placeholders (using # as sed delimiter to handle values with /)
sed -i "s#__SECRET_KEY__#$SECRET_KEY#g" "$TEMP_CONFIG"
sed -i "s#__JWT_KEY__#$JWT_KEY#g" "$TEMP_CONFIG"
sed -i "s#__SFTP_PWD__#$SFTP_PWD#g" "$TEMP_CONFIG"

# Validate the modified YAML (optional but highly recommended)
echo "Checking YAML syntax..."
if ! kubectl apply --dry-run=client -f "$TEMP_CONFIG"; then
  echo "Error: Invalid YAML after replacement. Check your input for special characters like unescaped quotes."
  rm "$TEMP_CONFIG"
  exit 1
fi

# Deploy the ConfigMap
echo "Deploying ConfigMap..."
kubectl apply -f "$TEMP_CONFIG"

# Clean up temporary file
rm "$TEMP_CONFIG"

echo "ConfigMap deployed successfully!"

Key Tips to Avoid Issues:

  • Special Character Handling: If your input values include characters like /, &, or quotes, use sed with a non-standard delimiter (we used # here) and wrap values in double quotes in the YAML if needed.
  • Dry Run Validation: The kubectl apply --dry-run=client step catches YAML errors before deployment, saving you from troubleshooting failed deployments.
  • Template Preservation: Using a temporary file keeps your original configMap.yaml intact as a reusable template for future deployments.

Fixing the Original YAML Error

Before running the script, double-check line 22 of your original configMap.yaml. Common culprits here are:

  • A colon without a space (e.g., SECRET_KEY:mykey instead of SECRET_KEY: mykey)
  • Misaligned indentation (e.g., the key isn't lined up with other entries under data:)
  • A missing value for a key (YAML requires values for all key-value pairs in the data section)

内容的提问来源于stack exchange,提问作者user11270902

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:54:10