Automation scripting

Intermediate Level

Mid-level automation and scripting interview questions covering advanced scripting concepts, error handling, and automation tools.

Intermediate Level

This section covers more advanced concepts in automation and scripting, including error handling, scheduling, and integration with various DevOps tools.

How do you pass arguments to a Bash script?

#!/bin/bash
echo "Hello, $1!"

Run: ./script.sh DevOps → Output: Hello, DevOps!

How do you handle errors in Bash scripts?

Use set -e to stop execution on errors.

What is an Ansible playbook?

A YAML file that defines automation tasks for servers.

How do you handle exceptions in Python?

try:
    print(1 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero")

How do you schedule a script with Cron?

Edit crontab -e and add:

0 5 * * * /path/to/script.sh

Runs the script daily at 5 AM.

How do you execute a Groovy script in Jenkins?

Use script {} inside a Jenkins pipeline.

How do you create a list in Python?

mylist = [1, 2, 3]
print(mylist[0])

What is sed in Bash?

Used for text replacement. Example:

sed -i 's/old/new/g' file.txt

How do you define a dictionary in Python?

mydict = {"name": "DevOps"}
print(mydict["name"])

How do you validate a YAML file?

Use yamllint or kubectl apply -f --dry-run=client.

How do you install Python modules?

pip install requests

What is Jenkins pipeline syntax for automation?

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo "Building..."
            }
        }
    }
}

How do you iterate over a dictionary in Python?

for key, value in mydict.items():
    print(key, value)

How do you set environment variables in Bash?

export VAR="DevOps"

What is an Ansible role?

A reusable way to organize Ansible tasks, handlers, and templates.

What is a multiline string in YAML?

message: |
  Line 1
  Line 2

What is an associative array in Bash?

declare -A myarray
myarray[name]="DevOps"
echo ${myarray[name]}

How do you run a shell command in Python?

import os
os.system("ls")

What is jq in Linux?

Used to parse JSON. Example:

cat data.json | jq '.name'

How do you exit a script with a status code?

exit 1

On this page