CompTIA Linux+ (XK0-006) · Domain 5 · 17% of exam

Automation, Orchestration, and Scripting

Drill 20 practice questions focused entirely on Automation, Orchestration, and Scripting for the CompTIA XK0-006 exam. Tap an answer for instant feedback and a full explanation — no sign-up, always free.

Verified answer20 questions
Question 1 of 20

An Ansible playbook runs as the unprivileged user 'deploy' via SSH. One task must install a package with the 'dnf' module, which requires root. The playbook connection succeeds, but the package task fails with a permissions error. Which change to the failing task correctly escalates privileges to root using sudo?

Reviewed for accuracy · Report an issue
Question 2 of 20

Before applying a large Ansible playbook to 40 production web servers, a DevOps engineer wants to preview exactly which tasks would report a change without actually modifying any managed node. Which ansible-playbook invocation accomplishes this dry run?

Reviewed for accuracy · Report an issue
Question 3 of 20

You are writing an Ansible playbook that installs the Apache web server on a mixed fleet of servers. RHEL-based hosts need the 'httpd' package while Debian-based hosts need 'apache2'. You want a single task that installs the correct package based on the target host's distribution, using facts automatically collected at the start of the play. Which conditional expression should you attach to the install task?

Reviewed for accuracy · Report an issue
Question 4 of 20

You are writing an Ansible playbook that deploys an updated Nginx configuration file with the 'template' module. Nginx should only be restarted when the configuration file actually changes, not on every playbook run. Which approach correctly implements this behavior?

Reviewed for accuracy · Report an issue
Question 5 of 20

An Ansible playbook manages a fleet of web servers. A team member added a task that runs a shell command to regenerate a config cache. Every playbook run reports this task as 'changed', even when nothing was actually modified, which triggers an unnecessary handler that restarts the web service. Which approach BEST makes this task idempotent so it only reports 'changed' when the cache is truly rebuilt?

Reviewed for accuracy · Report an issue
Question 6 of 20

An Ansible playbook installs several packages using a loop and registers the output. A teammate wants a subsequent task to print the name of each package that was actually installed (changed), skipping packages that were already present. Which construct correctly iterates over the registered results to access per-item status?

Reviewed for accuracy · Report an issue
Question 7 of 20

You maintain an Ansible playbook that deploys a web application. The playbook references a variables file, group_vars/prod/secrets.yml, containing a database password in plaintext. Your security team requires that this file be encrypted at rest in the Git repository but still usable during automated playbook runs. Which approach meets these requirements?

Reviewed for accuracy · Report an issue
Question 8 of 20

A systems administrator is writing a Bash maintenance script that accepts a single argument ($1) representing an action: 'start', 'stop', or 'restart'. Any other value should print an error and exit with status 1. The administrator wants the cleanest, most readable construct that matches the argument against these fixed string patterns. Which snippet correctly implements this behavior?

Reviewed for accuracy · Report an issue
Question 9 of 20

A colleague wrote a Bash script with a function that computes a value and stores it in a variable named 'result'. After the function runs, the main script uses '$result' but keeps getting the wrong value because an unrelated global variable also named 'result' is being overwritten by the function. What is the BEST way to fix the function so it does not clobber the global variable while still returning its computed value to the caller?

Reviewed for accuracy · Report an issue
Question 10 of 20

You are writing a provisioning script that must create an application config file. The file needs to embed the value of the shell variable $APP_PORT while writing multiple lines to /etc/myapp/app.conf in a single command. Which construct correctly writes the block, with variable expansion, to the file?

Reviewed for accuracy · Report an issue
Question 11 of 20

A backup script must process a list of directory paths, some of which contain spaces (e.g., '/data/project files'). The current code fails, treating each word as a separate item: ```bash DIRS="/data/project files /var/log /srv/web root" for d in $DIRS; do tar -czf "/backup/$(basename "$d").tgz" "$d" done ``` Which change correctly handles paths containing spaces?

Reviewed for accuracy · Report an issue
Question 12 of 20

A DevOps engineer writes a Bash script that must process a list of hostnames from a file called servers.txt. For each host, it should attempt an SSH connection test and print 'UP' only if the connection succeeds. The current loop always prints 'UP' regardless of connectivity: while read host; do ssh -o ConnectTimeout=5 "$host" exit echo "$host UP" done < servers.txt Which modification correctly prints 'UP' only when the SSH command succeeds?

Reviewed for accuracy · Report an issue
Question 13 of 20

A backup script must use the value of the environment variable BACKUP_DIR if it is set, but fall back to /var/backups if the variable is unset or empty, without permanently assigning the variable. Which line accomplishes this correctly?

Reviewed for accuracy · Report an issue
Question 14 of 20

You are writing a deployment script that accepts a variable number of hostnames as command-line arguments. After processing the first argument (a flag like --restart), you want to loop through the remaining arguments as hostnames. Which construct correctly removes the first positional parameter so that $@ contains only the remaining hostnames?

Reviewed for accuracy · Report an issue
Question 15 of 20

A DevOps engineer needs a Bash script to process a file named servers.txt that contains one hostname per line, including some lines with embedded spaces and trailing whitespace. Each hostname must be read exactly as written and passed to a ping check. Which loop construct correctly reads the file line by line while preserving leading/trailing whitespace and avoiding word splitting?

Reviewed for accuracy · Report an issue
Question 16 of 20

A deployment script silently continues after a failed command in the middle of a pipeline, leaving the system in an inconsistent state. A colleague reports that even when a step like 'curl ... | tar xz' fails at the curl stage, the script still exits 0 and proceeds. You want the script to abort immediately on any command failure, including failures anywhere in a pipeline, and treat unset variables as errors. Which line should you add near the top of the script?

Reviewed for accuracy · Report an issue
Question 17 of 20

A backup script creates a temporary working directory with mktemp -d and stores its path in the variable WORKDIR. The script may exit at several points due to errors, and each early exit currently leaves behind orphaned temp directories in /tmp. You must guarantee the temp directory is removed whenever the script terminates, regardless of how it exits. Which line should you add near the top of the script?

Reviewed for accuracy · Report an issue
Question 18 of 20

You are writing a deployment script that must pause until a backend service responds successfully to a health check before continuing. The command 'curl -sf http://localhost:8080/health' returns exit status 0 when the service is healthy and non-zero otherwise. You want the script to keep retrying (with a 2-second pause between attempts) and only proceed once the check succeeds. Which loop construct correctly accomplishes this?

Reviewed for accuracy · Report an issue
Question 19 of 20

You maintain a docker-compose.yml file that defines a web service. During a load test, you need to run three instances of the 'web' service without editing the compose file. Which command starts the stack with three replicas of the web service?

Reviewed for accuracy · Report an issue
Question 20 of 20

A production hotfix was committed to the 'develop' branch as commit 4f2a9c1, but the release manager needs only that single fix applied to the 'release-2.3' branch without merging any of the other work-in-progress commits from 'develop'. You have already checked out the 'release-2.3' branch. Which Git command applies only that one commit to the current branch?

Reviewed for accuracy · Report an issue

More XK0-006 practice

Keep going with the other CompTIA Linux+ (XK0-006) domains, or take a full timed mock exam.

← Back to XK0-006 overview