root access

$ cat job-board-api/part-30.md

Building a Job Board API, Part 30: Deprecated Tools and a Versioned Role

Where we are

Hi, I’m Reza. In my newsletter, Root Access, I am sharing one of my projects from the very beginning: a Job Board API built with Django, Postgres, Docker, AWS and Azure. I am a junior engineer looking for my first SRE, DevOps or Cloud job, and that is why I build my projects in public. You can see how I think, what breaks, and how I fix it.

In Part 29, I built dev-toolchain, a shared Ansible role that installs a project’s local tools. A project gives it a list, the role checks the list, and then it runs one small file per tool. Running it twice must give changed=0. It already had five tools: the PostgreSQL client, redis-cli, Docker, uv and Python 3.12.

This part has two halves:

  1. The last five tools: the AWS CLI, LocalStack, awslocal, pre-commit and Azurite. Two of them turned out to be deprecated, so the role installs their replacement instead.
  2. Publishing the role on GitHub as v0.1.0, and making the Job Board API depend on it, like any other dependency.
The project pins the role to a tag, then runs it

The project never copies the role. It names a version, downloads that version, and runs it.

The AWS CLI: the first real install

In Part 29, thor already had every tool, so the role only skipped. The AWS CLI was different: it was missing on thor, and the role installed it from scratch.

Check for v2, not just “aws”

- name: "awscli | check for AWS CLI v2"
  ansible.builtin.command: aws --version
  register: toolchain_aws
  changed_when: false
  failed_when: false
- name: "awscli | decide whether to install"
  ansible.builtin.set_fact:
    toolchain_aws_missing: "{{ toolchain_aws.rc != 0 or 'aws-cli/2' not in (toolchain_aws.stdout + toolchain_aws.stderr) }}"
  • set_fact creates a variable during the run. Here it gives one decision a readable name.
  • “Missing” means either: aws isn’t there, or it is there but it isn’t v2, for example the old v1 from pip.
  • Why stdout + stderr? v2 prints its version to stdout, but the old v1 printed it to stderr. Looking at both means a v1 is detected correctly.

Four install steps, one condition

- name: "awscli | install AWS CLI v2 (official installer)"
  when: toolchain_aws_missing | bool
  block:
    - name: "awscli | unzip is needed by the installer"
      ansible.builtin.apt: { name: unzip, state: present, update_cache: true, cache_valid_time: 3600 }
      become: true
    - name: "awscli | download the installer for {{ ansible_facts['architecture'] }}"
      ansible.builtin.get_url:
        url: "https://awscli.amazonaws.com/awscli-exe-linux-{{ ansible_facts['architecture'] }}.zip"
        dest: /tmp/awscliv2.zip
        mode: "0644"
    - name: "awscli | unpack it"
      ansible.builtin.unarchive: { src: /tmp/awscliv2.zip, dest: /tmp, remote_src: true }
    - name: "awscli | run the installer (/usr/local/bin/aws)"
      ansible.builtin.command: /tmp/aws/install --update
      become: true

(I wrote some tasks in short form here to save space. In the role, each one is written out in full.)

  • block: with one when: groups the four steps. They can’t get out of sync, and the output shows them as one unit.
  • | bool. set_fact with a template stores the result as text (“True”). The bool filter turns it back into a real boolean, so when: reads it correctly.
  • unzip is needed to open the .zip, and minimal Ubuntu doesn’t always have it.
  • AWS names its files with Linux’s own architecture names (x86_64, aarch64), so the fact goes straight into the URL.
  • remote_src: true means the archive is already on the target. Without it, unarchive would look for the file on the controller. On localhost that is the same machine, but it matters when the role runs over SSH.
  • –update lets the installer succeed even if an older v2 is there.
  • Not pinned, on purpose. This URL always serves the newest v2. AWS also publishes versioned files, but the official instructions use this one, and the CLI changes little between versions.

The proof that the install path really ran: /tmp/awscliv2.zip (75,050,619 bytes) and /usr/local/bin/aws -> /usr/local/aws-cli/v2/current/bin/aws, both created at 20:57. And the version: aws-cli/2.40.2.

LocalStack and awslocal: both deprecated

My first version of the role installed the Python-based localstack CLI and awslocal, as uv tools. It worked, and the second run gave changed=0. But the version check printed this:

The ‘localstack’ CLI is deprecated. Use the ‘lstk’ CLI instead. The ‘localstack’ CLI no longer receives updates.

And awslocal said:

‘awslocal’ is deprecated. Use ‘lstk aws’ instead.

The second warning only showed in full after I reran with head -n 15, because tail -n 3 had cut it off.

I had three options: switch to lstk, keep the old CLI, or drop the CLI completely, since LocalStack runs from Compose anyway. I chose lstk. Installing a tool that no longer gets updates is not a good start for a new role.

