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?

Positional arguments arrive as $1, $2, ... with $0 being the script name:

#!/bin/bash
if [[ $# -lt 1 ]]; then
  echo "Usage: $0 <environment>"; exit 1
fi
echo "Deploying to $1"
./deploy.sh staging    # -> Deploying to staging

Also know: $# is the argument count, "$@" is all arguments (quoted, preserves spaces), and shift drops $1 and moves the rest down. For anything beyond simple flags, mention getopts as the standard way to parse options like -e prod.

How do you handle errors in Bash scripts?

A production-grade Bash script starts with strict mode:

#!/bin/bash
set -euo pipefail
  • -e — exit immediately if any command fails (non-zero exit code)
  • -u — error on unset variables (catches typos like $ENVIROMENT)
  • -o pipefail — a pipeline fails if any command in it fails, not just the last one

For cleanup and reporting, pair strict mode with a trap:

trap 'echo "Failed at line $LINENO"; rm -f /tmp/lockfile' ERR

Also show judgment: set -e doesn't fire inside if conditions or after ||, so commands whose failure you expect (like grep finding nothing) need explicit handling.

What is an Ansible playbook?

A playbook is a YAML file that defines automation as an ordered list of plays, each mapping a group of hosts (from an inventory) to tasks:

- hosts: webservers
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

Each task calls a module (like apt or copy) — and modules are idempotent, so running the playbook twice leaves the same end state without side effects. Playbooks are how configuration management, patching, and multi-tier deployments are expressed in Ansible; the inventory, variables, and roles keep them reusable across environments.

How do you handle exceptions in Python?

try:
    config = open("app.conf")
except FileNotFoundError as err:
    print(f"Config missing: {err}")
    raise    # re-raise after logging if the script cannot continue
finally:
    print("Cleanup runs no matter what")

Best practices interviewers expect:

  • Catch specific exceptions (FileNotFoundError, KeyError) — a bare except: hides real bugs like typos and KeyboardInterrupt
  • Use finally (or a with context manager) for guaranteed cleanup
  • Let a script exit non-zero on unhandled failure so CI/CD pipelines detect the failure

How do you schedule a script with Cron?

Edit the crontab with crontab -e and add a line with five time fields:

0 5 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1

Fields are: minute hour day-of-month month day-of-week — so this runs daily at 05:00. Useful examples: */15 * * * * (every 15 minutes), 0 4 * * 1 (Mondays at 04:00).

Two gotchas worth mentioning: cron runs with a minimal environment (always use absolute paths for binaries and scripts), and redirecting output (>> file 2>&1) is the only way to capture failures, since nothing is watching cron's stdout.

How do you execute Groovy logic in a Jenkins pipeline?

In a declarative pipeline, Groovy goes inside a script { } block:

pipeline {
    agent any
    stages {
        stage('Decide') {
            steps {
                script {
                    if (params.ENVIRONMENT == 'prod') {
                        echo "Extra approval needed"
                    }
                }
            }
        }
    }
}

Outside pipelines, Groovy runs from Manage Jenkins → Script Console for admin automation. Best practice to mention: keep script {} blocks small — declarative pipeline steps are safer, resumable, and readable; heavy Groovy belongs in a shared library.

How do you create and manipulate lists in Python?

tools = ["docker", "k8s", "terraform"]
tools.append("ansible")        # add one item
tools.remove("k8s")            # remove by value
print(tools[0])                # docker  (indexing)
print(tools[-1])               # ansible (last item)

prod = [t for t in tools if t != "ansible"]   # list comprehension

Lists are ordered and mutable — the everyday container in automation scripts (hostnames, file paths, service names). List comprehensions are worth mentioning: they're the idiomatic Python way to filter/transform a list in one line.

What is sed in Bash?

sed is a stream editor for parsing and transforming text line by line — most famously for substitution:

sed 's/old/new/g' file.txt            # print with replacements (file unchanged)
sed -i 's/old/new/g' file.txt         # edit the file in place
sed -i 's|/old/path|/new/path|g' f.txt  # | delimiter avoids escaping /
sed -n '10,20p' file.txt              # print only lines 10-20

The s command means substitute, g means every occurrence on the line (without it, only the first). In DevOps it's the quick fix for config changes in pipelines — though for structured formats (YAML/JSON), tools like yq/jq are safer than text substitution.

How do you define a dictionary in Python?

server = {"name": "web-01", "region": "us-east-1"}
print(server["name"])               # web-01
print(server.get("owner", "n/a"))   # safe access with default

server["size"] = "t3.medium"        # add/update a key
for key, value in server.items():   # iterate pairs
    print(f"{key}={value}")

Dictionaries are key-value maps with O(1) lookup by key — the natural fit for configuration, API payloads, and tagging metadata. Prefer .get() over [] when a key may be missing, since [] raises KeyError.

How do you validate a YAML file?

Options depend on context — mentioning more than one scores well:

# dedicated linter: catches syntax AND style issues
yamllint playbook.yml

# Python round-trip: fails loudly on invalid syntax
python3 -c "import yaml, sys; yaml.safe_load(open(sys.argv[1]))" file.yaml

# Kubernetes-specific: validates against the API schema too
kubectl apply -f manifest.yaml --dry-run=client

Key distinction: syntax checks (yamllint, safe_load) only prove the YAML parses; kubectl --dry-run additionally validates fields against the Kubernetes API schema. In CI, run validation as a pre-commit or pipeline step before anything gets deployed.

How do you install Python modules?

pip install requests            # latest version
pip install requests==2.31.0    # pinned version
pip install -r requirements.txt # install everything, pinned

Always install inside a virtual environment (or container) — global installs cause version conflicts between projects and pollute the host. In pipelines, generate the lockfile with pip freeze > requirements.txt so every run installs exactly the same versions.

What is the Jenkins pipeline syntax for automation?

The modern declarative style:

pipeline {
    agent any                  // where to run
    stages {
        stage('Build') {
            steps {
                sh 'make build'
                echo "Building..."
            }
        }
        stage('Test') {
            steps { sh 'make test' }
        }
    }
    post {
        always { echo "Cleanup" }
        failure { echo "Build failed" }
    }
}

Structure to name: pipeline → agent → stages → stage → steps, plus post for always/failed cleanup. The older scripted pipeline (a raw Groovy file with node { }) is more flexible but less structured — declarative is the default recommendation today.

How do you iterate over a dictionary in Python?

regions = {"us": "N. Virginia", "eu": "Ireland"}

for key, value in regions.items():
    print(f"{key} -> {value}")

for key in regions:              # keys only (default)
for value in regions.values():   # values only

.items() yields (key, value) tuples, which unpack directly into two loop variables. A practical DevOps use: looping over a dict of environment → config to generate deployments for each environment.

How do you set environment variables in Bash?

export APP_ENV="staging"     # exported: inherited by child processes
DEBUG=1 ./deploy.sh          # one-off: only for that command
printenv | sort              # inspect the current environment

The export keyword is the interview point: a variable set without export exists only in the current shell and is not visible to scripts/commands it launches. That's why CI tools and Dockerfiles use exported variables (or ENV instructions) — so child processes like your build script can read them.

What is an Ansible role?

A role is a reusable, self-contained unit of Ansible automation with a standard directory structure:

roles/nginx/
├── tasks/       # main.yml — the tasks to run
├── handlers/    # services to restart on change
├── templates/   # Jinja2 config templates
├── defaults/    # low-priority default variables
└── meta/        # role dependencies

Roles let you package "install and configure nginx" once and reuse it across playbooks, projects, and teams (public ones are shared via Ansible Galaxy). In interviews, connect it to DRY: playbooks define what runs where, roles define how a component is configured.

What is a multiline string in YAML?

Block scalars:

script: |            # literal: keeps newlines exactly
  echo "line 1"
  echo "line 2"

description: >       # folded: newlines become spaces
  This long sentence
  becomes one line.

Use | for scripts and certificates where line breaks matter, > for long human-readable text. A trailing minus (|-, >-) strips the final newline — relevant when generating files where you don't want an extra blank line. This comes up constantly in CI configs, Kubernetes ConfigMaps, and Ansible tasks.

What is an associative array in Bash?

A key-value map, available in Bash 4+:

declare -A server_env
server_env[web]="staging"
server_env[db]="prod"

echo "${server_env[db]}"                  # prod
for key in "${!server_env[@]}"; do        # iterate keys
  echo "$key -> ${server_env[$key]}"
done

Regular Bash arrays are index-based (arr[0]); associative arrays let you look up by name — ideal for mappings like environment → URL or service → host. Worth noting: declare -A requires Bash 4+, so it doesn't run on default macOS Bash (3.2) — a real portability gotcha in mixed teams.

How do you run a shell command from Python?

Use the subprocess module — os.system() is legacy:

import subprocess

result = subprocess.run(
    ["df", "-h"],              # list form: no shell injection risk
    capture_output=True,       # capture stdout/stderr
    text=True,                 # strings, not bytes
    check=True,                # raise CalledProcessError on non-zero exit
)
print(result.stdout)

Why subprocess wins: it returns the exit code and output programmatically, raises exceptions on failure (check=True), and the list form avoids shell injection. Pass shell=True only when you genuinely need shell syntax (pipes, globs) and the input is trusted.

What is jq in Linux?

jq is a command-line JSON processor — it parses, filters, and transforms JSON the way sed/awk handle text:

jq '.name' data.json                    # extract a field
jq -r '.servers[].ip' data.json         # -r: raw strings, no quotes
curl -s https://api.github.com/users/torvalds | jq '.followers'

It's essential in DevOps because cloud CLIs and APIs emit JSON: extracting instance IDs, parsing GitHub API responses, or pulling a value out of a Terraform output. Flag worth knowing: -r prints raw strings (without surrounding quotes), and -e sets the exit code based on the result — handy for scripting conditions.

How do you exit a script with a status code?

#!/bin/bash
[[ -f app.conf ]] || { echo "config missing"; exit 1; }
exit 0    # success (implicit if the script ends here)

Every command finishes with a status code: 0 means success, 1–255 mean failure, and $? holds the previous command's code. Conventions to mention: 1 is a general failure, 2 is often CLI misuse, and custom codes (e.g., 3, 4...) let the caller distinguish failure reasons. CI/CD pipelines and wrappers decide success purely by this code — a script that "fails loudly" but exits 0 will silently pass a pipeline.

On this page