Introducing alai - Collection Manager for Ansible

In the past few weeks I’ve been working on alai which is a tool that helps manage the collections an Ansible project needs. It is heavily inspired by uv, so if you’ve used uv for Python, then alai should hopefully feel right at home.

A note upfront, alai was made with the help of AI tools. It hasn’t just been
thrown over the fence, I’ve put in a lot of time to think about the general
architecture, CLI user experience, and more. I understand that for some
people this turns them off a project which is a totally respectable
position.

This is also not an official Ansible/Red Hat product. It is something I’ve been working on in my own time and has no support or official connection to Red Hat. Do not expect support for this at any time as part of any contract/agreement with Red Hat.

Why another tool?

https://galaxy.ansible.com/ and ansible-galaxy are the defacto server and client tools that people use today for managing collections with Ansible but they leave a few gaps that alai has tried to fill.

The biggest one is speed, Galaxy just isn’t designed to be fast and by creating our own index format we can dramatically reduce the time it takes for installing a collection. For example community.general takes nearly 15 seconds with ansible-galaxy, on a good day, but with alai it drops down to 1-2 seconds.

ansible-galaxy and alai each installing community.general with nothing cached, played side by side from the same start: alai finishes and installs it again into another project while ansible-galaxy is still running

Adding more collections will slow down ansible-galaxy even further due to the number of API requests that need to be made to the server.

Not only that, with caching and other optimisations, other projects can automatically reuse artifacts that another project has already downloaded trimming down the time even more.

Some of the other benefits that alai brings to the table are:

  • Lock file support: Makes it dead simple to recreate an environment with the exact same collection versions, dependencies included.
  • Project scoped collections: Collections specified in a project mean you no longer have system wide collections or hidden dependencies overriding the collections your playbook sees. This keeps things truly isolated from any system changes and dependency hell
  • No Ansible requirement: A single tool can setup your environment without actually requiring Ansible to be present or installed
  • Collection development: You no longer need to clone a local collection to ansible_collections/ns/name/ to use it with Ansible. There are some known bugs with ansible-test with this mode that I’m hoping to improve going forward.
  • Plugin locator: With alai find ..., you can easily find collections that contain a plugin based on the name or other wildcard matches.

I’ve also tried to create some other CLI tools like:

  • alai-doc: TUI for reading plugin documentation.
  • alai-index: CLI for managing your own index.

Trying it out

alai is published on PyPI as alai-sh. The
easiest way to install it is through uv as a tool.

$ uv tool install alai-sh
$ alai --version
alai 0.1.0

Using uv tool keeps alai in its own environment isolated from everything else. Once installed as a tool alai can be run just like any other command.

An alai.toml marks the project and its root, so the first step when using alai is to create one beside your playbook or in your collection root:

$ touch alai.toml
$ alai add ansible.posix 'community.general>=10,<12'
Resolved 2 collections in 522ms
Prepared 2 collections in 1.21s
Installed 2 collections in 41ms
 + ansible.posix==2.2.2
 + community.general==11.4.9

You should commit alai.toml and alai.lock but ignore collections/ as alai manages that directory. Anyone who clones the project can then get the exact same dependencies with alai sync.

To ensure Ansible can see the collections you have in your project, run all Ansible commands with alai run ...:

$ alai run ansible-playbook site.yml -i inventory.ini

A few other commands worth knowing:

Task Command
Add a collection only the tests need alai add --group test ansible.utils
Remove a collection alai remove cisco.ios
Upgrade one collection, or everything alai lock -P cisco.ios, alai lock -U
Install a collection outside the lock alai collection install ansible.posix

The migration guide covers some of the common use cases and how to use them with alai instead.

Things to know

  • alai is considered in preview. The interface, CLI commands, etc can change.
  • alai runs on Linux and macOS, no Windows support because Ansible itself cannot run on Windows.
  • Collections come from an index. The default index (https://index.alai.sh/) is a mirror of Galaxy that syncs regularly. You can run your own index with alai-index.

Links

The roadmap links to issues for what I’m thinking about next, such as requirements.yml support, building and publishing collections, and managing ansible itself. Comments on those issues are very welcome, and so is any feedback here.

6 Likes

More nice user and developer tools for ansible ecosystem for the win!

I like the lock files feature.
I did not quite get how to use ansible from uv managed venv to run playbooks?
How to manage collection dependencies for different python/ansible-core versions. E.g. I am running some tests for all supported ansible-core versions of my collections and collection dependencies are different so I manage them inside uv venv. How to do that with alai?

There’s no uv or Ansible management yet. I’ve got an issue placeholder to talk about it some more but I need to put in the work to actually write down my thoughts Managing Ansible and Python Dependencies · Issue #68 · alai-sh/alai · GitHub. I’ve definitely been thinking about it and really wanted to include it as part of this initial release but unfortunately it’s an extremely complicated subject and I really want to do it right and not just chuck something over the fence.

Alai can specify a range for ansible_requires so that the collections it downloads match the range but it has no concept of running Ansible or selecting a version. That’s part of the feature/roadmap item I linked to above. You’ll still have to use a separate tool, like uv, or anything else you use today, to install and manage Ansible versions in your testing.

Hey @jborean

This looks really cool, I’ll need to check this out when I get some time.

How does alai handle if collections/ansible_collections directory already exists?

ansible-creator init playbook command seeds a playbook project with collections/ansible_collections/<namespace>/<name>/ - Is it compatible with that?

Do you have plans to support specifying the configuration for alai in a pyproject.toml file? Similar to:

# pyproject.toml
...
[tool.alai.project]
collections = [
    "ansible.posix>=2.2.2",
    "community.general>=10,<12",
]
1 Like

If using alai add/remove/sync (essentially lock files) then it will remove any existing collection in there and only sync what is in the lock file. The recommendation from alai is to keep collections stored in the “playbook project” in a separate directory and either add them as an editable source or just a normal directory source Collection sources - alai. This way alai sync will automatically setup the collection into collections/... when it syncs.

There are three alternatives you can use if you don’t like this though and prefer to keep this custom one in collections/. The first one would be to use a custom project.collections_dir in your configuration (alai.toml, env var, CLI arg) so that you have collections/... being your manual ones and another directory managed by the lock file. Running Ansible with alai run ansible-playbook ... will ensure that both the collections/ and alai managed collections directory are seen as runtime by Ansible.

Another is to use the --inexact arg with alai sync to have it keep the existing collections in place. I wouldn’t recommend this because you need to remember to set this on every call and it is too easy to not to and then loose your local collection.

The final option is to just not use lock files and the concept of an alai project. Using alai collection install|uninstall can manually install/uninstall collections into a directory of your choice while keeping things in place.

This reflects how uv works with a virtual env where sync ensures what is there is what is in the lock file. It’ll blow away any custom changes to ensure your environment matches.

There are no concrete plans but it is a good idea for helping to remove an extra file and might play nicely into how I want to support managing Ansible and Python dependencies themselves. Thank you for suggesting it and I’ll play around with it a bit more.

1 Like

@dbrennand I’ve opened alai.toml config in pyproject.toml · Issue #77 · alai-sh/alai · GitHub to document this new feature and have opened a PR. Support for specifying the alai settings inside pyproject.toml should be coming in the next version after this. Appreciate the suggestion for this.

1 Like