1# Foreman inventory (https://github.com/theforeman/foreman_ansible_inventory) 2# 3# This script can be used as an Ansible dynamic inventory. 4# The connection parameters are set up via *foreman.ini* 5# This is how the script founds the configuration file in 6# order of discovery. 7# 8# * `/usr/local/etc/ansible/foreman.ini` 9# * Current directory of your inventory script. 10# * `FOREMAN_INI_PATH` environment variable. 11# 12# ## Variables and Parameters 13# 14# The data returned from Foreman for each host is stored in a foreman 15# hash so they're available as *host_vars* along with the parameters 16# of the host and it's hostgroups: 17# 18# "foo.example.com": { 19# "foreman": { 20# "architecture_id": 1, 21# "architecture_name": "x86_64", 22# "build": false, 23# "build_status": 0, 24# "build_status_label": "Installed", 25# "capabilities": [ 26# "build", 27# "image" 28# ], 29# "compute_profile_id": 4, 30# "hostgroup_name": "webtier/myapp", 31# "id": 70, 32# "image_name": "debian8.1", 33# ... 34# "uuid": "50197c10-5ebb-b5cf-b384-a1e203e19e77" 35# }, 36# "foreman_params": { 37# "testparam1": "foobar", 38# "testparam2": "small", 39# ... 40# } 41# 42# and could therefore be used in Ansible like: 43# 44# - debug: msg="From Foreman host {{ foreman['uuid'] }}" 45# 46# Which yields 47# 48# TASK [test_foreman : debug] **************************************************** 49# ok: [foo.example.com] => { 50# "msg": "From Foreman host 50190bd1-052a-a34a-3c9c-df37a39550bf" 51# } 52# 53# ## Automatic Ansible groups 54# 55# The inventory will provide a set of groups, by default prefixed by 56# 'foreman_'. If you want to customize this prefix, change the 57# group_prefix option in /usr/local/etc/ansible/foreman.ini. The rest of this 58# guide will assume the default prefix of 'foreman' 59# 60# The hostgroup, location, organization, content view, and lifecycle 61# environment of each host are created as Ansible groups with a 62# foreman_<grouptype> prefix, all lowercase and problematic parameters 63# removed. So e.g. the foreman hostgroup 64# 65# myapp / webtier / datacenter1 66# 67# would turn into the Ansible group: 68# 69# foreman_hostgroup_myapp_webtier_datacenter1 70# 71# If the parameter want_hostcollections is set to true, the 72# collections each host is in are created as Ansible groups with a 73# foreman_hostcollection prefix, all lowercase and problematic 74# parameters removed. So e.g. the Foreman host collection 75# 76# Patch Window Thursday 77# 78# would turn into the Ansible group: 79# 80# foreman_hostcollection_patchwindowthursday 81# 82# If the parameter host_filters is set, it will be used as the 83# "search" parameter for the /api/v2/hosts call. This can be used to 84# restrict the list of returned host, as shown below. 85# 86# Furthermore Ansible groups can be created on the fly using the 87# *group_patterns* variable in *foreman.ini* so that you can build up 88# hierarchies using parameters on the hostgroup and host variables. 89# 90# Lets assume you have a host that is built using this nested hostgroup: 91# 92# myapp / webtier / datacenter1 93# 94# and each of the hostgroups defines a parameters respectively: 95# 96# myapp: app_param = myapp 97# webtier: tier_param = webtier 98# datacenter1: dc_param = datacenter1 99# 100# The host is also in a subnet called "mysubnet" and provisioned via an image 101# then *group_patterns* like: 102# 103# [ansible] 104# group_patterns = ["{app_param}-{tier_param}-{dc_param}", 105# "{app_param}-{tier_param}", 106# "{app_param}", 107# "{subnet_name}-{provision_method}"] 108# 109# would put the host into the additional Ansible groups: 110# 111# - myapp-webtier-datacenter1 112# - myapp-webtier 113# - myapp 114# - mysubnet-image 115# 116# by recursively resolving the hostgroups, getting the parameter keys 117# and values and doing a Python *string.format()* like replacement on 118# it. 119# 120[foreman] 121url = http://localhost:3000/ 122user = foreman 123password = secret 124ssl_verify = True 125 126# Retrieve only hosts from the organization "Web Engineering". 127# host_filters = organization="Web Engineering" 128 129# Retrieve only hosts from the organization "Web Engineering" that are 130# also in the host collection "Apache Servers". 131# host_filters = organization="Web Engineering" and host_collection="Apache Servers" 132 133[ansible] 134group_patterns = ["{app}-{tier}-{color}", 135 "{app}-{color}", 136 "{app}", 137 "{tier}"] 138group_prefix = foreman_ 139 140# Whether to fetch facts from Foreman and store them on the host 141want_facts = True 142 143# Whether to create Ansible groups for host collections. Only tested 144# with Katello (Red Hat Satellite). Disabled by default to not break 145# the script for stand-alone Foreman. 146want_hostcollections = False 147 148# Whether to interpret global parameters value as JSON (if possible, else 149# take as is). Only tested with Katello (Red Hat Satellite). 150# This allows to define lists and dictionaries (and more complicated structures) 151# variables by entering them as JSON string in Foreman parameters. 152# Disabled by default as the change would else not be backward compatible. 153rich_params = False 154 155# Whether to populate the ansible_ssh_host variable to explicitly specify the 156# connection target. Only tested with Katello (Red Hat Satellite). 157# If the foreman 'ip' fact exists then the ansible_ssh_host varibale is populated 158# to permit connections where DNS resolution fails. 159want_ansible_ssh_host = False 160 161[cache] 162path = . 163max_age = 60 164 165# Whether to scan foreman to add recently created hosts in inventory cache 166scan_new_hosts = True 167