digitalcourage.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Diese Instanz wird betrieben von Digitalcourage e.V. für die Allgemeinheit. Damit wir das nachhaltig tun können, erheben wir einen jährlichen Vorausbeitrag von 1€/Monat per SEPA-Lastschrifteinzug.

Server stats:

860
active users

Fun question for the bubble:

I noticed that Ansible on NixOS does not find python modules that have been installed from NixPkgs. This means, modules like the kubernetes module are not found and Ansible cannot do its work, when a task is executed on localhost (e.g. via delegate_to)

Is there a way to install those additional modules together with the Ansible package?

Or is this just some messup with autodetecting the python interpreter on localhost?

Second round for the bubble:

With lots of help I now have a Ansible derivation that includes python modules for requirements from e.g. the kubernetes.core Ansible collection.

But things still do not work. Example: Creating something in a Kubernetes cluster using the kubernetes.core collection. No matter whether I target localhost in my playbook or the remote host with a delegate_to for localhost.

Ansible uses the installed python directly, but without all the module-including-voodoo that is done in the .ansible-wrapped file (that is being called when "ansible" is being invoked". And thus the kubernetes module is not found.

I also tried installing python3 and python3Packages.kubernetes in addition to ansible. I see the kubernetes in $HOME/.nix-profile/lib/python3.12/site-packages/, but it is still not found. I tried telling Ansible that localhost has a python interpreter, but no change.

Frustrating...

Third round for the bubble:

I think I have a solution! At least I got some thing working.

Having the python312 nixpkg installed is not enough. It is also not enough to have python312, python312Packages.kubernetes and python312Packages.cryptography installed.

I had to install them "in the same package":

```
home.packages = with pkgs; [
ansible
ansible-lint
(pkgs.python3.withPackages (python-pkgs: [
python-pkgs.cryptography
python-pkgs.kubernetes
]))
];
```

Then I could set the ansible_python_interpreter to `python3` and could use the kubernetes python module for using things from the kubernetes.core Ansible collection.

This is in addition to having the modules also available in the ansible derivation, no idea if that is really necessary.

I'll do some more digging and testing, but at least something worked now! I call that a win!

# NixOS

Fourth and hopefully last round for the bubble:

Short recap: Having the kubernetes module in the ansible derivation/package is not enough, it needs to also be in the python3 or python312 derivation/package.

Fun fact:
- `which python3` gives me /home/tux.penguin/.nix-profile/bin/python3
- this links to /nix/store/ipwx571dlhxmdf1n1yd8vgqq5ndjnxba-python3-3.12.8-env/bin/python3
- `which -a python3` only gives me this one python, no other binaries found in the PATH with that name

1. Using /nix/store/ipwx571dlhxmdf1n1yd8vgqq5ndjnxba-python3-3.12.8-env/bin/python3 as ansible_python_interpreter works, the kubernetes module is found
2. Using /home/tux.penguin/.nix-profile/bin/python3 for ansible_python_interpreter works
3. Using "{{ lookup('env','HOME') }}/.nix-profile/bin/python3" for ansible_python_interpreter works
4. Using just python3 DOES NOT work?!?!? What the hell? Even if this is resolved (in the shell at least) to the same python3 binary/link in ~/.nix-profile/bin/python3? What the hell?

Johannes Kastl

Hardcoding a nix path, even it is just ~/.nix-profile/bin/python3, is of course not feasible if the code is being used on other machines where this is not guaranteed to exist (as outlined in the answers yesterday).

What am I missing?