Lesson 4 of 6 · Config Management with Ansible

The Core Ansible Workflow

Lint, dry-run with check and diff, try one host, run in batches, and read the recap, plus the options DBAs use most.

Beginner 8 sections 3 min read

The core workflow

Like Terraform, Ansible has a short, safe cycle for every change: check the playbook, preview what it would change with a dry run, try it on one server, then run it everywhere it's meant to go, and read the recap. The dry run is Ansible's equivalent of terraform plan.

WRITE1 LINT2 DRY RUN3 ONE HOST5 RECAP 4 RUN change theplaybook ansible-lint,syntax check --check --diff:changes nothing --limit to atest server ok, changed,failed all targets,in batches next change: write, lint, dry run, and run again

1. Check the playbook

$ ansible-playbook prepare_oracle.yml --syntax-check
playbook: prepare_oracle.yml

$ ansible-lint prepare_oracle.yml
Passed: 0 failure(s), 0 warning(s) on 1 files.
What it does
--syntax-check confirms the YAML and playbook structure are valid, without connecting to any server. ansible-lint goes further, flagging bad practices: short module names instead of full ones, shell where a module exists, tasks without names, and commands that aren't safe to repeat.
When to run it
On every change, and as the first step of the pipeline for every pull request.

2. Preview the changes

$ ansible-playbook -i inventory prepare_oracle.yml --check --diff
TASK [Create Oracle directories, owned by oracle] **************
--- before
+++ after
@@ -1,4 +1,4 @@
 {
     "path": "/u02/oradata",
-    "state": "absent"
+    "state": "directory"
 }
changed: [db02.acme.local] => (item=/u02/oradata)

PLAY RECAP *****************************************************
db01.acme.local : ok=4  changed=0  unreachable=0  failed=0
db02.acme.local : ok=4  changed=1  unreachable=0  failed=0
What it does
Check mode (--check) runs the playbook without changing anything: each task reports what it would do. --diff shows the exact before and after for files and settings. Together they're Ansible's dry run.
Its limits
Tasks that depend on earlier changes, and command or shell tasks, can't be fully predicted, and are skipped or reported as unknown in check mode. Treat the dry run as a strong preview, not a guarantee.

3. Try it on one server

$ ansible-playbook -i inventory prepare_oracle.yml --limit db03.acme.local

--limit restricts a run to some of the targets, here a single test server. If it goes well there, widen it: --limit test, then everything. It's also the quickest way to rerun a playbook only on servers that failed last time.

4. Run it everywhere

$ ansible-playbook -i inventory prepare_oracle.yml

For changes that could disrupt service, run in batches rather than everywhere at once. In the playbook:

- name: Apply OS settings to production database servers
  hosts: oracle:&prod
  become: true
  serial: 2                    # two servers at a time
  max_fail_percentage: 0       # stop everything if any server fails
  tasks:
    ...

With serial, Ansible finishes the whole play on each batch before starting the next, so a problem stops the rollout after a couple of servers instead of all of them.

5. Read the recap

The PLAY RECAP at the end summarizes each server: how many tasks were ok, changed, failed, skipped, or unreachable. Check it every time: an unexpected changed on a server that should already be right means someone has changed it by hand, which is drift. Running the playbook again straight away should show changed=0 everywhere, which proves it's idempotent.

Other useful options

OptionWhat it does
--tags, --skip-tagsRun only, or skip, tasks with given tags, such as --tags kernel
--start-at-task "name"Resume from a given task after fixing a failure
--stepAsk for confirmation before each task
-v, -vvvMore detail, for troubleshooting
-e "var=value"Set a variable on the command line, such as -e target_ru=19.28
--list-hostsShow which servers would be targeted, without running anything

Key takeaways

  • Check every playbook with --syntax-check and ansible-lint.
  • Dry-run with --check --diff to see what would change; it's a strong preview, not a guarantee.
  • Try changes on one server with --limit, then widen.
  • Roll out in batches with serial and stop on failures with max_fail_percentage.
  • Read the PLAY RECAP every time; a second run should show changed=0.
  • Use --list-hosts before touching production.