Automation in DevOps refers to scripting repetitive tasks like provisioning, configuration, deployment, and monitoring to improve efficiency and reduce errors.
- Reduces manual effort
- Increases consistency and repeatability
- Improves efficiency and speed
- Reduces errors and enhances security
Bash scripting is writing command-line instructions in a script file (.sh) to automate tasks in Unix/Linux environments.
#!/bin/bash
echo "Hello, DevOps!"Save the file (script.sh), make it executable (chmod +x script.sh), and run it (./script.sh).
Bash is a type of shell, but shell scripting can also be done in other shells like sh, csh, and zsh. Bash provides more advanced scripting features.
Variables store values and are defined without a $ sign but accessed using $.
name="DevOps"
echo "Hello, $name"- Infrastructure as Code (IaC)
- CI/CD automation
- Log analysis
- Cloud automation (AWS, Azure, GCP SDKs)
def greet():
print("Hello, DevOps!")
greet()YAML (Yet Another Markup Language) is a human-readable format used for Kubernetes configurations, Ansible playbooks, CI/CD pipelines, etc.
version: '3'
services:
web:
image: nginx
ports:
- "80:80"println "Hello, DevOps!"Groovy is used in Jenkins pipelines and automation tasks.
The shebang (#!/bin/bash or #!/usr/bin/python3) specifies the interpreter for executing the script.
Bash supports for, while, and until loops. Example:
for i in {1..5}; do echo "Iteration $i"; doneif-else statements execute different code based on conditions.
if [ $USER == "root" ]; then echo "Admin access"; else echo "User access"; fiecho "Enter name: "
read name
echo "Hello, $name"python3 -m venv myenv
source myenv/bin/activateimport json
data = '{"name": "DevOps"}'
parsed = json.loads(data)
print(parsed["name"])awk is used for text processing. Example:
awk '{print $1}' file.txtExtracts the first column from file.txt.
Use # for comments.
# This is a comment
name: DevOpsdef name = "DevOps"
println name#!/bin/bash
echo "Hello, $1!"Run: ./script.sh DevOps → Output: Hello, DevOps!
Use set -e to stop execution on errors.
A YAML file that defines automation tasks for servers.
try:
print(1 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")Edit crontab -e and add:
0 5 * * * /path/to/script.shRuns the script daily at 5 AM.
Use script {} inside a Jenkins pipeline.
mylist = [1, 2, 3]
print(mylist[0])Used for text replacement. Example:
sed -i 's/old/new/g' file.txtmydict = {"name": "DevOps"}
print(mydict["name"])Use yamllint or kubectl apply -f --dry-run=client.
pip install requestspipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building..."
}
}
}
}for key, value in mydict.items():
print(key, value)export VAR="DevOps"A reusable way to organize Ansible tasks, handlers, and templates.
message: |
Line 1
Line 2declare -A myarray
myarray[name]="DevOps"
echo ${myarray[name]}import os
os.system("ls")Used to parse JSON. Example:
cat data.json | jq '.name'exit 1Use set -x for debugging:
#!/bin/bash
set -x
echo "Debugging mode enabled"trap "echo 'Script interrupted'; exit" SIGINT SIGTERMCatches Ctrl+C (SIGINT) and terminates gracefully.
Both execute commands, but $(command) is preferred as it's nestable.
Use \ for line continuation:
echo "This is a \
multiline command"With Jinja2 templating in Ansible:
tasks:
- name: Install package
yum:
name: httpd
when: ansible_os_family == "RedHat"Use Ansible Vault:
ansible-vault encrypt secrets.yamlpython3 <<EOF
print("Hello from Python")
EOFbreakexits the loop entirely.continueskips the current iteration.
Use jq:
cat data.json | jq '.key'def arr = ["a", "b", "c"]
println arr[0]A reusable pipeline function stored in Git.
timeout 10s ./script.sh(my_function)Runs in a new shell, not affecting the parent script.
import requests
response = requests.get("https://example.com")
print(response.text)import requests
requests.get("https://example.com", auth=("user", "pass"))Use yq:
yq e '.name="NewValue"' -i config.yamlpipeline {
agent any
stages {
stage('Build') { steps { echo "Building..." } }
stage('Test') { steps { echo "Testing..." } }
}
}Running a script directly inside a pipeline (e.g., GitHub Actions, Jenkins).
Example in GitHub Actions:
jobs:
build:
steps:
- run: echo "Inline script execution"ssh user@server 'bash -s' < local_script.shAnsible modules ensure idempotency by only making changes when needed.
💡 Want to contribute?
We welcome contributions! If you have insights, new tools, or improvements, feel free to submit a pull request.
📌 How to Contribute?
- Read the CONTRIBUTING.md guide.
- Fix errors, add missing topics, or suggest improvements.
- Submit a pull request with your updates.
📢 Stay Updated:
⭐ Star the repository to get notified about new updates and additions.
💬 Join discussions in GitHub Issues to suggest improvements.
🔗 GitHub: @NotHarshhaa
📝 Blog: ProDevOpsGuy
💬 Telegram Community: Join Here
