Infrastructure as code (IaC) means writing servers, networks and firewall rules as text files and applying them with a tool, instead of clicking through a console. Terraform describes the end state you want; an Ansible playbook runs ordered tasks. Both files live in git, so every change has a commit, a review and a date. Terraform also keeps a state file, which can hold secrets and must stay out of git.
- IaC turns infrastructure into reviewed, versioned text files.
- Terraform is declarative: it compares the desired end state with what exists and plans the difference.
- Ansible playbooks run plays and tasks in order, from top to bottom.
- Terraform state maps your configuration to real resources and can contain passwords and API tokens in plain text.
- The git history of an IaC repo is a dated record of who changed the infrastructure, why, and who approved it.
What is infrastructure as code?
Infrastructure as code (IaC) is the practice of defining servers, networks, firewall rules and cloud services in text files that a tool applies for you. HashiCorp puts it this way: "IaC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share" (HashiCorp).
The point is that the files, not someone's memory of a console session, say what exists. A new environment is built from the same files as the old one. A change goes through the same steps as a code change: a branch, a review, a merge, an apply. HashiCorp notes that "you can commit your configurations to version control to safely collaborate on infrastructure" (HashiCorp).
There are two broad styles. A declarative tool, such as Terraform, is told the end state and works out the steps. A procedural tool, such as an Ansible playbook, is told the steps in order. Most teams use both: Terraform to create cloud resources, and Ansible to configure what runs on them.
Infrastructure as code examples: Terraform and Ansible
The Terraform file below creates one AWS security group, which is a cloud firewall attached to servers. It allows HTTPS from anywhere and SSH only from an office network held in a variable. Terraform's language "is declarative, meaning that it describes the desired end-state for your infrastructure" (HashiCorp), so the file lists rules, not the API calls that create them.
The Ansible playbook below turns off SSH password logins on every host in the webservers group, checks the new file before saving it, and restarts the SSH service only if the file changed. In Ansible's words, "a playbook consists of one or more 'plays' in an ordered list", and "a playbook runs in order from top to bottom" (Ansible documentation).
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "admin_cidr" {
description = "Office network allowed to reach SSH"
type = string
default = "203.0.113.0/24"
}
resource "aws_security_group" "web" {
name = "web-frontend"
description = "Public HTTPS, SSH from the office only"
ingress {
description = "HTTPS from anywhere"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH from the office network"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.admin_cidr]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
---
- name: Harden SSH on web servers
hosts: webservers
become: true
tasks:
- name: Turn off password logins for SSH
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PasswordAuthentication'
line: 'PasswordAuthentication no'
validate: '/usr/sbin/sshd -t -f %s'
notify: Restart sshd
handlers:
- name: Restart sshd
ansible.builtin.service:
name: sshd
state: restarted
Illustrative examples written for this page, not taken from any company. 203.0.113.0/24 is a range reserved for documentation.
Before applying a playbook, Ansible's check mode shows what would change: "ansible-playbook --check playbook.yaml" (Ansible documentation). Terraform's equivalent is the plan step, which will "preview the changes Terraform will make to match your configuration" (HashiCorp).
Terraform vs Ansible: which one does what?
Ansible modules are built to be safe to run again. The documentation says "most Ansible modules check whether the desired final state has already been achieved and exit without performing any actions if that state has been achieved" (Ansible documentation). Terraform gets the same effect a different way: it records what it built in a state file and plans only the difference.
| Question | Terraform | Ansible |
|---|---|---|
| Style | Declarative: the end state | Ordered tasks in a playbook |
| Typical job | Create cloud networks, servers, DNS, firewalls | Configure operating systems, packages, services |
| How it knows what exists | A state file | Checks each host when it runs |
| Preview before change | terraform plan | ansible-playbook --check |
| File format | HCL (.tf files) | YAML playbooks |
What is Terraform state, and why is it sensitive?
Terraform state is the file in which Terraform records what it has built. "The primary purpose of Terraform state is to store bindings between objects in a remote system and resource instances declared in your configuration" (Terraform documentation). By default it is a local file named terraform.tfstate, written as JSON.
The state is sensitive because it copies real values. HashiCorp warns that "if you add secret values directly to your configuration, Terraform stores those secrets in its state and plan files", and that state and plan files can contain "sensitive values, such as initial database passwords or API tokens" (Terraform documentation). Stored locally, the state is a plaintext file.
For that reason HashiCorp says to avoid keeping state in version control, "because doing so can result in data loss or exposure of secrets stored in the state file" (Terraform documentation), and to treat it "as sensitive data by excluding it from Git workflows" (Terraform documentation). Teams keep state in a remote backend with locking and access control. Saved plan files (made with the -out option) carry the same risk.
What is GitOps, and how does it relate to infrastructure as code?
GitOps is a way of running infrastructure in which a git repository is the only place changes are made, and software keeps the live system in line with it. The OpenGitOps project lists four principles (OpenGitOps):
- Declarative: "a system managed by GitOps must have its desired state expressed declaratively".
- Versioned and immutable: desired state "retains a complete version history".
- Pulled automatically: software agents pull the desired state from the source.
- Continuously reconciled: "software agents continuously observe actual system state and attempt to apply the desired state".
IaC is the files. GitOps is the operating rule that every change goes through those files and an agent applies them. GitOps is most common with Kubernetes, but the idea applies to any IaC repository.
What does the git history of an infrastructure as code repo record?
Because every change is a commit, an IaC repository becomes a dated log of how the infrastructure changed. Read with the tickets and incident records of the same period, it shows not only what changed but why. The table lists what a mature repo usually holds.
A Terraform plan is often saved as a build artifact next to the commit. The terraform show command "has a -json option for inspecting the latest state snapshot in full, and also for inspecting saved plan files" (Terraform documentation), which makes plans readable by other tools.
| Record | What it shows | Where it lives |
|---|---|---|
| Commits | Who changed which file, when, with a message | git |
| Pull or merge requests | The proposed change, review comments and the approver | GitHub, GitLab, Bitbucket |
| Plan output | The exact resources Terraform would add, change or destroy | CI logs or saved plan files |
| Apply logs | What actually ran and whether it worked | CI system or HCP Terraform |
| Ticket references | The request or incident behind the change | Commit messages and PR descriptions |
| Tags and releases | Which version ran in production on a given date | git |
Common mistakes with infrastructure as code
Most IaC problems come from treating the files as the whole truth while people still change things by hand.
- Committing terraform.tfstate or .tfvars files with real passwords and keys to git.
- Making emergency fixes in the console and never writing them back into code, so the next apply undoes them. See config drift.
- Writing commit messages such as "fix" with no ticket, so nobody can tell later why a firewall rule exists.
- Applying without a reviewed plan, or reviewing a plan that is not the one that ran.
- Keeping one huge state file for everything, so one bad change can touch every system.
- Deleting a secret in a new commit and assuming it is gone. It stays in the history; see remove secrets from git history.
Is infrastructure as code history also a data asset?
It can be, when it covers the company's own infrastructure. AI data buyers have asked Origin for configuration and infrastructure-as-code history (Terraform, Ansible, firewall rule sets and routing) with the commits and reviews, going back years. They want it joined to the incidents it answered: the ticket, the chat thread, the config diff or pull request, and the outcome, with the runbooks, on-call records and postmortems from the same period. The first step they asked for is a sample of two or three incidents end to end plus a slice of the config history. See incident timelines, incident postmortems and the hub page for infrastructure incident data.
Origin is an independent data partner. Your company decides what is shared and signs any license itself, and a buyer may pay Origin when a deal closes.
- State and plan files: leave terraform.tfstate, saved plans and .tfvars out entirely. They hold real values, not code.
- Secrets in history: scan every commit, rotate anything found, then rewrite the history. See remove secrets from git history.
- Client infrastructure: repos that manage a client's systems belong to that client unless the client has agreed.
- Addresses and hostnames: public IP addresses, account IDs and internal names can identify the company and its customers. See anonymizing logs and captures.
- Personal data: commit authors, reviewer names and emails need masking or consistent pseudonyms.
- Third-party code: check the license terms of vendored modules and roles before they leave the company.
Does your company hold records like these? See what your data is worth.
Common questions
What is infrastructure as code in simple terms?
Infrastructure as code means describing servers, networks, firewall rules and cloud services in text files, then letting a tool such as Terraform or Ansible build them. The files live in git, so every change is reviewed, dated and can be repeated or rolled back, instead of being clicked together by hand in a console.
What are some infrastructure as code examples?
Common infrastructure as code examples are a Terraform file that creates a cloud network, server or firewall group, an Ansible playbook that installs and configures software on a set of hosts, and Kubernetes manifests applied by a GitOps agent. This page shows a Terraform security group and an Ansible playbook that turns off SSH password logins.
What is stored in a Terraform state file?
A Terraform state file stores the link between each resource in your configuration and the real object it created, plus that object's attributes and metadata. Those attributes can include sensitive values such as initial database passwords or API tokens. Stored locally it is a plaintext JSON file named terraform.tfstate by default, so it needs access control.
Should Terraform state be committed to git?
No. HashiCorp advises against storing Terraform state in version control because it can expose secrets held in the state file and cannot be locked safely. Keep state in a remote backend with locking, encryption and access control, and add terraform.tfstate and its backup to .gitignore. Saved plan files deserve the same care.
What is the difference between GitOps and infrastructure as code?
Infrastructure as code is the practice of defining infrastructure in files. GitOps is an operating model built on top of it: the desired state lives in a versioned git repository, software agents pull it automatically, and they keep reconciling the live system with it. You can use infrastructure as code without GitOps, but not GitOps without declarative files.
Is Ansible infrastructure as code?
Yes. Ansible playbooks are text files kept under source control that configure servers and network devices the same way every time. They run ordered tasks rather than declaring only an end state like Terraform, but most Ansible modules are idempotent, so running a playbook twice leaves the system unchanged the second time.
Can a SaaS company share its Terraform history and postmortems with an AI company?
Often yes, for its own infrastructure, once secrets, state files, customer data and personal data are removed and the company has checked its customer contracts. The useful record is the Terraform history with reviews, joined to the incidents, paging records and postmortems of the same period. A first step is usually a small sample of a few incidents end to end.
