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?!