Parse the Foreman API per environment and generate Ansible hosts file containing host group and related hosts per group
Generate Ansible hosts inventory file based on Foreman hostgroups and servers by querying the Foreman API and parsing the data.
Query the Foreman API using desired input parameters for Foreman host group and environment to be parsed and parse all host groups in the desired
environment with their host members, generate Ansible inventory file
using the parsed data and specific Ansible hosts file format.
The foreman.ini configuration file is located at
section [foreman] with the following information required:
base_url = https://foreman.example.test/api/v2/environments/
username = adminusername
password = adminpassword
hfile = foreman_inventory_<envid>
base_url - the Foreman API endpoint which the script will connect to and parse data from (required value)
# navigate to the project folder
cd </path/to/project>
# display help
python3 main.py [-h | --help]
# make sure that file is executable if calling without 'python3'
./main.py -h
./main.py --help
# list foreman environments with respective IDs
python3 main.py --action listenvs
python3 main.py -a listenvs
# parse selected foreman environment and generate ansible hosts file
python3 main.py --action parseenv 1
python3 main.py -a parseenv 1
# navigate to the project folder
cd <project>
# display help
python main.py [-h | --help]
python main.py [-h | --help]
# list foreman environments with respective IDs
python main.py --action listenvs
python main.py -a listenvs
# parse selected foreman environment and generate ansible hosts file
python main.py --action parseenv 1
python main.py -a parseenv 1
Each host group in selected Foreman environment is represented in the file below in square brackets with all servers contained
lets open example file, generated by the script from a test Foreman instance
- vim ./foreman_hosts
- nano ./foreman_hosts
- cat ./foreman_hosts
- less ./foreman_hosts
# or use any other method to list the contents of the file which is suitable to your operating system
# Ansible hosts file for Foreman inventory id 1 generated on 07/05/2021 17:50:21
[production/Web]
webserver01.domain.a
webserver02.domain.a
[test/DB]
dbserver01.domain.b
dbserver02.domain.b
[development]
devbox01.domain.a
devbox01.domain.a
[applicationA/Dev/locationA]
appsrv01.domain.c
appsrv02.domain.c
[demo/DB/locationB]
demo-dbsrv01.domain.d
demo-dbsrv02.domain.d
demo-dbsrv03.domain.d
[mailservers]
mail01.example.com
mail02.example.com
[backupservers]
one-backup.example.com
two-backup.example.com
three-backup.example.com
The generated inventory file can be used by Ansible as a hosts file
Use Ansible 'raw' module to execute the 'uptime' command on all servers in the 'test/DB' hostgroup
ansible -m raw -a "uptime" -i /path/to/foreman_inventory test/DB
Use Ansible 'raw' module to execute the 'hostname -s' command on all servers in the 'mailservers' and 'backupservers' hostgroups
ansible -m raw -a "hostname -s" /path/to/foreman_inventory mailservers:backupservers
</path/to/foreman_inventory> is the path to the Ansible inventory file generated by the script
Any other Ansible module can be used as well as playbook or role, all you need to do is point the path to the ‘foreman_hosts’ file and specify the desired host group(s), or simply set ‘all’ to run on all host groups in the file