What lstk is

lstk is LocalStack’s current CLI. It manages LocalStack (start, stop, status, logs, snapshots), and lstk aws … runs AWS CLI commands against it, with the endpoint, credentials and region already set.

The variables

toolchain_lstk_version: "1.4.5"
toolchain_lstk_arch: "{{ {'x86_64': 'amd64', 'aarch64': 'arm64'}[ansible_facts['architecture']] }}"
toolchain_lstk_url: "https://github.com/localstack/lstk/releases/download/v{{ toolchain_lstk_version }}"
toolchain_lstk_dir: "{{ ansible_facts['user_dir'] }}/.local/share/lstk/{{ toolchain_lstk_version }}"
  • 1.4.5 was the latest release. I read it from GitHub’s API on thor, together with the release’s real file names, so the download URL isn’t guessed.
  • toolchain_lstk_arch is a small dictionary indexed by the machine’s architecture. Linux says x86_64, but lstk’s files say amd64. On any other architecture, the lookup fails loudly instead of downloading the wrong file.
  • The folder includes the version. An upgrade unpacks into a new folder and moves a link. The AWS CLI uses the same layout.

A verified download

- name: "localstack | download the release, checked against its checksums.txt"
  ansible.builtin.get_url:
    url: "{{ toolchain_lstk_url }}/lstk_{{ toolchain_lstk_version }}_linux_{{ toolchain_lstk_arch }}.tar.gz"
    dest: "/tmp/lstk_{{ toolchain_lstk_version }}_linux_{{ toolchain_lstk_arch }}.tar.gz"
    checksum: "sha256:{{ toolchain_lstk_url }}/checksums.txt"
    mode: "0644"

checksum: “sha256:<URL>” is a nice get_url feature. It downloads the release’s checksums.txt, finds the line for this file, and refuses the download if the SHA-256 doesn’t match. That protects against a corrupted or tampered file. I checked it before running on thor: the line for lstk_1.4.5_linux_amd64.tar.gz matched the archive.

lstk is a folder, not a single binary

lstk can’t be installed as a single binary. While testing, I found that the release also ships bundled-extensions and a file called lstk-extensions.toml, which says “lstk reads this file to learn which bundled commands exist”. With only the binary, lstk would lose its bundled commands. So the role unpacks the whole release into ~/.local/share/lstk/1.4.5/, and then makes a link:

- name: "localstack | put lstk on the PATH (~/.local/bin/lstk -> version {{ toolchain_lstk_version }})"
  ansible.builtin.file:
    src: "{{ toolchain_lstk_dir }}/lstk"
    dest: "{{ ansible_facts['user_dir'] }}/.local/bin/lstk"
    state: link

This task is outside the install block on purpose. If someone deletes the link, the next run puts it back, even though lstk itself is still installed.

awslocal now means “AWS CLI + lstk”

---
# 'awslocal' is deprecated by LocalStack: its replacement is `lstk aws`, which needs
# the AWS CLI on the PATH. So this tool = the AWS CLI + lstk.
- name: "awslocal | needs the AWS CLI (lstk aws runs it)"
  ansible.builtin.include_tasks: tools/awscli.yml
- name: "awslocal | provided by lstk (use: lstk aws ...)"
  ansible.builtin.include_tasks: tools/localstack.yml
  • Projects can still ask for awslocal, the name in my spec, and they get what replaces it.
  • The AWS CLI comes first. While testing, lstk aws without it printed Error: aws CLI not found in PATH.
  • No awslocal command is created. A shim script with that name would hide the deprecation. And my project is not affected: its LocalStack init scripts call awslocal inside the LocalStack container, which ships its own copy.
  • The role only installs things. It never removes them. So I removed the old CLIs by hand, once: uv tool uninstall localstack awscli-local.

pre-commit and Azurite

pre-commit is a uv tool:

- name: "pre-commit | make sure uv is installed"
  ansible.builtin.include_tasks: tools/uv.yml
- name: "pre-commit | install pre-commit as a uv tool"
  ansible.builtin.command: "{{ toolchain_uv }} tool install pre-commit"
  args:
    creates: "{{ ansible_facts['user_dir'] }}/.local/bin/pre-commit"

creates: makes it idempotent. One note: installing the tool is not the same as using it. A project still needs a .pre-commit-config.yaml and pre-commit install. That is a project decision, not a toolchain one.

Azurite runs as a container in my project. So “having Azurite” means having Docker and the image:

toolchain_azurite_image: "mcr.microsoft.com/azure-storage/azurite:3.37.0"
- name: "azurite | needs Docker (Azurite runs as a container)"
  ansible.builtin.include_tasks: tools/docker.yml
