I have an issue with Ansible due to how it runs a pure Nix Python which lacks the pyyaml module that normally would be installed on any other system, and for that reason a local_action calling a script which does import yaml fails with ModuleNotFoundError.
I tried this:
{
# HACK: Fix for Ansible not having PyYaml available.
python38Yaml = pkgs.python38.withPackages (ps: [ ps.pyyaml ]);
ansible = python38Yaml.pkgs.toPythonApplication pkgs.python38Packages.ansible;
}
But still no cigar. The Python used is still not the one with pyyaml:
This is more done for alternative python interpreters (e.g. Pypy)
EDIT:
One issue with just using the python package is that you no longer have access to the other ansible versions. So you will have a less āstableā environment
My understanding is that when i try to use local_action(or any other way of running things locally) Ansible uses only itās own interpreter without any additional packages to run the script.
I might add that this way of running local python scripts totally works on any other OS, like Ubuntu, and python picks up yaml module just fine. Itās a weird edge case.
I wish I could. Too much infra already using it, and Iād love to port everything to NixOS/NixOps, but thereās just too much work with new stuff to spend time porting everything else =].
Thatās cool, but Iām not working on a derivation. Maybe I can explain whatās happening:
I have a python script that I want to run a the beginning of a playbook to check versions of Ansible roles against the requirements.txt file because Ansible or Ansible galaxy doesnāt actually check that(stupid, I knowā¦). And I have a simple script that does that:
But to run it locally I need yaml modules to parse requirements.txt(which is actually yaml despite the .txt extensionā¦). Since Iām on NixOS and install Ansible via NixOS it uses the interpreter directly without any packages and is missing yaml, which makes the script fail.
Itās not a huge issue, and I guess Iāll just rewrite the script to parse requirements.txt āby handā, but this is annoying :D.
But it looks like Ansible just doesnāt inherit the $PYTHONPATH because it just fails with ModuleNotFoundError. And I checked that PYTHONPATH is set when I call ansible-playbook.
I think this is hopeless and the simplest way is to just not use yaml module.
By using ansible_env I can access the environment variables of the host from which Ansible is being run, and hence set it for the task. Not pretty, but it does work.
I donāt think this is the nixos-way to do it. I stumble accross the same Problem.
IMHO - The problem lays in the provided ansible-base package. Which uses python as an reference.
Iām to new to nixos to realy understand how ansible is build here - but IMHO there should be a way to enhance ansible-core by a setup-hook by the packages you need to have ansible to run. Python - NixOS Wiki
Or am i wrong?
But iām unsure on how to archive this.
Setting the python interpreter in the ansible-files seems really wrong?!