Hi guys
I have a role that installs Python on the target host ([0]). Different hosts require different Python libraries (some only need the basic set of libraries, others need completely different ones). My idea was to define a list of default packages in the role vars [1] and then provide the custom packages in the host_vars [2] whenever required. Unfortunately according to [3] the role vars have higher precedence over the host_vars so this explains why the custom packages in host_vars never got picked up.
My next approach was to just have an additional task in the role that installs the host specific packages after the default ones. This works as long as all hosts have those variables defined. My goal however is to only specify a list of libraries for those hosts that differ from the default list. As you can see in [4] I tried two different things to work around missing host_vars for certain hosts: either try to fall back to an empty list via default() ) or use a when statement to check whether the variable is defined in host_vars or not. Both didn’t work.
Apart from that I also tried to move the content of python.pip.pkgs from ./roles/python/vars/main.yml to ./roles/python/defaults/main.yml but that didn’t work either. Can defaults have hirarchical variables at all? It seemed like Ansible just ignored whatever I put in there.
One last thing I was looking into is include_vars but I got stuck here with two issues: I have different environments (./inventories/{prod,staging,test}) and therefore the path to the host_vars is based on the inventory I provide when calling the playbook however I couldn’t get the path or name of the selected inventory. The second issue is that I don’t know how I can skip this include when the file doesn’t exist.
Could somebody shed some light on how I can define host specific variables that can overwrite role variables for a use case as described above. I googled for this but either it is so simple that nobody cares to write about it or I’m just bad at finding the solution.
Any help is appreciated.
Cheers
[0] https://pastebin.com/raw/1XSk1t8Z
[1] https://pastebin.com/raw/R0USFkEw
[2] https://pastebin.com/raw/kTDF3zuS
[3] http://docs.ansible.com/ansible/latest/playbooks_variables.html
[4] https://pastebin.com/raw/Emmb49qL