Configuration management
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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اقرأ الدرس خطوة بخطوة
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 HOWWrite 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 stepsRead 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}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 doChange 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 {}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 sameSomeone 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, unnoticedThe 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 undonePush 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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What does the desired state describe?
Desired state is declarative: it says what the result should be, and the tool works out the steps to get there.
02With want = {"nginx": "on", "port": 80} and have = {"nginx": "off", "port": 80}, what does dict(want.items() - have.items()) give?
Only the pair ("nginx", "on") is wanted but missing. ("port", 80) is in both, so it is taken away.
03You run the tool twice in a row. What happens on the second run?
The operation is idempotent: running it once or many times gives the same result, so a repeat run changes nothing.
04Someone changes web3's port to 8080 by hand. What is this called, and what fixes it?
The server has drifted from the written state. The next run finds a non-empty diff for web3 and changes only its port back.
05What is the difference between push and pull mode?
Ansible pushes by default (over SSH). Puppet and Chef agents, and ansible-pull, pull on a schedule. Both run the same read, compare, change loop.
المسرد
- Configuration management
- Keeping servers in a written-down desired state with a tool, instead of changing them by hand. Tools include Ansible, Puppet, Chef and Salt.
- Desired state
- A description of the end result a server should have (which programs run, which settings they use), written in a file.
- Declarative
- Describing what the result should be, and letting a tool work out the steps to reach it.
- Facts
- What a server reports about itself at the start of a run (Ansible's word). The tool compares them with the desired state.
- Idempotent
- Giving the same result whether it runs once or many times. A second run of an idempotent tool changes nothing.
- Configuration drift
- When a server slowly stops matching its written state, usually because of changes made by hand.
- Push and pull mode
- Push: the tool contacts each server. Pull: each server checks in on a schedule and applies the state itself.
الملاحظات والمصادر
- The figure shrinks each server to two settings. Real tools manage packages, files, users and services, and each one has its own check (is it installed? is the file the same?), but the idea is the same: compare, then change only what differs.
- The card is real Python 3. want.items() - have.items() works because items views act like sets when every value is hashable; dict() turns the leftover pairs back into a dict.
- Ansible gathers facts with its setup module; Puppet and Chef use their own fact libraries (Facter and Ohai). Ansible also has a check mode (--check) that only reports what would change.
- Kubernetes controllers work the same way: a control loop keeps moving the current state toward the desired state, like a thermostat.
- Ansible documentation: Glossary (idempotency, facts, check mode, push and pull mode)
- Ansible documentation: Discovering variables: facts and magic variables
- Kubernetes documentation: Controllers (control loops, desired and current state)
- Kubernetes documentation: Declarative management of objects using configuration files
- Google SRE book, ch. 5: Eliminating Toil
- Martin Fowler: SnowflakeServer
- Python documentation: Dictionary view objects
اختبار يتاح بعد الخطوة الأخيرة.