Ansible variables as "smart class parameters"

Hi devs!

I have a little proof of concept of how can we start moving our Foreman Ansible plugin to use Smart Class Parameters. Using GitHub - dLobatog/foreman_ansible at ansible-params and GitHub - dLobatog/smart_proxy_ansible at smart_class_params we can:

  • Import variables from Ansible (I improved the variable detection code)
  • The variables are created as smart class parameters, and they are associated to an ansible role. This means to use them in our ansible roles template, we should do something like this in our job template:
    - hosts: some host
      - role: jenkins
        mysql_password: '{{jenkins_mysql_password}}'
      - role: redmine
        mysql_password: '{{redmine_mysql_password}}'

Here’s how it looks like on the UI.

Index page:

Import page:

Menu entry:

Edit page:

There are a few TODOs of course, in order of importance:

  1. Parameters are not displayed on Hosts#form
  2. I’m not sure whether the validator is working yet
  3. No variable types are detected automatically. It could be done by looking at the variables under /role/defaults/*.yml though. E.g:
  4. No API/hammer support (this one should be easy though, just extending lookup_keys API to accept the ansible_role parameter)

I’m also concerned about the number of vague terms we use for this. Our users shouldn’t need a glossary to use Foreman (cc @Roxanne_Hoover) :

  • Variable: This one could refer to Ansible variables, or Smart Variables
  • Key: This is the “name” of the Smart Class Parameter, Ansible Variable, or Smart Variable

(cc @Bernhard_Suttner as I know he’s quite interested after Foreman Ansible integration future brainstorming with the devs community - #3 by dLobatog)


AFAIK we don’t have this for Puppet either. That nowadays has actual data types while with Ansible you’d always guess (like in Puppet before version 4) so I wouldn’t prioritize this.

1 Like

Have you thought of reusing the variables also in rex job templates?

– Ivan

I’m not sure I understand the question, the format " {{ variable_name }} " can already be used instead of “<%= host_param(‘variable_name’) %>” in job templates. These would be substituted by the parameter with the highest priority, like in puppet (Global > Host group > Host params).

Is that what you meant?

I meant more pre-populating the template’s inputs based on roles being used in the template.

For more context, today the template input can be fed not only by user interactively, but from fact, smart variable and smart class parameter. For the latest, one only selects the puppet class and the name of the parameter. Something similar for ansible variables would be nice.

That would be very nice to have at some point. Notice that even without template inputs, we can use Playbooks as job templates. For example a job template:

hosts: all
  - foreman

Let’s say that role ‘foreman’ uses a variable called ‘database_choice’. Even without any template inputs, as long as you have ‘database_choice’ in the list of host parameters, it will be sent as part of the inventory, so it will be used. Template Inputs in this case are only relevant when you want the user to change them before running the job.

For the record, I’ve submitted this work (with improvements) at - if anyone can start making the UI for the parameters tab it’d be a huge time saver :smile:


Sorry for reviving this old thread, but from reading the source code I gather that this is still the case?


Yes, however there’s an active WIP on this, watch Fixes #25159 - Add Ansible variables to hosts form by xprazak2 · Pull Request #376 · theforeman/foreman_ansible · GitHub for details, once it’s merged, it should appear in nightly builds.