Articles

Infrastructure automation

Declaring infrastructure instead of typing it: idempotence, state, and where it stops.

Reading: 5 minServer & Virtualization

Article cover: Infrastructure automation

Infrastructure automation replaces typed commands with a declaration: describe the state a system should be in, and an engine makes it match. Two ideas carry the practice. Idempotence means that running the same declaration twice leaves the same result. The mapping is the gap between declaration and machine: Ansible re-reads each host on every run, while Terraform keeps a state file of what it created.

        declare the destination, not the steps

  declared (desired)          engine                  observed (real)
  ------------------          ------                  ---------------
  playbook / task         Ansible keeps no state      host, service,
  .tf configuration       of its own: every run       package, VM,
  policy (.rego)          re-reads the host and       network, image
                          changes only what differs
                                    |
  +---------------------------------+----------------------------------+
  | Terraform keeps a state file: the mapping between each declared    |
  | instance and the real object it created.                            |
  |    refresh  ->  re-read reality into the state                      |
  |    plan     ->  configuration minus state   (the computed delta)    |
  |    apply    ->  perform that delta, on approval                     |
  +---------------------------------------------------------------------+

  idempotence: run it a second time and the second run has nothing to change

Declaring infrastructure instead of typing it

An Ansible playbook is the written form of a declaration. Playbooks provide a “repeatable, reusable, simple configuration management and multimachine deployment system that is well suited to deploying complex applications”, and a playbook lets you “Declare configurations.” If a task must run more than once, the playbook is the artifact, kept under version control. Terraform says the same from the other side: “Terraform configuration files are declarative, meaning that they describe the end state of your infrastructure.” Both ask for the destination, not for steps.

Idempotence: the property everything rests on

The promise is that a declared state is a fixed point. “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”, and so “Whether you run a playbook once or multiple times, the outcome should be the same.” The honesty clause matters as much as the promise: “not all playbooks and not all modules behave this way”, so a playbook is tested in a sandbox first.

Check mode is the read-only form of the same run: “In check mode, Ansible runs without making any changes on remote systems”, and “Instead of implementing any modifications, Ansible provides a report on the changes it would have made.” The report is only as good as the modules, though: “Modules that do not support check mode report nothing and do nothing”. The summary to keep: “Check mode is just a simulation.”

Terraform: the plan, and the state that makes it possible

Terraform turns the gap into a product of its own workflow. “Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.” The plan is a computed difference between the configuration and what exists; then “Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies.”

The plan is possible only because Terraform remembers: “It also keeps track of your real infrastructure in a state file” that is the source of truth. “The primary purpose of Terraform state is to store bindings between objects in a remote system and resource instances declared in your configuration” — the state is that mapping, and without it the engine cannot tell an object it created from one it has never seen. State is an artifact to guard: “Avoid storing your state in a version control system or other storage solution that does not support Terraform state locking and secure access control”, and do not edit it by hand — “Do not directly edit this file.”

Policy as code: when the declaration needs rules

A declaration says what should exist; it does not say what is allowed. That second kind of statement is policy, and Open Policy Agent turns it into source. OPA is a “general-purpose policy engine that unifies policy enforcement across the stack” that “lets you specify policy as code and simple APIs to offload policy decision-making from your software”; the principle is that “OPA decouples policy decision-making from policy enforcement.”

The documentation’s example is the infrastructure case: a rule that has to hold when a change is made and again on a later audit — “The policy needs to be enforced when servers, networks, and ports are provisioned and the compliance team wants to periodically audit the system to find servers that violate the policy.” Written as code it returns a set of violations rather than a verdict, and the same engine runs over a plan “to validate Terraform plans”.

First boot: provisioning what the declaration cannot reach

Some of the estate exists before any configuration tool can log in. cloud-init is the “industry standard multi-distribution method for cross-platform cloud instance initialization”: “Cloud instances will automatically be provisioned during first boot with networking, storage, SSH keys, packages and various other system aspects already configured.” It is “no-install first-boot configuration management of a cloud instance”.

Platforms expose the same idea through their API. Proxmox VE’s history turns on it — “The introduction of the REST API, with a complete declarative specification written in JSON Schema” — and automation is the expected operator: “Once the installation is complete and the host has booted up, automation tools like Ansible can be used to further configure the installation.” A template and a clone are the unit of a declared virtual machine, and the credential that drives the automation should expire: “Set an expire timestamp (Unix epoch seconds) when creating a token so that abandoned automation does not silently keep working.”

Where automation stops

The declaration is only as good as the three things under it: an idempotent engine, a state that is kept, and a boundary that is admitted.

Automation without state diverges. An engine that changes a machine but records nothing leaves reality unowned, and nothing can tell a declared change from an undeclared one. Measuring that gap is the companion sheet’s subject — detection and reconciliation belong to the sheet on configuration drift — but a declaration still needs an owner for its result.

Check mode is a simulation, not a proof. “Check mode is just a simulation”, and a module that cannot simulate reports nothing; a silent read-only run proves only that no module had anything to say.

What cannot be declared stays manual and documented. Physical cabling, a one-off migration, a licence decision and the hand-fix applied during an incident are not states a playbook can assert, so they are left manual and recorded as such.

And not every scheduled action is this subject: a script run by a cron job or a systemd timer is scheduling, not declarative infrastructure, and belongs to its own operating-system sheet.

Level and prerequisites

L5 — specialist: declaring infrastructure, and reasoning about the model behind the tools rather than their syntax. It assumes the shell, service and scheduler material of the L2 operating-system sheets and the operational vocabulary of the L4 virtualization sheets, and it sits beside the companion sheet on detecting configuration drift. No single module or provider is taught, because the subject is the model.

Where to go next

  • Server & Virtualization — the area this sheet belongs to.
  • Virtualization and networking — the node this sheet sits in.
  • The companion sheet on detecting configuration drift, and the L2 sheets on scheduled tasks and on systemd units and services, which this sheet assumes.
  • The L4 sheets on capacity, host maintenance, clusters and quorum.

References

Nodrius Field Kit — cover

Get the Nodrius Field Kit

Enter your email and we'll send you the complete Field Kit: eight technical resources in one download.

Your email address is sent to Nodrius and used to deliver the Field Kit to you by email. Marketing messages are separate and optional: the checkbox does not affect your download, and you are only added to our updates list if you tick it. Privacy Policy.

Privacy & cookies

This site does not use cookies, analytics or tracking, and it does not profile you. There is nothing technical to switch off, so Accept and Reject change nothing about how the site works: either choice lets you browse everything normally.

The only difference is what your browser remembers: two entries in local storage — your choice here, so this notice is not shown again, and a marker that the Field Kit offer has already been made — so the offer is presented once and not on every page. They contain no identifier, they are not shared with anyone, and the site sets no cookie for either.

What we do with your data
How your email address is used when you ask for a Resource, how long it is kept and which rights you have — in the Privacy Policy.
What the site stores in your browser
Two entries in local storage — your choice on this notice and whether the Field Kit offer has been shown — and no cookies, no session storage, no third-party content: in the Cookie Policy.