DEV Community

Cover image for Day 91: A Playbook That Undoes Itself and a Test That Could Fail
Nnamdi Felix Ibe
Nnamdi Felix Ibe

Posted on AI-assisted

Day 91: A Playbook That Undoes Itself and a Test That Could Fail

Day 91 of DevOps, Day 41 of Azure. Both tasks today were about verification, from opposite ends. The Azure half is the best verification I have written in this series. The Ansible half ran clean, reported success, and had already undone half its own work in a way the first run could not show me.

One Ansible task, one Azure task. A playbook that fights itself, then a test designed so it could fail. The tasks come from the KodeKloud Engineer platform.

Task 1: Putting a line at the top of a file with Ansible

Install httpd on three app servers, start it, create /var/www/html/index.html with one line of content, then put a second line above it. Mode 0655, owned by apache.

Step 1: The playbook

The last three tasks had taught me the pattern, so this one wrote itself:

    - name: Create index.html with initial content
      copy:
        dest: /var/www/html/index.html
        content: |
          This is a Nautilus sample file, created using Ansible!

    - name: Add welcome line at the top of file
      lineinfile:
        path: /var/www/html/index.html
        line: Welcome to Nautilus Group!
        insertbefore: BOF
        mode: '0655'
        owner: "apache"
        group: "apache"
Enter fullscreen mode Exit fullscreen mode

insertbefore: BOF is the documented way to put a line at the top. The documentation for lineinfile lists it directly: "A value is available; BOF for inserting the line at the beginning of the file." No regular expression needed, no counting lines.

Step 2: Running it

PLAY RECAP ***************
stapp01 : ok=5  changed=4
stapp02 : ok=5  changed=4
stapp03 : ok=5  changed=4
Enter fullscreen mode Exit fullscreen mode

Step 3: Checking it properly

This is the part I got right today, and the part I skipped two days ago:

[tony@stapp01 ~]$ cat /var/www/html/index.html
Welcome to Nautilus Group!
This is a Nautilus sample file, created using Ansible!

[tony@stapp01 ~]$ ls -la /var/www/html/index.html
-rw-r-xr-x 1 apache apache 82 May 25 10:22 /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Content, order, mode and ownership, on all three servers. All four requirements met, all four confirmed. 82 bytes checks out as 27 plus 55, which is a small thing to notice and is exactly the kind of small thing that catches a truncated write.

So the task was done. The next bit is what I found when I asked what a second run would do.

Step 4: The two tasks undo each other

copy with content is idempotent by comparison. It reads the destination, compares it to what the playbook says the file should contain, and does nothing if they already match. lineinfile inserts its line when the line is not present.

Put those two behaviours together and trace a second run against the file that exists at the end of the first:

Step File before What the module decides File after
copy welcome line, then sample line Does not match content, which is the sample line alone. Rewrite. sample line only
lineinfile sample line only Welcome line is absent. Insert at BOF. welcome line, then sample line

The copy task deletes the line the lineinfile task added. The lineinfile task puts it back. Every run. Forever.

The file is correct when the playbook finishes, which is why this hides. But two things follow from it.

The recap never settles. Every subsequent run reports changed=2, so the changed count stops carrying information. If I ran this playbook weekly I would learn to ignore those two, and the day a real change appeared I would ignore that too.

And there is a window where the served page is wrong. Between the copy task finishing and the lineinfile task finishing, that file is missing its first line, and Apache is serving it to anyone who asks. The window is short. It is also real, and it opens on every single run.

What the docs recommend

The fix is to stop using two tasks to describe one file:

    - name: Create index.html with its full content
      copy:
        dest: /var/www/html/index.html
        content: |
          Welcome to Nautilus Group!
          This is a Nautilus sample file, created using Ansible!
        mode: '0655'
        owner: apache
        group: apache
Enter fullscreen mode Exit fullscreen mode

One task. Idempotent. No window. changed=0 on the second run.

The Ansible documentation points the same way without quite saying why. lineinfile "is primarily useful when you want to change a single line in a file only", and then: "For other cases, see the ansible.builtin.copy or ansible.builtin.template modules." A file whose entire contents you are specifying is one of those other cases. I was using a line editor to write a two-line file.

blockinfile, which I used two days ago, is the other answer and the one to reach for when something else also writes to the file. It owns a marked region and leaves the rest alone. Here nothing else writes to the file, so copy is simpler.

The task asked for the two-step approach, so the playbook follows the instructions and the instructions produce a playbook that cannot be run twice without churn. Worth knowing which of those two facts you are being graded on.

One more thing, on ownership

The mode, owner and group are on the second task, not the first. So copy creates the file as root, because the play runs with become: yes, and lineinfile is what makes it apache:apache.

Remove that second task, or reorder the two, and you get a root-owned file in a web root. That is the same bug I hit on Days 84 and 85, arriving through a different door: the permissions are not attached to the thing that creates the file.

And 0655 gives -rw-r-xr-x. Group and other get execute on a static HTML file, where execute does nothing, and apache, the owner, is the one principal that cannot execute it. 0644 is the conventional answer. The task asked for 0655, so 0655 is what I set. Two days ago, the same file was asked for at 0777. Neither is a permission a web server needs.


