Ansible variables not returned by foreman dynamic inventory plugin

Problem:
I cannot seem to find a way to get the ansible variables in foreman to be accessible via the inventory plugin. No matter which of the configuration values I set (use_reports_api, want_hostcollections, want_facts, want_params, legacy_hostvars, etc.) in the my.foreman.yml file, I never see ansible variables. If I set want_params to true, I do get some of the host parameters, but I never see any ansible variables that are set at the global, host group or individual host levels. I can see them in the GUI under host-ansible-inventory, but the plugin never returns them. I ran the Ansible inventory reports template manually and it does not return the variables either. Is there an ansible plugin reports template object that I can add to a cloned reports template to return the ansible variables? Or is there a different way to return these objects for my playbooks that are run from the ansible host’s CLI itself?

Expected outcome:
I had hoped that the foreman ansible dynamic inventory plugin would return the ansible inventory values as it shows in the foreman GUI under hostname - ansible - inventory

Foreman and Proxy versions:
foreman 3.9.1 / Katello 4.11
Ansible smart proxy 3.10

Foreman and Proxy plugin versions:
foreman_ansible 13.0.3
foreman_remote_execution 12.0.5

Distribution and version:
Rocky 8.9

Other relevant data:

Hi,

the Ansible variables in Foreman only make sense if you run Ansible from within Foreman (as they refer to Roles that you assign in Foreman). Because of that they are not part of the “external” inventory we present to other Ansible runs.

Can you describe your use-case a bit more where you need the variable set in Foreman but running Ansible outside of it?

CC @nofaralfasi as the foreman_ansible maintainer.

1 Like

Without the ability to parse the ansible variables in the dynamic inventory, you are right that you can only run roles with the variables in the foreman GUI. The roles code itself lives outside of the foreman GUI on the ansible server. I was hoping to still have the ability to run ansible roles and scripts outside of foreman directly in the ansible server CLI. This gives me the ability to do role and script development interactively on the ansible server CLI and check in changed to GIT. Even if all of the role code lives on the foreman server itself in the /etc/ansible directory, you still need CLI capability to develop and do interactive testing before handing off to the foreman GUI. Without CLI access to foreman ansible variables, you have to maintain variables in two separate inventories one for the GUI and one for the CLI. Keeping them in sync with multiple users would be precarious at best. It seems in the current paradigm, you have to choose either no foreman GUI usage or ansible CLI variable management. I wanted to give foreman users the ability to manage variables without providing CLI access to ansible. If the foreman dynamic inventory plugin could parse the ansible variables, you could maintain all variables in foreman for both GUI and CLI.

Here is another use case that could potentially leverage dynamic inventory facts. Ansible allows for a host to be in multiple groups. Foreman does not have the concept of multiple host groups. Hosts in foreman can only be in a single host group. Katello does however have the concept of host collections. You can assign hosts to multiple host collections. As it stands today, the dynamic inventory plugin can convert these host collections into groups for the CLI. Lets assume you have a host collection called GENERIC and wanted it to show up as a group for a host in ansible. By adding the following logic to the plugin yaml file, you could effectively assign a system to a group based on host collection membership.

groups:
GENERIC: (‘GENERIC’) in foreman_host_collections

Assuming you had a parallel host group name called GENERIC in foreman with the needed group ansible variables, you could query the ansible facts assigned to the group in foreman via the plugin. So you could potentially use host collections to let foreman provide the multiple group functionality that ansible has by nature. This was the methodology I was hoping existed so that I could move my multiple group ansible inventory and variables into foreman.

I am using foreman inventory plugins with Ansible, and I am able to get parameters as Ansible variables.

I use it with hostgroup and host; I should have added hostgroup access to my ansible user to get them.

Related to multiple host groups I have the same … I have added the collection features to create special “groups”.

For clarification, are you seeing ansible variables or host parameters in the plugin output? If the former, what versions of things are you running. I can see host parameters, but the ansible variables are not showing up. For me the ansible variables only work when running things from the foreman GUI.