- name: "azurite | is the pinned image already pulled?"
  ansible.builtin.command: "docker image inspect {{ toolchain_azurite_image }}"
  register: toolchain_azurite
  changed_when: false
  failed_when: false
  become: true
- name: "azurite | pull {{ toolchain_azurite_image }}"
  ansible.builtin.command: "docker pull {{ toolchain_azurite_image }}"
  when: toolchain_azurite.rc != 0
  become: true
  • The same tag as docker-compose.yml. If the two were different, the toolchain would pull one image and Compose would pull another.
  • become: true on both. On a fresh machine, the user was just added to the docker group, but that only works after the next login. Root can always reach Docker.
  • Why not npm’s azurite package? It needs Node.js, which nothing else here uses, and my project never runs Azurite outside Docker.

Results for the five tools

  • Run 1 (with lstk): ok=46 changed=5 failed=0 skipped=18. lstk was downloaded (checksum OK), unpacked and linked.
  • Run 2: ok=43 changed=0 failed=0 skipped=21. Idempotent.
  • lstk –version: lstk 1.4.5, and ~/.local/bin/lstk -> /home/reza/.local/share/lstk/1.4.5/lstk.
  • pre-commit 4.7.0, and the Azurite image was already there from Phase 6.
  • The real test, with my project’s LocalStack running: lstk aws s3 ls showed the 00-job-board-api bucket, and lstk aws sqs list-queues showed job-board-status and job-board-status-dlq. It works against my Compose LocalStack, without lstk start.

Problem: 5 changes where I expected 4

Run 1 with lstk reported 5 changes. I expected 4. My suspect was this task, which made sure ~/.local/bin exists:

- name: "localstack | make sure ~/.local/bin exists (never changes an existing folder's mode)"
  ansible.builtin.file:
    path: "{{ ansible_facts['user_dir'] }}/.local/bin"
    state: directory

The first version had mode: “0755”. But ~/.local/bin is a shared folder: uv, pipx and other tools use it. The role must not change its permissions. stat showed 755 afterwards, which can’t prove whether the role changed it or not.

So I removed mode:. Without it, an existing folder is left exactly as it is, and a missing one is created with default permissions. The run after the fix: changed=0.

To be honest, the 5th change stays unexplained. But the fix is right either way: a shared folder is not the role’s to re-permission.

The limits of my testing

Before running on thor, my test environment couldn’t reach awscli.amazonaws.com or run Docker. So the AWS CLI install and the Azurite pull were first proven on thor. The lstk download and the checksum check were tested beforehand.

One more thing: some output went missing during these runs, like the GitHub API result and the counts at the end of a run. Each time, I ran the missing part again by itself before drawing any conclusion.

Publishing the role as v0.1.0

The role now has 10 tools, covering all 8 in the Setup list, plus docker and uv as separate entries. Time to publish it.

First, a README. It says what the role does, the 10 supported tools and how each is installed, how a project uses it, how to test it, and its requirements. It also says awslocal is deprecated; use lstk aws, so nobody expects an awslocal command.

gh repo create mohammadrezachegini/ansible-role-dev-toolchain --public --source=. --push \
  --description "Ansible role: installs a project's local dev tools on Ubuntu, idempotently."
  • One command creates the GitHub repo, adds it as origin, and pushes the current branch.
  • –public, because ansible-galaxy clones over HTTPS without credentials. A private repo would need a token on every machine.
git tag -a v0.1.0 -m "dev-toolchain v0.1.0: 10 tools, idempotent, lstk instead of deprecated LocalStack CLIs"
git push origin v0.1.0
  • git tag -a makes an annotated tag, with its own message, author and date. A plain git tag is just a name for a commit.
  • v0.1.0 follows semantic versioning. A 0.x version says “early, may change”. 1.0.0 can come once more projects have used it.
  • Tags are pushed separately. A plain git push doesn’t send them.

The project depends on the role

Now the Job Board API gets four new files.

requirements.yml: which role, from where, which version

---
# Ansible roles this project depends on. Install with:
ansible-galaxy role install -r requirements.yml
roles:
  - name: dev-toolchain
    src: https://github.com/mohammadrezachegini/ansible-role-dev-toolchain.git
    scm: git
    version: v0.1.0

This is Ansible’s version of pyproject.toml + uv.lock, for roles.

  • name: dev-toolchain is the folder name it is installed as. Without it, the folder would be called ansible-role-dev-toolchain.
  • scm: git tells Galaxy to git clone it, instead of looking it up on galaxy.ansible.com. So I don’t need a Galaxy account.
  • version: v0.1.0 is the key line. The project always gets this code, even after the role changes on main. Without a version, two machines bootstrapped a week apart could get different roles.

