Day 86 of DevOps, Day 36 of Azure. Both tasks today were about a setting whose scope is not where you wrote it.
In the Ansible half, that is the feature. One line in a playbook produced three different file owners in a single run, because the value comes from the inventory and the inventory is per host. In the Azure half, it is the hazard. A filter that reads like it names one container is a prefix on a policy that belongs to the entire storage account, and leaving the filter out means the rule deletes everything.
One Ansible task, one Azure task. Create a file with the right permissions and owner on three servers, then set a lifecycle rule that deletes blobs after seven days. The tasks come from the KodeKloud Engineer platform.
Task 1: Three servers, three owners, one line
The task: create /tmp/nfsshare.txt on all three app servers with mode 0644, owned by that server's own user.
Step 1: The playbook
---
- hosts: app_servers
become: yes
tasks:
- name: Create empty file with proper permissions
file:
path: /tmp/nfsshare.txt
state: touch
mode: '0644'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
[tony@stapp01 ~]$ -rw-r--r-- 1 tony tony 0 /tmp/nfsshare.txt
[steve@stapp02 ~]$ -rw-r--r-- 1 steve steve 0 /tmp/nfsshare.txt
[banner@stapp03 ~]$ -rw-r--r-- 1 banner banner 0 /tmp/nfsshare.txt
{{ ansible_user }} is the inventory variable from Day 82, read back inside the playbook. The inventory sets it per host, so one line resolves to three different values in one run.
Which is the clearest argument yet for the separation Day 83 described. The playbook describes the shape of what should exist. The inventory supplies the particulars. Neither file duplicates the other, and adding a fourth server changes one line in one file.
It also means this playbook only works against an inventory that sets ansible_user explicitly. A host relying on the default, which is whoever is running ansible-playbook, has nothing to interpolate, and the task fails with an undefined variable rather than quietly picking something. Worth knowing because that failure happens at template time, not when the file is created.
Step 2: This is the day the ownership question gets answered
Days 84 and 85 both created root-owned files, and neither mentioned it. The reason is in the module's defaults: owner unspecified "uses the current user unless you are root, in which case it can preserve the previous ownership", and a file that did not exist has no previous ownership to preserve. With become: yes, that is root every time.
Two lines fix it, and this is the first of the three days to write them.
What the docs recommend
Three things, and the first I got right for the documented reason.
mode: '0644' is quoted, which is what the documentation asks for: "For consistent results, quote octal numbers (for example, '644' or '1777') so Ansible receives a string and can do its own conversion from string into number."
The warning attached to it is about the unquoted form: "Adding a leading zero (for example, 0755) works sometimes, but can fail in loops and some other circumstances." Quoted, the leading zero is harmless. Unquoted, YAML gets to decide what 0644 means, and that is the failure being described.
The second is an assumption the playbook makes and does not state. group: "{{ ansible_user }}" asks for a group named tony, steve and banner. All three existed, so it worked, and that is a property of how those accounts were created rather than anything the playbook establishes. A new user on a RHEL-family system gets a private group of the same name by default, which is why the listing shows tony tony. On a system where users share a group such as users, this same playbook succeeds on the owner and fails on the group.
The task's wording, that ownership and group "must be that of the users of the respective app-servers", reads as one requirement and is two lookups against two different databases. Omitting group entirely is closer to what it means, because the system then resolves the user's actual primary group whatever it happens to be called.
Third, this playbook is still not idempotent, and now for exactly one reason. mode, owner and group are comparisons: the module reads the current value and reports a change only if it differs, so all three settle after the first run. state: touch defaults both timestamps to now, as Day 84 covered, so a second run reports changed=1 on all three hosts and the only thing that changed is two timestamps nobody asked about.
Three days, three playbooks, and the pattern is clear enough to state. file is the only module in this batch that is not idempotent by default, and it is the one I used twice.
Task 2: A rule that deletes blobs after seven days
The task: a lifecycle rule on a container, deleting blobs seven days after their last modification.
Step 1: There is no such thing as a rule on a container
Azure has one management policy per storage account. Container scoping happens inside the rule:
"filters": {
"blobTypes": ["blockBlob"],
"prefixMatch": ["nautilus-container21738529"]
}
prefixMatch is a blob path prefix, and because a blob's full path begins with its container name, the container name alone scopes the rule to that container. More specific prefixes work too, so adding /logs/ would hit only that virtual folder, which is the flat-namespace behaviour from Day 18.
Leave prefixMatch out, and the rule applies to every blob in the account. For a delete action, that is the difference between tidying one container and emptying a storage account on a seven-day rolling basis.
Which is the Ansible half inverted. There, one line resolving differently per host is the feature. Here, a filter that looks like it names a container is a prefix on an account-wide policy, and the scope is wider than the place you wrote it.
Azure's own documentation signals this trips people up without explaining the mechanism. The configuration page's only mention of prefixes is a link to an FAQ entry named for the symptom: "The blob prefix match string didn't apply the policy to the expected blobs".
Step 2: The policy replaces, and that is the service
az storage account management-policy create --account-name $SA -g $RG --policy '{"rules":[...]}'
create sets the entire policy. I had written this up as CLI behaviour, and it is stated more strongly than that, as a property of the service: "You must read or write a lifecycle management policy in full. The service doesn't support partial updates."
So adding a second rule later means reading the current policy, appending to its rules array, and writing the whole thing back. Running create again with just the new rule silently removes the first one.
Third instance of the same pattern in this challenge:
| Day | Operation | Replaces |
|---|---|---|
| 12 | az resource tag |
All tags |
| AWS 46 | put-bucket-notification-configuration |
All notifications |
| 36 | management-policy create |
All lifecycle rules |
Step 3: Modification is not creation
| Condition | Clock starts at |
|---|---|
daysAfterModificationGreaterThan |
Last write to the blob |
daysAfterCreationGreaterThan |
Blob creation |
The task said "7 days of last modification", so the first. They diverge for anything rewritten: a blob overwritten weekly never ages past seven days under the modification rule, and would be deleted on schedule under the creation rule. For a retention policy, that distinction decides whether active data survives.
Worth adding that delete is not the only action available. tierToCool, tierToCold and tierToArchive move blobs to cheaper storage rather than destroying them, which is usually the better first move if the goal is cost. This task asked for deletion.
What the docs recommend
Here is the thing I cannot do, and would rather say than imply otherwise.
"Azure Storage runs the policy once per day", and the blob is seven days from being eligible. So there is no way to observe this rule working inside a one-hour lab, or any reasonable session.
Which makes this a genuine exception to the rule these notes keep arriving at. Day 28 failed because behaviour was proven and the configuration was wrong. Day 38 failed because the configuration was right and behaviour was never tested. The resolution both times was to check both.
Here behaviour is unobservable on a human timescale. So verification was configuration only, read back from the service rather than trusting the create response, with each requirement pulled out as its own field so a missing one is visible:
{
"Name": "nautilus-del-rule",
"Enabled": true,
"DeleteAfterDays": 7.0,
"Prefix": ["nautilus-container21738529"],
"BlobTypes": ["blockBlob"]
}
Saying "the rule works" would be an inference dressed as a result. What would strengthen it in a real environment is the lifecycle management log category, which records what each rule matched and deleted on each run.
Two smaller findings. DeleteAfterDays came back as 7.0 having been sent as 7, because the API models it as a number rather than an integer, so a scripted check needs a numeric comparison and not a string one. And two claims from my own notes I could not source this time, so they are marked unverified rather than repeated: that the first run after a policy change can take up to 48 hours, and that lifecycle management requires a StorageV2 account.
What I learned
Scope, twice, pointing in opposite directions.
The Ansible playbook is the version where this works for you. One line, three results, because the value lives in the inventory and the inventory knows about hosts. That is a tool designed so the thing you write once stays written once.
The Azure rule is the version where it works against you. The task says container, the resource is an account, and the only thing standing between "delete blobs in this container" and "delete everything in this storage account" is a filter that is easy to leave out and that the documentation barely explains. It runs once a day, it does not ask, and if soft delete is off, which is the default, there is no undo.
And the honest part of today is the bit I could not finish. The rule's configuration is verified exhaustively, and its behaviour is not verified at all, because nobody can watch a seven-day timer in a one-hour lab. Every other day in this series ends with a check that something actually happened. This one ends with a careful description of what was configured and a sentence saying which kind of check was possible.
So here is the Day 86 question. Which of your scheduled deletion rules has anyone watched actually run?
Day 86 down. Fourteen to go.
Top comments (0)