Groups in Foreman Ansible inventories

I’m seeing a light at the end of the tunnel with this trick — I can edit the inventory file out of the Ansible template itself, and then call meta: refresh_inventory. This lets me piggy-back templating the inventory into the “main” job template, and thus (almost¹) obviates the need for any change in Foreman.

Here is an example playbook (that can be used as an Ansible job template):

- name: homemade inventory
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - debug:
        var: ansible_inventory_sources
    - copy:
        dest: "{{ ansible_inventory_sources[0] }}/hosts"
        mode: "0644"
        content: |
           all:
               hosts:
                 itsidevfsd0009.xaas.epfl.ch: {}
                 itsidevfsd0010.xaas.epfl.ch: {}
    - meta: refresh_inventory

- name: Test new inventory
  hosts: all
  tasks:
    - debug:
        var: inventory_hostname

¹ I’m hitting a bug whence hosts: localhost causes a crash, because foreman-tasks-core in the smart proxy cannot publish_data_for a hostname that it doesn’t know about (here, localhost). Bug report and PR upcoming, perhaps this weekend.