← Back to Blog

πŸ› οΈ Replace Method for YAML in OpenShift

πŸ› οΈ Replace Method for YAML in OpenShift Working with OpenShift involves managing many YAML configuration files. Each deployment often requires tweaking specific values, which can be time-consuming and error-prone if done manually.

πŸ› οΈ Replace Method for YAML in OpenShift

Working with OpenShift involves managing many YAML configuration files. Each deployment often requires tweaking specific values, which can be time-consuming and error-prone if done manually. This post will guide you through automating YAML value replacements to simplify and speed up the deployment process in OpenShift.


πŸ’‘ Why Automate YAML Replacement?

In OpenShift, YAML files control the configuration of pods, services, routes, and more. When managing multiple environments (e.g., development, staging, and production), certain values, such as URLs or resource limits, need updating. Automating these changes can:

  • 🎯 Reduce human error.

  • ⏱️ Save time.

  • πŸ€– Enhance consistency across deployments.


πŸ–₯️ How to Automate Value Replacement in YAML Files

Here’s a simple approach to streamline value replacement within YAML files using a Bash script. This example focuses on modifying an environment variable within a DeploymentConfig YAML.

🎯 Step 1: Define the Variables You Want to Replace

Identify the values you want to replace. In this example, let’s assume:

  • APP_ENV: Environment variable for different stages (e.g., dev, staging, prod).

  • IMAGE: Container image URL.

🎯 Step 2: Create a Bash Script

Use yq or sed to automate replacements in your YAML files. yq is especially suited for YAML as it parses and edits YAML syntax directly. Install yq by running:

brew install yq # MacOS
sudo snap install yq # Ubuntu

🎯 Step 3: Write the Script

Here’s an example Bash script that changes the APP_ENV and IMAGE values:

!/bin/bash

Set variables

APP_ENV="prod"
IMAGE="your-image-url:latest"
YAML_FILE="deployment.yaml"

Update APP_ENV and IMAGE in YAML file

yq eval ".spec.template.spec.containers[0].env[0].value = \"$APP_ENV\"" -i $YAML_FILE
yq eval ".spec.template.spec.containers[0].image = \"$IMAGE\"" -i $YAML_FILE

echo "Updated $YAML_FILE with APP_ENV=$APP_ENV and IMAGE=$IMAGE."

Save this script as update_yaml.sh, make it executable, and run it:

chmod +x update_yaml.sh
./update_yaml.sh

This script updates APP_ENV and IMAGE values in your YAML file automatically. You can modify it to suit other configurations as needed.


πŸš€ OpenShift Deployment

Once the YAML is updated, apply it in OpenShift using:

oc apply -f deployment.yaml

This approach ensures that you deploy consistent and correct configurations every time, making your deployment process faster and less error-prone.


πŸ› οΈ Simplify Your Workflow

Integrate this script into your CI/CD pipeline for automated environment updates whenever you trigger a deployment.


πŸ”— Connect with me

Let’s connect if you’re interested in discussing OpenShift or DevOps!


Imported from rifaterdemsahin.com Β· 2025