← All TILs · ansible

Lire en français traduction automatique Google Traduire sur votre ordinateur Firefox

The Foreman/Satellite dynamic inventory plugin

ansible - 2026-09-05

Sixteenth entry in the Ansible inventory from scratch series. Inventory plugins as a single source of truth explained inventory plugins, the auto plugin and the inventory cache in general. This entry applies them to one source: Foreman. Foreman is an open-source server that provisions and tracks machines; Red Hat Satellite is built on it. theforeman.foreman.foreman, the inventory plugin in the theforeman.foreman collection, asks Foreman's API for its hosts and turns them into Ansible hosts, groups and variables, so the inventory is whatever Foreman has registered, not a list kept by hand. Everything below ran with ansible-core 2.21.4 and theforeman.foreman 5.13.0, against a fake Foreman (see The example).

A few Foreman terms first:

Install and point it at Foreman

ansible-galaxy collection install theforeman.foreman

The plugin imports the Python requests library, and refuses to load without it (requires: requests >= 1.1 in its documentation). It runs on the controller, so requests must be in the Python that runs Ansible, such as the virtualenv ansible-core is installed in. ansible-core doesn't install it.

The source is a YAML file whose name ends in foreman.yml or foreman.yaml:

# inventory/hosts-api.foreman.yml
plugin: theforeman.foreman.foreman
url: https://foreman.example.com
use_reports_api: false

Two corrections to what this entry used to say:

The user and password are required, but don't have to be in the file: the plugin reads FOREMAN_USER and FOREMAN_PASSWORD from the environment, and FOREMAN_URL for the URL. The example sets the first two in run.sh and keeps them out of the source. Item 10 covers vault for values that must be in a file.

Two APIs underneath

The plugin talks to Foreman in one of two ways:

Against a Foreman without foreman_ansible, the default doesn't fall back to the Hosts API. The schedule request gets a 404, the plugin raises Error scheduling inventory report on foreman. Please check foreman logs!, and Ansible turns it into warnings: ansible-inventory --graph exits 0 and shows only @all and @ungrouped. A playbook would run on no hosts. So when the graph comes back empty, set use_reports_api: false and try again before reading Foreman's logs. (The max_timeout option's documentation speaks of falling back to old host API when the report takes too long, but in 5.13.0's code a timeout raises Timeout receiving inventory report from foreman instead.)

What the plugin requested, logged by the fake Foreman, for three hosts:

Source Requests
Reports API, default options 3: status, schedule, the report
Hosts API, want_params 4: the list, then /api/v2/hosts/<id> per host
Hosts API, want_params, want_facts, want_hostcollections 13: the list, then per host /api/v2/hosts/<id> twice and /api/v2/hosts/<id>/facts twice

/api/v2/hosts/<id> comes twice because parameters and host collections each fetch it; the facts come in pages until an empty one. On a few thousand hosts, that's thousands of requests per run, which is the reason the Reports API exists, and the reason for the cache below. --limit doesn't reduce them: ansible-playbook --limit db1 made the same four requests as a full run, since the inventory is built before the limit applies (item 12 covers --limit).

Hosts, groups and variables

The example's Foreman has three hosts: web1 and web2 in host group Base/Web, db1 in Base/DB, in two locations (Brussels, Paris) and one organization (Acme). With the Hosts API:

plugin: theforeman.foreman.foreman
url: http://127.0.0.1:18160
use_reports_api: false
want_params: true
want_facts: true
want_hostcollections: true
hostnames:
  - name.split('.')[0]
compose:
  ansible_host: foreman_ip
keyed_groups:
  - key: foreman_location_name | lower
    prefix: location
  - key: tier
    prefix: tier
groups:
  postgresql: db_engine is defined and db_engine == 'postgresql'
@all:
  |--@ungrouped:
  |--@foreman_base:
  |  |--@foreman_base_web:
  |  |  |--web1
  |  |  |--web2
  |  |--@foreman_base_db:
  |  |  |--db1
  |--@foreman_hostcollection_webservers:
  |  |--web1
  |  |--web2
  |--@foreman_hostcollection_patchtuesday:
  |  |--web1
  |  |--db1
  |--@location_brussels:
  ...
  |--@postgresql:
  |  |--db1

web1's variables (ansible-inventory --host web1):

{
    "ansible_host": "192.0.2.11",
    "app_port": 8080,
    "foreman_facts": {
        "os::family": "RedHat",
        "os::release::full": "9.6",
        "processorcount": "2"
    },
    "foreman_hostgroup_id": 2,
    "foreman_id": 1,
    "foreman_ip": "192.0.2.11",
    "foreman_location_name": "Brussels",
    "foreman_organization_name": "Acme",
    "ntp_server": "ntp.brussels.example.com",
    "tier": "frontend",
    ...
}

legacy_hostvars: the old script's layout

Before the plugin, Foreman shipped an inventory script, foreman.py. legacy_hostvars: true gives its layout: Foreman's fields in one dict, foreman, the parameters in another, foreman_params, and the facts in foreman_facts when asked for. db1's top-level variables are then just foreman and foreman_params. That keeps the parameters from colliding with other variables, and a playbook reads them as a dict. To loop over one, turn it into a list of key/value pairs with dict2items, which part 2 of the data-shaping series explains:

- name: Show the parameters, a dict with legacy_hostvars, as a list
  ansible.builtin.debug:
    msg: "{{ item.key }}={{ item.value }}"
  loop: "{{ foreman_params | dict2items }}"
ntp_server=ntp.brussels.example.com
db_engine=postgresql
tier=backend

The same source sets group_prefix: fm_, and the groups become fm_base, fm_base_web and fm_base_db. The older option layout, foreman: and report: dicts holding use_reports_api and the Reports API options, still works, and its documentation marks it deprecated.

The Reports API's groups

With the Reports API and want_hostcollections, the same three hosts give the host group groups again, plus foreman_location_brussels, foreman_location_paris, foreman_organization_acme, and, from Katello's content details, foreman_lifecycle_environment_production and foreman_content_view_rhel9. The host collection Web servers becomes foreman_hostcollection_web_servers there, against foreman_hostcollection_webservers with the Hosts API: the two code paths in the plugin don't clean names the same way. Switching APIs renames those groups, so a playbook targeting them breaks.

The cache

The plugin supports the inventory cache: Foreman's answers are kept between runs, and a run within the timeout makes no request at all.

cache: true
cache_plugin: ansible.builtin.jsonfile
cache_connection: out/cache
cache_timeout: 3600

The first ansible-inventory --graph made four requests and wrote one file in out/cache/, holding Foreman's answers keyed by URL (/api/v2/hosts, /api/v2/hosts/1, ...), not the finished inventory. The second made none and printed the same graph. --flush-cache ignored the cache and made the four requests again. The cost is freshness: a host added in Foreman appears only when the cache expires or is flushed.

The example

The series' companion repository, abdelhousni/ansible-inventory-series, holds the sources and a fake Foreman: a small Python server that answers with JSON files and logs each request. The answers are shaped after the plugin's source (the fields it reads) and Foreman's API v2, not recorded from a live server, so field lists in a real Foreman are longer. run.sh starts the server, prints the graphs, web1's variables and the requests of each source, runs the cache twice and flushes it, then the two pitfalls. Its CI runs it on every push and compares the output with the expected one.

Sources

Created 2026-09-05T14:42:16+02:00, updated 2026-10-03T14:28:13+02:00 · History · Edit