ansible.cfg and inventory.ini

[defaults]
# Roles from requirements.yml are installed here (git-ignored: a downloaded dependency).
roles_path = roles
# This machine, with Ubuntu's Python (it has python3-apt, which the apt module needs).
inventory = inventory.ini
localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python3
  • Ansible reads ansible.cfg from the current folder automatically. That is why you run the commands from the repo root.
  • roles_path = roles. ansible-galaxy installs roles here, and ansible-playbook looks for them here. One setting keeps the two in sync. The default, ~/.ansible/roles, is shared by every project on the machine, so two projects pinning different versions would overwrite each other.
  • inventory = inventory.ini. I found this one while testing beforehand. Without an inventory, Ansible prints No inventory was parsed, only implicit localhost is available. Worse, the implicit localhost runs modules with the controller’s Python, which here is uv’s. It has no python3-apt, so the apt tasks would fail. That is the same trap as in Part 29.

bootstrap.yml: the 9 tools this project needs

---
# Readies a machine for this project (Ubuntu). Run from the repo root:
#   ansible-galaxy role install -r requirements.yml
#   ansible-playbook bootstrap.yml -K
- name: Install the Job Board API's local toolchain
  hosts: localhost
  connection: local
  roles:
    - role: dev-toolchain
      vars:
        toolchain_tools:
          - python3.12
          - docker
          - postgresql-client
          - redis-cli
          - awscli
          - localstack
          - awslocal
          - azurite
          - pre-commit
  • The comment is the usage, visible to anyone who opens the file.
  • The list is the spec’s Setup list, in the spec’s order. uv is not listed, because python3.12, localstack and pre-commit each include it. It is still installed, as a dependency instead of a request.
  • One tool per line, so adding or removing a tool is a one-line diff.

.gitignore

grep -qx "roles/" .gitignore || echo "roles/" >> .gitignore

roles/ is downloaded from requirements.yml, so it isn’t committed, just like .venv/. A committed copy would drift from the pinned tag. grep -qx matches the whole line, so the line is only added once.

Results

  • ansible-galaxy role install -r requirements.yml: dev-toolchain (v0.1.0) was installed successfully, into the project’s roles/ folder. That message also proves the publishing worked: it is only possible if the repo is public, pushed, and the tag exists on GitHub.
  • ls roles/dev-toolchain: README.md defaults meta tasks tests. That is the published repo, including the new README.
  • The same install again: dev-toolchain (v0.1.0) is already installed, skipping. Galaxy installs are idempotent too.
  • ansible-playbook bootstrap.yml -K, run 1: ok=40 changed=0 failed=0 skipped=20, with no warning lines.
  • Run 2: the same, changed=0.
  • git status –short: the four new files and .gitignore, and no roles/.

Why is run 1 already changed=0? thor already had every tool. The point of this check was not installing. It was proving that the GitHub copy of the role, not my working folder, runs cleanly from the project.

Known limitations

  • Changing the role doesn’t change this project. It only changes when requirements.yml gets a new version: and the role is reinstalled with –force. That is on purpose: the pin is the point.
  • “One command” is currently two. The role must be on disk before Ansible can read a playbook that uses it. I will decide how to wrap them at the Done-when check.
  • The role assumes the machine’s apt sources work. While testing, I found that one broken repository makes update_cache fail (Failed to update apt cache after 5 retries), and every apt task with it. Fix the source, or remove it, then rerun.
  • The fresh-machine paths for uv, Python and Docker from Part 29 are still unproven. They get their test on a clean Ubuntu 26.04 machine.

What I learned

  • Read the warnings, all of them. Two of my tools were deprecated, and tail -n 3 hid one of the warnings.
  • Follow upstream deprecations instead of installing tools that no longer get updates.
  • Verify what you download. get_url with a checksum: URL refuses a file that doesn’t match.
  • Versioned install folders plus a link make upgrades clean.
  • Don’t own shared folders. state: directory without mode leaves an existing folder alone.
  • A shared role is a dependency. Pin it to a tag, download it into the project, and keep the download out of git.
  • Use an explicit inventory. The implicit localhost warns, and it uses the wrong Python.

What is next

This completes Setup checklist item 1: bootstrap.yml with the dev-toolchain role, covering all 8 tools in the spec. Next, I add a verify task, so the role doesn’t only install the tools, it also proves they work.

$ ls -t root-access/ | head -3

Building a Job Board API, Part 31: Installed Is Not the Same as Working job-board-api · 31/31 10 min Building a Job Board API, Part 29: One Ansible Role for Every Project job-board-api · 29/31 14 min Building a Job Board API, Part 28: One Image, Two Clouds job-board-api · 28/31 10 min

Leave a comment

Your email address will not be published. Required fields are marked *