ENع
←Config mgmt
Lesson 14 · Delivery

Configuration management

12 min9 steps5 questionsIntermediate
About this lesson

Configuration management keeps many servers set up the same way. You write down the desired state once, and a tool makes every server match it. Follow one tool, five lines of real Python, across three servers: it reads what each one has, works out the difference, changes only that, and reports it. Run it again and nothing changes. Edit a server by hand and the next run puts it back.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 14 · Interactive
01 / 09

One written state, many servers

Configuration management means writing down how every server should be set up, then letting a tool make each one match. No one logs in to change servers by hand, so they all stay the same.

desired state (written once, in a file)
        ↓ a tool applies it
web1  web2  web3   …all made to match

tools: Ansible, Puppet, Chef, Salt
idea : say WHAT you want, not HOW
Loading figure…
or ← →
Read the lesson step by step
  1. One written state, many servers

    Configuration management means writing down how every server should be set up, then letting a tool make each one match. No one logs in to change servers by hand, so they all stay the same.

    desired state (written once, in a file)
            ↓ a tool applies it
    web1  web2  web3   …all made to match
    
    tools: Ansible, Puppet, Chef, Salt
    idea : say WHAT you want, not HOW
  2. Write the desired state

    Line 1 is the desired state: nginx (a web server program) switched on, listening on port 80. It says what the end result is, not the steps to get there. This style is called declarative.

    want = {"nginx": "on", "port": 80}
    
    nginx  "on"   the web server is running
    port   80     it listens on port 80 (HTTP)
    
    declarative: describe the result
    the tool works out the steps
  3. Read what each server has

    A run starts by asking every server what it has now. Ansible calls these answers facts. web1 has nginx off, web2 is brand new and empty, web3 is already right.

    have (what each server reports)
      web1  {"nginx": "off", "port": 80}
      web2  {}             new and empty
      web3  {"nginx": "on", "port": 80}
  4. Work out the difference

    Line 3 compares want with have. Taking one set of key-value pairs away from the other leaves only what is wanted but missing or wrong. web1 needs one fix, web2 needs two, web3 needs none.

    diff = dict(want.items() - have.items())
    
    web1  diff = {"nginx": "on"}
    web2  diff = {"nginx": "on", "port": 80}
    web3  diff = {}            nothing to do
  5. Change only what differs

    Lines 4 and 5 make just those changes and report them. web1 gets one change, web2 gets two, and web3 is not touched at all. Ansible prints these as changed or ok.

    have.update(diff)      return diff
    
    web1  changed  {"nginx": "on"}
    web2  changed  {"nginx": "on", "port": 80}
    web3  ok       {}
  6. Run it again: nothing changes

    Run the tool a second time. Every server already matches, so every diff is empty and nothing changes. A step that gives the same result however many times it runs is called idempotent.

    second run
      web1  diff = {}   ok
      web2  diff = {}   ok
      web3  diff = {}   ok
    
    idempotent: run once or many times,
    the result is the same
  7. Someone edits a server by hand

    To test something, a person logs in to web3 and changes the port to 8080. Now web3 no longer matches the written state. This slow, silent difference is called configuration drift.

    by hand on web3:  port 80 → 8080
    
    written state  port 80
    web3 really    port 8080
    
    drift: a server slips away from
    the written state, unnoticed
  8. The next run undoes the drift

    The next run reads every server again. Only web3 has a non-empty diff, so only its port goes back to 80. Running often on a schedule keeps drift short-lived.

    next run
      web1  diff = {}             ok
      web2  diff = {}             ok
      web3  diff = {"port": 80}   changed
    
    port 8080 → 80   drift undone
  9. Push or pull

    In push mode, the default in Ansible, the tool contacts each server. In pull mode each server checks in on a schedule and applies the state itself, as Puppet and Chef agents do. Either way, the loop is the same.

    push  tool → servers   (Ansible, over SSH)
    pull  servers → tool   (agents check in
                            on a schedule)
    
    both: read → diff → change → report

Quiz Unlocks after the last step.