项目作者: kunchev

项目描述 :
Parse the Foreman API per environment and generate Ansible hosts file containing host group and related hosts per group
高级语言: Python
项目地址: git://github.com/kunchev/ansible-foreman-hosts-inventory.git
创建时间: 2021-04-14T14:43:47Z
项目社区:https://github.com/kunchev/ansible-foreman-hosts-inventory

开源协议:MIT License

下载


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 []
  • 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 /config/foreman.ini, it consists of the following details

  • section [foreman] with the following information required:

    1. base_url = https://foreman.example.test/api/v2/environments/
    2. username = adminusername
    3. password = adminpassword
    4. 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 (foremaninventory_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

  1. # navigate to the project folder
  2. cd </path/to/project>
  3. # display help
  4. python3 main.py [-h | --help]
  5. # make sure that file is executable if calling without 'python3'
  6. ./main.py -h
  7. ./main.py --help
  8. # list foreman environments with respective IDs
  9. python3 main.py --action listenvs
  10. python3 main.py -a listenvs
  11. # parse selected foreman environment and generate ansible hosts file
  12. python3 main.py --action parseenv 1
  13. python3 main.py -a parseenv 1

Example usage on Windows operating systems

  1. # navigate to the project folder
  2. cd <project>
  3. # display help
  4. python main.py [-h | --help]
  5. python main.py [-h | --help]
  6. # list foreman environments with respective IDs
  7. python main.py --action listenvs
  8. python main.py -a listenvs
  9. # parse selected foreman environment and generate ansible hosts file
  10. python main.py --action parseenv 1
  11. 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

  1. - vim ./foreman_hosts
  2. - nano ./foreman_hosts
  3. - cat ./foreman_hosts
  4. - less ./foreman_hosts
  5. # or use any other method to list the contents of the file which is suitable to your operating system
  1. # Ansible hosts file for Foreman inventory id 1 generated on 07/05/2021 17:50:21
  2. [production/Web]
  3. webserver01.domain.a
  4. webserver02.domain.a
  5. [test/DB]
  6. dbserver01.domain.b
  7. dbserver02.domain.b
  8. [development]
  9. devbox01.domain.a
  10. devbox01.domain.a
  11. [applicationA/Dev/locationA]
  12. appsrv01.domain.c
  13. appsrv02.domain.c
  14. [demo/DB/locationB]
  15. demo-dbsrv01.domain.d
  16. demo-dbsrv02.domain.d
  17. demo-dbsrv03.domain.d
  18. [mailservers]
  19. mail01.example.com
  20. mail02.example.com
  21. [backupservers]
  22. one-backup.example.com
  23. two-backup.example.com
  24. three-backup.example.com

The generated inventory file can be used by Ansible as a hosts file

  1. Use Ansible 'raw' module to execute the 'uptime' command on all servers in the 'test/DB' hostgroup
  2. ansible -m raw -a "uptime" -i /path/to/foreman_inventory test/DB
  3. Use Ansible 'raw' module to execute the 'hostname -s' command on all servers in the 'mailservers' and 'backupservers' hostgroups
  4. ansible -m raw -a "hostname -s" /path/to/foreman_inventory mailservers:backupservers
  5. </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