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!