I'm trying to run Ansible playbooks without starting the execution from foreman/satellite, for many reasons (ugly scheduling is one). Instead, I want to use GitLab CI.
For this, I'm using the foreman inventory plugin, (theforeman.foreman.foreman) which allow Ansible to get inventory from foreman/satellite.
Now, my roles are using variables defined in foreman/satellite as host parameters and I don't find the way to access those from my playbook using the foreman inventory.
I have these settings in my inventory plugin yaml config:
plugin: theforeman.foreman.foreman
url: https://whatever.url...
user: rhs_inventory_reader
password: **********
host_filters: 'last_checkin > "12 hours ago" and hypervisor = false'
want_facts: true
want_hostcollections: true
want_params: true
legacy_hostvars: true
According to the documentation, parameter legacy_hostvars does this:
Places hostvars in a dictionary with keys
foreman,foreman_facts, andforeman_params
I've tried to print the vars but they're not even defined in Ansible:
tasks:
- debug:
var: foreman
What is wrong with this approach and how can I gather foreman host parameters from Ansible with foreman inventory, without triggering the run from within foreman?
As far as I understood using theforeman.foreman plugin inventory you need to use ansible-inventory to retrieve the data you want.
We have our foreman.yml config like below:
You can call to get foreman data from a specific host by:
and you will get info like below:
The limitation using this plugin I cannot find yet the way to integrate in ansible.cfg and it seems the plugin cannot build specific Groups based on foreman_params.
The advantage is that the json data is taken directly from foreman database whenever you run the command.
However, there's another method which might help.
We don't use a plugin method as dynamic inventory, we use a adhoc python script who create a copy of foreman json data:
ansible.cfg looks like below:
Inside /project/automati0/playbooks/dxc_lux/customers/Caceis/playbooks/dynamic_inventory you will have two files:
The foreman.ini file would look as foreman.yml plugin:
So, the python script will create an json inventory retrieved from foreman based on foreman.ini settings.
The advantage this method is that you can create Ansible Groups based on Foreman Params, which will give you an incredible flexibility for fulfilling vars based on Groups....
The disadvantage is that you need to run periodically ( or manually ) if you want to see latest change within foreman.