Skip to content

Parse the Foreman API per environment and generate Ansible hosts file containing host group and related hosts per group

License

Notifications You must be signed in to change notification settings

kunchev/ansible-foreman-hosts-inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ansible-foreman-hosts-inventory

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.

  • Parse the Foreman API and print all environments with their respective environment IDs [<integer_value>]
  • Parse the Foreman API per environment and generate Ansible hosts file containing host group and hosts related
  • Cron job can be set to automatically generate recent hosts file
  • Requires network connection from the source machine to the Foreman server
  • Requires some additional modules to be installed (check the requirements.txt file)
  • Runs on Windows and NIX* operating systems with Python 3.x installed

Included modules and configuration files

  • modules/frmn_configparser.py - Parse required settings in order proper API requests to be initiated
  • modules/frmn_envparser.py - Initiate API calls to the Foreman API endpoint(s) and parse/generate the data
  • config/foreman.ini - API connection details (API endpoint URL, API user, API password, output file name)

Configuration file

The foreman.ini configuration file is located at <PROJECT_ROOT>/config/foreman.ini, it consists of the following details

  • 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)

  • username - the API user for the connection (make sure to provide sufficient read rights, user is created in Foreman, required value)

  • password - the password for the API user (required value)

  • hfile - prefix of the name of the output file which will be generated (foreman_inventory_envid_ the parsed environment id will be appended to the prefix provided, example: foreman_inventory_envid_2, required value)

Example usage on Linux os macOS operating systems

# 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

Example usage on Windows operating systems

# 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

Sample file generated

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

Plans for future development

  • Run this as a containerized application

Releases

No releases published

Packages

No packages published

Languages