devops / infrastructure as code
Infrastructure as Code: Terraform State, Drift, OpenTofu and Ansible
How Terraform decides what to change, what the state file is for, how to catch drift, what happened to Terraform's license, and where Ansible fits. License and version facts were checked on the HashiCorp, OpenTofu and Ansible sites in October 2026.
Infrastructure as code means servers, networks and databases are described in text files and created by a tool, not by clicking through a console. The payoff is that infrastructure becomes reviewable, repeatable and recoverable like any other code. The hard part is not the syntax. It is understanding what the tool remembers about what it built, and what happens when the real world stops matching that memory. That memory is Terraform's state, and the mismatch is called drift. Both are covered here with a free practice setup that needs no cloud account. The page also answers two questions that now come up in almost every Terraform search: what changed with the license, and whether to learn OpenTofu.
01 · THE THREE-WAY COMPARISON
Config, state and reality: how a plan is decided
Every plan compares three things: your configuration (what you want), the state (what Terraform last recorded it built), and the real infrastructure (what exists now). HashiCorp's documentation says the primary purpose of state is to store bindings between objects in a remote system and the resource instances in your configuration, and that Terraform refreshes state against real infrastructure before any operation. Once you see the three inputs, plans stop being mysterious.
| Situation | Config | State | Real world | Plan result |
|---|---|---|---|---|
| You add a new resource block | has it | no record | absent | Create it |
| You change an argument | new value | old value | old value | Update in place, or replace if the provider cannot change it live |
| Someone edits it in the console | unchanged | old value | different | Refresh notices the difference; apply reverts it to match config |
| Someone deletes it in the console | has it | has it | gone | Create it again |
| You delete the block from config | absent | has it | present | Destroy it |
| You lose the state file | has it | empty | present | Plans to create a duplicate; use import to re-adopt the object first |
You can practice every row with no cloud account using the local_file resource from the hashicorp/local provider (version 2.9.1 was the latest in the registry when checked). It follows HashiCorp's documented example:
terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.9" } } } resource "local_file" "greeting" { content = "hello from terraform\n" filename = "${path.module}/greeting.txt" }
Run terraform init, then terraform plan, then terraform apply. Terraform creates greeting.txt next to the configuration and writes terraform.tfstate. The registry page for the resource even documents a state-versus-reality quirk: on a new machine where the file is absent, Terraform sees the resource as deleted and plans to recreate it.
02 · THE STATE FILE
Where state lives and why it needs care
By default Terraform stores each workspace's state in a local file named terraform.tfstate and keeps the previous version as terraform.tfstate.backup. The documentation calls the file JSON text and says not to edit it directly; use the terraform state subcommands for changes. It also warns against storing state in version control or any storage without state locking and secure access control, because that can cause data loss or expose secrets stored inside it.
That last point catches people out. State can contain sensitive values, such as passwords you passed to resources. A team therefore moves state to a remote backend that supports locking, so two people cannot apply at once, and restricts who can read it. HashiCorp recommends HCP Terraform or a remote backend for exactly this reason. For a solo practice project, the local file is fine as long as it never reaches a public repository.
*.tfstate and *.tfstate.backup to .gitignore before your first commit. A state file in a public repo is a credentials leak waiting to be found. Other IaC tools face the same question: Pulumi's documentation has its own page on state and backends, and the idea is identical.03 · DRIFT
Catching drift before it causes an outage
Drift is any difference between the infrastructure and what the code and state say. HashiCorp's drift tutorial states it plainly: do not make manual changes to resources Terraform controls, because the state will drift from the real infrastructure. Emergencies still happen, so know three tools.
- Refresh-only plan.
terraform plan -refresh-onlyupdates the plan to match changes made outside Terraform without proposing to revert them. The tutorial notes this is safer than the standalone refresh command, which overwrites your state without showing you the changes first. - A scheduled plan in CI.
terraform plan -detailed-exitcodeexits with 0 for no changes, 1 for an error and 2 when the plan contains changes. Run it nightly in a pipeline and alert on exit code 2 and you have free drift detection. The tutorial points out that built-in drift detection is an HCP Terraform feature on its Standard Edition, so the scheduled plan is the no-cost route. - Import. When something exists that Terraform does not know about,
terraform importor animportblock brings it under management. The import documentation says import blocks can run during apply, which lets you automate adoption in a pipeline.
04 · THE LICENSE CHANGE
Terraform, OpenTofu and what actually changed
On August 10, 2023, HashiCorp announced it was moving its products from the Mozilla Public License 2.0 to the Business Source License 1.1 (BUSL) for future releases, while its APIs, SDKs and almost all libraries stayed on MPL 2.0. The current Terraform license file shows the licensed work as Terraform 1.6.0 or later, with IBM as licensor. Its additional use grant allows production use, provided you do not offer the software to third parties on a hosted or embedded basis to compete with the paid versions. The same file says products not provided on a paid basis are not competitive, and sets a change license: four years after each version is published it converts to MPL 2.0.
| Terraform | OpenTofu | |
|---|---|---|
| License | BUSL 1.1 from version 1.6.0 on | Mozilla Public License 2.0 |
| Governance | HashiCorp, with IBM named as licensor | Under the Linux Foundation's stewardship |
| Latest version listed | 1.17.1 | 1.13.2 (newest 1.13 release on its GitHub page) |
| Origin | Original project | A fork created in response to the license change, by Gruntwork, Spacelift, Harness, Env0, Scalr and others |
| Day-to-day commands | terraform init / plan / apply | The same, with the tofu command; the site calls it a drop-in replacement |
For a learner the practical advice is short. The concepts, state, providers, modules and plans, are the same in both, so skills transfer. If you only use Terraform to manage your own or your employer's infrastructure, the additional use grant covers that. The license matters most to vendors who build products on top of it, and to organizations whose policy forbids source-available licenses. Check which tool your target employer uses, and learn that one first.
05 · WHERE ANSIBLE FITS
Provisioning versus configuring
Terraform creates infrastructure objects and tracks them in state. Ansible configures machines that already exist. Its documentation says it uses SSH with existing OS credentials to reach remote machines, so there is no agent to install, and that most modules check whether the desired state is already achieved and do nothing if so. That property is idempotency: running a playbook twice should not change anything the second time. The Ansible source is licensed under the GNU General Public License v3.
- name: Configure the web servers
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Start nginx and enable it at boot
ansible.builtin.service:
name: nginx
state: started
enabled: trueThe common pattern is to let Terraform create the servers and hand their addresses to Ansible, which installs and configures the software. When someone searches "infrastructure as code Ansible", this split is usually the answer they need: they are complementary tools, not alternatives. Pulumi, which uses general-purpose languages such as Python or TypeScript instead of HCL, is Apache 2.0 licensed and is the main alternative to Terraform itself.
06 · DOCUMENTATION WORTH OPENING
Free references for each piece
- Terraform state ↗What state stores, where it lives by default, and the warnings about version control.developer.hashicorp.com
- Manage resource drift ↗A hands-on tutorial that introduces drift on purpose and walks through refresh-only plans.developer.hashicorp.com
- terraform plan command reference ↗Every option, including
-refresh-onlyand-detailed-exitcode.developer.hashicorp.com - Terraform tutorial library ↗HashiCorp's step-by-step tutorials, organized by cloud and topic.developer.hashicorp.com
- HashiCorp licensing FAQ ↗HashiCorp's own answers on what the BUSL permits, which it describes as binding.hashicorp.com
- OpenTofu getting started ↗Installation and a first configuration with the
tofucommand.opentofu.org - Getting started with Ansible ↗Install Ansible, build an inventory and run a first command.docs.ansible.com
- Ansible playbooks ↗Playbook syntax, desired state and idempotency, from the Ansible guide.docs.ansible.com
- Pulumi: state and backends ↗How a different IaC tool answers the same state question.pulumi.com
07 · PRACTICE
Cause drift on purpose
main.tf above and apply it. Open terraform.tfstate and find the resource entry (read it, do not edit it). Delete greeting.txt by hand, then run terraform plan and read what it proposes. Run terraform plan -detailed-exitcode and check the exit code with echo $?. Finally apply, delete the state file, and run a plan to see what losing state looks like. Everything is local, so nothing here can cost money.08 · FAQ
Frequently asked questions
What is the Terraform state file?
It is a JSON file where Terraform records which real-world objects correspond to the resources in your configuration. By default it is terraform.tfstate in your working directory. Do not edit it by hand, and do not commit it to version control, because it can contain secrets.
What is infrastructure drift?
Drift is when real infrastructure no longer matches what your code and state describe, usually because someone changed it manually. You can see it with a refresh-only plan, or detect it on a schedule by running terraform plan with the detailed exit code option and alerting when the exit code is 2.
Did Terraform stop being open source?
Since version 1.6.0 Terraform uses the Business Source License 1.1, which is source-available rather than open source by the usual definition. It still allows production use, except offering it as a hosted or embedded product that competes with the paid versions. OpenTofu is a fork that stays under MPL 2.0.
Should I learn Terraform or OpenTofu?
The concepts are shared, so either teaches you the same skills. Check which one the employers you are targeting use and start there. Before switching an existing project, read each tool's compatibility notes.
Should I learn Terraform or Ansible first?
Learn Terraform first if you want to create cloud resources, and Ansible first if you manage existing servers. They solve different problems: Terraform provisions infrastructure and tracks it in state, while Ansible configures software on machines over SSH. Most teams end up using both.