Host_collections and ansible variables

Problem:
How can I use the host_collection as matcher within an foreman ansible variable definition?

I have tried a lot of things but nothing worked (like foreman_host_collections or foreman::foreman_host_collections or foreman.foreman_host_collections as Attribute type).

Have anybody used this (or any other foreman fact) as matcher for selecting different values for a specific ansible variable?
hostgroup works fine but I would like to use another grouping option too

Best regards

Frank

Iā€™m afraid it currently does not work. Even basic inheritance seems broken to me on my nightly setup. @nofaralfasi could you please take a look if you can override variable e.g. based on host group or subnet? If this worked, potentially one could add a new matcher host_collection and it may just work.

@Marek_Hulan We are using basic inheritance for all of our Ansible variables, in the production release of F/K (3.5.1/4.7.2) it works fine. We set global defaults and override them based on host groups and location.
We are not using host collections, so I canā€™t answer to the original topic.

just for information: if I set a local ansible fact on the host, I can access it (like ā€˜ansible_local::preferences::general::collectionā€™ (file under /etc/ansible/facts.d/preference.fact with [general]collection=xxx)) . In my opinion, it just does not work with the foreman facts

Greetings

Frank

I need to look if this is broken on nightlies or just my setup then. Do you see the overrides correctly on the new host detail page then? (Ansible ā†’ Variables tab and sub-tab). If that works, one should be able to add ā€œhost_collectionā€ as a new matcher per ansible variable. Then the value should be able to be overriden by the collection.

Unfortunately my local environment got broken, so I couldnā€™t check that. I will try to respond ASAP :slight_smile:

1 Like

If I try to look at the variables(Host ā†’ Ansible ā†’ Variables tab and sub-tab) , this is the outcome :frowning:
The roles Tab and Sub Tab is fine.

Greetings

This is probably fixed here Fixes #35895 - fix ansible variables tab error by nofaralfasi Ā· Pull Request #591 Ā· theforeman/foreman_ansible Ā· GitHub

@nofaralfasi: thanks a lot! This fixed the Error 500 ! Now I can see all Variables in the Host Answible Tab ā€¦
but ā€¦ I still canā€™t use ā€œforeman_host_colltectionsā€ :frowning:
Maybe itā€™s because host_collections is an array ? If I use the field ā€œcommentā€, for example, I can work with it. However, it would be important to also make these fields usable so that I have another grouping (editable via the UI) option (and I can recreate something like ansible inventory groups).

Or is there a better way to recreate this in foreman (e.g. that a host can be in multiple groups like ā€œwebā€ and ā€œdatabaseā€ when both are running on this system)?

What unfortunately also cost me time (the documentation should be clearer here): all foreman ansible facts are ā€œflatā€, i.e. ā€œcommentā€ applies instead of ā€œforeman::commentā€ (as it is displayed in the fact list).

Greetings

Frank

1 Like

and depending on the different host_collections memberships, it should be possible to overwrite the variables with suitable values. The point is that if I already have the Ansible integration in foreman, I donā€™t have to build an additional Ansible infrastructure (like awx) in smaller environments.

I was able to override a variable based on a hostgroup on nightly.
However, Iā€™m not sure what you mean when you say we can do the override using host_collections.
Iā€™m trying to do that using facts now, but I havenā€™t figured it out yet.

I recorded a gif that should illustrate this

variables

I have a host in host collection (entity added by Katello plugin). I go to ansible variable thatā€™s overriden for the hostā€™s role. I can specify a new matcher in the order field (this is not well known feature). Then it appears in the attribute type selector. Given host_collection is valid relation, IMHO that should work. Once I save the variable, I donā€™t see the value on host detail page.

Whatā€™s also interesting - I already override the value per hostgroup and os, however I still only see the default value. Thatā€™s what I mean by the inheritance seems broken. I donā€™t see those values (hostgroup or os level) even in the ansible inventory.

hostgroup as one type of grouping is fine, but my hostgroups are only divided into OS and Lifecycle as subgroups (by the way: a host can only be a member of one hostgroup). I need a second kind of grouping (e.g. one group for all web servers, one group for all DB servers and so on (and like in Ansible inventories, a host should be able to be in different groups at the same time)). For me, foreman host_collections could be this kind of grouping ā€¦ but ā€¦ it is not possible to use this variable as matcher to overwrite (because this variable is an array and not a string?).

@Marek_Hulan: I think the correct foreman ansible variable name is: foreman_host_collections but even with that it does not work :frowning:

I went through the same steps as you suggested with the host_collection to try and override a variable, but it didnā€™t work for me (I tried it on downstream cause I donā€™t have the setup for katello ready yet).

Yeah, thatā€™s what I meant, Iā€™m afraid it wouldnā€™t work. But I was not sure if because the other issue that can be seen on the recording or because thereā€™s something else missing for host_collection (e.g. missing search definition).

Yeah, host collection working would just positively surprise me. However as you can see on the recording, the hostgroup nor os overriding does not seem to work on the nightly (demo hw btw). It may be something with the demo hw, since youā€™re saying hostgroup overriding works for you. Can you also try os just to be sure?

Works with os on nightly.
I forgot that you can see the value of the variable in the hostā€™s new UI (nice feature!), so I thought I have to run the role to see the variable.
Next time Iā€™ll save myself some time and use the demo HW :slight_smile:

@nofaralfasi: ??? what have you tested ?

I created a role that prints the variable I was testing. I wanted to see if we can use a matcher for host_collections (after it worked with the hostgroups).
Now Iā€™m trying to see if I can achieve the same goal by using facts.

Thanks for thƩ clarification!