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
      roles:
      - 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: https://github.com/geerlingguy/ansible-role-repo-epel/blob/master/defaults/main.yml
  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)

3 Likes

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
roles:
  - 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 https://github.com/theforeman/foreman_ansible/pull/193 - if anyone can start making the UI for the parameters tab itā€™d be a huge time saver :smile:

2 Likes

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

Best,

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.