How to query Ansible roles in a job template?

Problem:

I’m looking to create a job template where I can run a subset of Ansible roles instead of running all of them. This is really just for expedience in specific situations, but it would be a nice capability to have.

Expected outcome:

Template that lets me query Ansible roles using a search query and runs all the roles that match the query

Foreman and Proxy versions:
3.9.1.12-1

Distribution and version:
Rocky Linux 8

It seems like there should be some simple syntax to have the job input take a search query on Ansible roles - and I believe I have this working. But I can’t figure out how to use ERB syntax to use that search query in the template.

A subset of all ansible roles or a subset of roles assigned to the host? For the first one I’m afraid we don’t expose necessary macros to achieve that so you would most likely have to disable safe mode to pull it off. If the latter, then it should be doable, albeit in a slightly hacky way (and it wouldn’t use search query syntax).

I’m open to either. I probably would have preferred all, but ones assigned to the host is acceptable as well.

Then the easiest way would be to take the Ansible Roles - Ansible Default template, clone it and then in the clone add a input with Name = filter, Input Type = User input and Value Type = Plain and change the template to the following

---
- hosts: all
  pre_tasks:
    - name: Display all parameters known for the Foreman host
      debug:
        var: foreman
      tags:
        - always
  tasks:
    - name: Apply roles
      include_role:
        name: "{{ role }}"
      tags:
        - always
      loop: "{{ foreman_ansible_roles | select('regex', '<%= input('filter') %>') | list}}"
      loop_control:
        loop_var: role

Then when running this job template, pass in a regular expression as an input, only roles whose name matches the regular expression will be applied. It is a bit crude, but hopefully good enough as a starting point for the time being

Thanks! Given that my use case is almost always one role, I went with a simpler template without a loop. What I have is workable, but it would definitely be an improvement if there was a way to do it with a loop and either a search query or a pick list. It would be a nice to have more than anything, but I do suspect I’m not the only user who would find something like that nice to have