Task 2: Azure Table Storage

A table called tasks, two entities in it, and the two statuses verified.

Step 1: The account and the table

az storage account create -g "$RG" -n "$SA" -l eastus --sku Standard_LRS --kind StorageV2
KEY=$(az storage account keys list -g "$RG" -n "$SA" --query "[0].value" -o tsv)
az storage table create --name tasks --account-name "$SA" --account-key "$KEY"
Enter fullscreen mode Exit fullscreen mode
{ "created": true }
Enter fullscreen mode Exit fullscreen mode

No wait, no provisioning state. The table was ready for writes immediately, which is not what I had got used to.

Step 2: The entities

az storage entity insert --table-name tasks --account-name "$SA" --account-key "$KEY" \
  --entity PartitionKey=tasks RowKey=1 task="Learn Table Storage" status=completed
Enter fullscreen mode Exit fullscreen mode

And what came back:

{
  "date": "2026-10-21T03:29:38+00:00",
  "etag": "W/\"datetime'2026-10-21T03%3A29%3A39.5133745Z'\"",
  "version": "2019-02-02"
}
Enter fullscreen mode Exit fullscreen mode

No PartitionKey. No RowKey. None of the properties I sent. The insert response tells me the write was accepted and tells me nothing about what was written.

I have come to prefer that. Day 38's blob upload returned a content_md5 that the client had computed, so the service reporting it back only confirmed the service had faithfully recorded my own claim about my own file. That looks like verification and is not. Here there is nothing to mistake for verification, so the question of whether the entity is correct stays open and visible, which is where it belongs.

Step 3: Making the service check it

task1 completed -> 1
task2 in-progress -> 1
total rows -> 2
wrong-status control (expect 0) -> 0
Enter fullscreen mode Exit fullscreen mode

The first three lines come from server-side filters:

--filter "PartitionKey eq 'tasks' and RowKey eq '1' and status eq 'completed'"
Enter fullscreen mode Exit fullscreen mode

A count of 1 means a row exists with that partition, that key and that status. The service applied the predicate. I did not read any JSON and decide it looked right, which is the step where a transposed digit gets waved through.

The fourth line is the one that makes the other three worth anything.

RowKey eq '1' and status eq 'in-progress' is deliberately false. No such row exists. If I had only the three positive counts, they would look conclusive and prove very little: if a malformed filter were being ignored, or were matching everything, every filter I ran would return a positive count, and all three lines would read as success. Getting 0 back from a filter that must not match is what proves the filters actually discriminate, and that is what licenses reading the other three as real matches.

A test that cannot fail is not a test. I had used the same shape on Day 35, where the ping that failed before the peering and succeeded after was worth far more than a ping that only ever succeeded.

Which is this day's other half turned inside out. Here the check was built so that a broken check would announce itself. Over on the Ansible side, four tasks ran, the recap said success, the file was correct, and two of those tasks had cancelled each other out in a way a single run was structurally incapable of revealing. The difference was not how carefully I looked at the end. It was whether the thing I built was capable of failing in the first place.

What the docs recommend

Two notes from the documentation that change how I would write this at any size.

az storage entity query returns at most 1000 entities per request, and when there are more, nextMarker carries a continuation token you feed back with --marker. With two rows, mine came back as {}, an empty object. So a script that treats "results, no error" as "all the results" is correct at two rows and silently wrong at 1001. The limit belongs to the service, so there is no page size to raise.

And PartitionKey is the unit of both distribution and transaction scope. The task puts both rows in one partition, which is fine for two rows and is not how you would build the application. One partition means entity group transactions work and scaling does not: a single partition cannot be split across servers, so its throughput is capped. For a real to-do app, the partition would be the user ID, with the task ID as RowKey.

That also decides what a query costs. A filter naming both keys is a point query. A filter naming only PartitionKey is a partition scan. A filter naming neither is a table scan, reading every partition. My two status checks named both keys. My negative control named only RowKey, so the cheapest-looking line in that output was the most expensive command in the task. Against two rows, irrelevant. At scale, that is the query that quietly costs money.

What I learned

copy with content and lineinfile pointed at the same file undo each other on every run, because copy restores its whole idea of the file and lineinfile re-adds its line. One copy with the full content fixes it, or blockinfile if something else writes to the file too.

Two tasks describing one file also open a window where the file is wrong, and on a served page that window is live every run.

insertbefore: BOF puts a line at the top, and mode, owner and group on that task are what set ownership, so removing it leaves a root-owned file in the web root.

az storage entity insert returns a date, an etag and an API version. Verification is always a separate read.

Verify with a server-side filter and count the matches, so the service does the comparing.

Pair every positive filter with a deliberately false one. Three matches prove nothing if a broken filter would have matched them too.

entity show returns the entity at the top level; entity query nests it under items. That is the eighth variation on this trap I have hit in the Azure track, and the fix is still to run the command plain and read the shape before writing a query.

So here is the Day 91 question. When your check passes, do you know what it would have looked like if it had failed? I could answer that for the Azure half, because I had made it fail on purpose. I could not answer it for the Ansible half, and that is exactly where the bug was.

Day 91 down. Nine to go.

Top comments (0)