Giter Site home page Giter Site logo

sagiru / foreman_ansible_inventory Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theforeman/foreman_ansible_inventory

0.0 1.0 0.0 57 KB

Foreman dynamic inventory script for ansible

License: GNU General Public License v3.0

Python 100.00%

foreman_ansible_inventory's Introduction

foreman_ansible_inventory

WARNING: This inventory is available by default with Ansible since version 2.2. Please submit any enhancements directly upstream to https://github.com/ansible/ansible/blob/devel/contrib/inventory/foreman.py

Build Status PyPI version

This script can be used as an Ansible dynamic inventory1. The connection parameters are set up via a configuration file foreman.ini. foreman.ini is found using the following order of discovery.

* `/etc/ansible/foreman.ini`
* Current directory of your inventory script.
* `FOREMAN_INI_PATH` environment variable.

Variables and Parameters

The data returned from Foreman for each host is stored in a foreman hash so they're available as host_vars along with the parameters of the host and it's hostgroups:

 "foo.example.com": {
    "foreman": {
      "architecture_id": 1,
      "architecture_name": "x86_64",
      "build": false,
      "build_status": 0,
      "build_status_label": "Installed",
      "capabilities": [
        "build",
        "image"
      ],
      "compute_profile_id": 4,
      "hostgroup_name": "webtier/myapp",
      "id": 70,
      "image_name": "debian8.1",
      ...
      "uuid": "50197c10-5ebb-b5cf-b384-a1e203e19e77"
    },
    "foreman_params": {
      "testparam1": "foobar",
      "testparam2": "small",
      ...
    }

and could therefore be used in Ansible like:

- debug: msg="From Foreman host {{ foreman['uuid'] }}"

Which yields

TASK [test_foreman : debug] ****************************************************
ok: [foo.example.com] => {
"msg": "From Foreman host 50190bd1-052a-a34a-3c9c-df37a39550bf"
}

Automatic Ansible groups

The inventory will provide a set of groups, by default prefixed by 'foreman_'. If you want to customize this prefix, change the group_prefix option in /etc/ansible/foreman.ini. The rest of this guide will assume the default prefix of 'foreman'

The hostgroup, location, organization, content view, and lifecycle environment of each host are created as Ansible groups with a foreman_ prefix, all lowercase and problematic parameters removed. So e.g. the foreman hostgroup

myapp / webtier / datacenter1

would turn into the Ansible group:

foreman_hostgroup_myapp_webtier_datacenter1

Furthermore Ansible groups can be created on the fly using the group_patterns variable in foreman.ini so that you can build up hierarchies using parameters on the hostgroup and host variables.

Lets assume you have a host that is built using this nested hostgroup:

myapp / webtier / datacenter1

and each of the hostgroups defines a parameters respectively:

myapp: app_param = myapp
webtier: tier_param = webtier
datacenter1: dc_param = datacenter1

The host is also in a subnet called "mysubnet" and provisioned via an image then group_patterns like:

[ansible]
group_patterns = ["{app_param}-{tier_param}-{dc_param}",
                  "{app_param}-{tier_param}",
                  "{app_param}",
                  "{subnet_name}-{provision_method}"]

would put the host into the additional Ansible groups:

- myapp-webtier-datacenter1
- myapp-webtier
- myapp
- mysubnet-image

by recursively resolving the hostgroups, getting the parameter keys and values and doing a Python string.format() like replacement on it.

foreman_ansible_inventory's People

Contributors

aboyett avatar agx avatar brandonweeks avatar dlobatog avatar nstrug avatar smnmtzgr avatar wenottingham avatar wwitzel3 avatar yankcrime avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.