Day 92 of DevOps, Day 42 of Azure. Both tasks are about sequence. The Ansible one renders a file from a variable that is resolved before the target machine is contacted at all. The Azure one is a copy and a delete, where writing them in the natural order destroys the thing you were copying.
One Ansible task, one Azure task. A template in a role, then a container that had to be backed up before it could be removed. The tasks come from the KodeKloud Engineer platform.
Task 1: Deploying a file from a Jinja2 template
Install and start httpd on one app server through a role, then write /var/www/html/index.html from a template carrying the host's name. Mode 0744, owned by the connecting user.
Step 1: Reading what was already there
The role existed with two tasks in it, install and start. The playbook had an empty hosts: line. So the work was three edits rather than a new playbook, and the first thing worth doing was finding out which three.
Step 2: The template
This file was created using Ansible on {{ inventory_hostname }}
- name: Deploy index.html template
template:
src: index.html.j2
dest: /var/www/html/index.html
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0744'
The src has no directory on it. The file lives at role/httpd/templates/index.html.j2, and the bare filename finds it because of the role layout, not because of anything in the module. Ansible's roles documentation says that copy, script, template and include tasks in a role "can reference files in roles/x/{files,templates,tasks}/" "without having to path them relatively or absolutely".
That is worth knowing as the mechanism rather than as a convenience. Move the template out of templates/ and the same task fails with a path error.
Step 3: Checking before running it
[steve@stapp02 ~]$ sudo systemctl status httpd
Unit httpd.service could not be found.
[steve@stapp02 ~]$ ls /var/www/html
ls: cannot access '/var/www/html': No such file or directory
Not ceremony. This is what makes the three changed results afterwards attributable to this run instead of to how the lab happened to start. I skipped this on Day 88 and it cost me the ability to say anything firm about that playbook's output.
Step 4: Running it and checking again
PLAY RECAP ***************
stapp02 : ok=4 changed=3
[steve@stapp02 ~]$ cat /var/www/html/index.html
This file was created using Ansible on stapp02
[steve@stapp02 ~]$ ls -la /var/www/html/index.html
-rwxr--r-- 1 steve steve 47 May 25 10:48 /var/www/html/index.html
47 bytes is the rendered line: 46 characters plus a newline. Worth computing, because a template that rendered the wrong variable would still produce a file that looked fine at a glance.
What the docs recommend
Three things from the documentation that this task either got right or got away with.
mode: '0744' is quoted, and the template documentation asks for that: "For consistent results, quote octal numbers (for example, '644' or '1777')." Unquoted octal is read as decimal.
become_user: root in the playbook is redundant. The privilege escalation page says plainly: "The default value is root." Harmless, and the related trap is worth carrying: the same page notes become_user "Does NOT imply become: true", so a playbook that sets only the user escalates nothing at all.
And there is an idempotence note this template dodges by luck. The documentation warns that "Including a string that uses a date in the template will result in the template being marked 'changed' each time." {{ inventory_hostname }} is stable, so this task really is idempotent. Swap in a timestamp and you have Day 85's always-changed problem, in a module that looks like it should not have it.
One thing to flag for tomorrow
inventory_hostname is documented as "The inventory name for the 'current' host being iterated over in the play." It comes from the inventory file. The controller knows it before it contacts anything.
I am noting that because the next task uses ansible_nodename, which looks like it means the same thing and does not. Here they agree, because the inventory line reads stapp02 ansible_host=stapp02. Nothing requires them to.
Task 2: Backing up and deleting a blob container
Copy a container's contents to /opt on the client host, then delete the container and confirm it is gone.
Step 1: The order, which is the entire task
The natural way to write this is the wrong way:
az storage blob download-batch ... --destination /opt
az storage container delete ...
# now check /opt
Three commands that all succeed, with the check in the one position where it cannot do anything. A truncated download would be discovered after the source was destroyed.
So I checked the retention policies first:
"blobDelete": { "allowPermanentDelete": false, "days": null, "enabled": false },
"containerDelete": null
Both off. No soft delete, no recovery window, nothing to undo with. That turned "this is probably fine" into a known quantity, and it is only useful before the delete.
Step 2: Verifying the copy against a digest I did not compute
OK devops.txt bytes=33 md5=Lu7zilatbGguzSz2Ecn5IQ==
download-batch exiting zero says the client believed it finished. The check recomputes MD5 from the bytes on disk and compares it to contentMd5, which Azure recorded when somebody uploaded the blob, before this task existed.
That independence is the point. On Day 38 I compared an upload response's digest against the file I had just sent, which only confirmed the service had faithfully recorded my own claim.
I also baselined /opt first, which already held creds.json, showcreds and a .init/ directory. Without that, "I see files in /opt, the copy worked" is a guess. It also would have been the only warning if the container had held a creds.json of its own, since download-batch overwrites without asking.
Step 3: Confirming the deletion four ways
| Check | Result | What it proves |
|---|---|---|
container delete |
{"deleted": true} |
The request was accepted |
container exists |
exists: false |
The service says it is gone |
container list |
[] |
Nothing else went with it |
container list --include-deleted |
[] |
No soft-deleted copy exists |
blob list -c <container> |
ContainerNotFound |
A read actively refuses |
--include-deleted is the one that separates deleted from deleted permanently. With container soft delete on, the first four would look exactly the same and the container would still be recoverable.
And the error class carries as much as the failure. ContainerNotFound means gone. BlobNotFound would have meant the container still exists and is empty, which is a different end state entirely.
What the docs recommend
Two notes on reading responses, and one of them caught me out.
The container inspection included a lease check, because a container with an active lease cannot be deleted and the error, LeaseIdMissing, reads like a credential problem. It returned:
{ "lease": null, "name": "devops-blob-324286988", "publicAccess": null }
I read lease: null as no lease held. It means nothing of the sort. The real path is properties.lease.state. properties.leaseState, which is what I wrote, does not exist, so JMESPath returned null for a field I invented. publicAccess: null on the same line is a real null meaning private. One legitimate null and one phantom null, printed together, impossible to tell apart.
The delete worked, so there was no lease. But I learned that from the delete, not from the check I wrote specifically to learn it.
The second note is smaller and worth stealing. Asking for a possibly-absent field directly prints an empty line under -o json, which is indistinguishable from a command that produced no output. Wrapping it in an object forces the absence to print as a value:
--query "{containerDelete:containerDeleteRetentionPolicy,blobDelete:deleteRetentionPolicy}" -o json
Which is how I found that both soft-delete features were off while being spelled differently: one as an object saying enabled: false, the other as plain null. Same state, two encodings, and only one of them reachable by the obvious query.
What I learned
In an Ansible role, a bare src: file.j2 resolves against that role's templates/ directory, and the template module reads and renders on the controller rather than the target.
become_user defaults to root, so writing it alongside become: yes is noise. Writing it without become escalates nothing.
A template containing a date is marked changed on every run. One containing only stable variables is idempotent.
inventory_hostname is the inventory's name for a host, resolved without gathering any facts.
Check the retention policies before deleting anything, because they tell you whether a mistake is recoverable and they only tell you in time if you ask first.
--include-deleted is what distinguishes deleted from soft-deleted.
A null from your own --query may mean the field does not exist rather than that the value is absent.
Baseline the destination before a bulk copy. It identifies what you copied and shows you what you would have overwritten.
So here is the Day 92 question. For every check in your script, is there a version of the failure it is meant to catch that it would report as a pass? My lease check had one. It returned null for a healthy container and it would have returned null for a leased one too.
Day 92 down. Eight to go.
Top comments (0)