Test what you actually know about Infrastructure as Code. Free sample questions below, from easy to hard, with instant explanations.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.