Back to sections

Infrastructure as Code Quiz

Test what you actually know about Infrastructure as Code. Free sample questions below, from easy to hard, with instant explanations.

easy

1. A DevOps engineer runs the same Ansible playbook twice in a row on a server that already has the correct package installed and the correct config file in place. What should happen the second time?

The playbook reinstalls the package and overwrites the config file every time
The playbook reports no changes needed, since the system already matches the desired stateCorrect
The playbook fails because the package is already installed
The playbook skips all tasks without checking anything

Good automation checks the current state first and only makes changes when something does not match what is wanted. Since everything already matches, Ansible reports the run as unchanged instead of redoing work that is not needed.

medium

2. [CloudFormation] How do you reuse configuration across multiple stacks (e.g., VPC, IAM roles)?

Copy-paste templates
Use Nested Stacks or CloudFormation Registry (custom resources)Correct
Hardcode ARNs
Use AWS CLI scripts

Copy-pasting the same VPC or IAM setup into every template means fixing a bug in ten different places later. Nested stacks let a parent template just reference a shared child template for something like a VPC, so that piece is written once and reused everywhere, with each part still independently maintainable.

hard

3. [Ansible] How do you run a playbook against hosts with different OSes (e.g., Ubuntu/CentOS) using correct package managers?

Assume all are Ubuntu
Write separate playbooks
Use ansible_os_family fact + conditional tasksCorrect
Use raw module

Ubuntu uses apt and CentOS uses yum, so a single hardcoded package-install task would break on one of them. The ansible_os_family fact tells you which family a host belongs to, so a task can branch and use the right package manager automatically instead of you maintaining entirely separate playbooks per OS.

easy

4. [Ansible] What is an inventory file used for?

Writing playbooks
Storing passwords
Defining target hosts and groups (e.g., [webservers] host1, host2)Correct
Configuring modules

The inventory file is Ansible's address book: it lists every host you manage and lets you organize them into named groups, like [webservers], so a playbook can target 'all my webservers' by name instead of listing every single machine.

medium

5. An engineer stores a database password directly in a .tfvars file and commits that file to the team's shared Git repository so everyone has the same settings. A few weeks later, a security audit flags this as a serious problem. What is the concern?

The .tfvars file format does not support storing passwords
Terraform will refuse to run if variables are stored in a .tfvars file
The password is now readable by anyone with access to the repository's history, even if it's later removedCorrect
Git repositories cannot store text files larger than a few kilobytes

Once a secret is committed to version control, it typically stays in the project's history even after being deleted in a later commit, and anyone with repository access can find it. Secrets like passwords should instead be kept out of version control, using a secrets manager or environment variables injected at runtime.

hard

6. A team's Terraform configuration calls a shared module without pinning it to a specific version, referencing a moving branch instead of a fixed release. One morning, an unrelated Terraform run pulls in a new, breaking change that was just merged into that module's branch, and production infrastructure changes unexpectedly. What practice would have prevented this?

Pinning the module reference to a specific, tested version or commit instead of a moving branch referenceCorrect
Removing all modules and writing every resource inline
Running terraform apply more frequently so changes are caught sooner
Disabling terraform plan so changes happen faster

Referencing a module by a moving branch means any new commit to that branch can immediately affect every consumer of the module on their next run, with no warning. Pinning to a specific version lets teams choose when to upgrade deliberately, after testing the new version first.

easy

7. What is the key difference between `terraform plan` and `terraform apply`?

`plan` shows a preview of what changes would be made without touching any real infrastructure, while `apply` actually performs those changesCorrect
`plan` modifies the state file, while `apply` only modifies the real infrastructure
`plan` only works on the first run of a project, while `apply` works on every run after that
`plan` requires cloud provider credentials, while `apply` does not

terraform plan is a dry run: it compares your configuration against the current state and the real infrastructure, then prints out exactly what would be added, changed, or destroyed, without making any of those changes. terraform apply is the step that actually carries out that plan against real infrastructure and then records the new results in state. Both commands need valid provider credentials since both must read the current state of real resources to compute an accurate diff, so neither runs credential-free.

medium

8. [IaC General] Which practice minimizes "configuration drift"?

Manual SSH fixes
Regularly re-applying declarative IaC (e.g., terraform apply, ansible-playbook)
Both B and CCorrect
Using immutable infrastructure (rebuild instead of patch)

Configuration drift is when the real infrastructure slowly stops matching what your code says it should be, usually from manual tweaks. Regularly re-applying your IaC catches and corrects that drift after the fact, while immutable infrastructure (replacing servers instead of patching them) prevents most manual tweaks from ever happening in the first place, so using both together covers it from two directions.

hard

9. A team's `aws_instance` resource registers itself with an external service-discovery system when it's created. When the instance is destroyed through Terraform, a deregistration script needs to run first, otherwise the external system keeps a stale entry pointing at a dead server. Which Terraform feature is designed for this?

A `data` source that queries the service-discovery system and deletes the stale entry on the next terraform plan
A provisioner block with `when = destroy`, which runs as the last step before the resource is torn downCorrect
The instance's `user_data` script, since it reruns automatically on every terraform destroy
A `lifecycle { prevent_destroy = true }` block, which pauses destruction until a script confirms cleanup finished

A destroy-time provisioner, declared with when = destroy inside a provisioner block, is specifically designed to run an action, commonly local-exec or remote-exec, as the LAST step before Terraform deletes a resource, making it the right tool for deregistration or notifying an external system before teardown. prevent_destroy is a safety guard that simply blocks destruction outright, it doesn't run any cleanup step at all. user_data only executes when an instance first boots via cloud-init and has no special behavior tied to termination, and data sources are strictly read-only, so they can't delete anything in an external system.

easy

10. What problem does Terraform's state locking mechanism, such as a DynamoDB table paired with an S3 backend, actually solve?

It automatically resolves merge conflicts when two engineers edit the same .tf file
It encrypts the state file so secrets stored in it cannot be read by anyone with access to the S3 bucket
It rolls the state back to the last successful apply if the current apply fails partway through
It prevents two people or automation jobs from running `terraform apply` against the same state at the same time and corrupting itCorrect

When a locking-capable backend is configured, Terraform writes a lock record before it reads or modifies state, so a second run started at the same time fails immediately with an error acquiring the state lock instead of writing over the same file simultaneously and corrupting it. This is a separate concern from encryption at rest, which some backends support independently, and Terraform provides no automatic rollback if an apply fails midway. Conflicts between two people's .tf source files are handled by version control like git, since locking only ever concerns the state file, never the configuration itself.

Ready for the real thing?

Take the full timed Infrastructure as Code quiz and see your score.

    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.