Foreman's Ansible integration

Fri, 19 Aug 2016 09:16:03 GMT

Gathering from some recent discussions it seems to be not that well known that Foreman (a lifecycle tool for your virtual machines) does not only integrate well with Puppet but also with ansible. This is a list of tools I find useful in this regard:

The ansible-module-foreman ansible module allows you to setup all kinds of resources like images, compute resources, hostgroups, subnets, domains within Foreman itself via ansible using Foreman's REST API. E.g. creating a hostgroup looks like:

- foreman_hostgroup:
    name: AHostGroup
    architecture: x86_64
    domain: a.domain.example.com
    foreman_host: "{{ foreman_host }}"
    foreman_user: "{{ foreman_user }}"
    foreman_pass: "{{ foreman_pw }}"

The foreman_ansible plugin for Foreman allows you to collect reports and facts from ansible provisioned hosts. This requires an additional hook in your ansible config like:

[defaults]
callback_plugins = path/to/foreman_ansible/extras/

The hook will report to Foreman back after a playbook finished.

There are several options for creating hosts in Foreman via the ansible API. I'm currently using ansible_foreman_module tailored for image based installs. This looks in a playbook like:

- name: Build 10 hosts
  foremanhost:
    name: "{{ item }}"
    hostgroup: "a/host/group"
    compute_resource: "hopefully_not_esx"
    subnet: "webservernet"
    environment: "{{ env|default(omit) }}"
    ipv4addr: {{ from_ipam|default(omit) }}"
    # Additional params to tag on the host
    params:
        app: varnish
        tier: web
        color: green
    api_user: "{{ foreman_user }}"
    api_password: "{{ foreman_pw }}"
    api_url: "{{ foreman_url }}"
  with_sequence:  start=1 end=10 format="newhost%02d"

The foreman_ansible_inventory is a dynamic inventory script for ansible that fetches all your hosts and groups via the Foreman REST APIs. It automatically groups hosts in ansible from Foreman's hostgroups, environments, organizations and locations and allows you to build additional groups based on any available host parameter (and combinations thereof). So using the above example and this configuration:

[ansible]
group_patterns = ["{app}-{tier}",
                  "{color}"]

it would build the additional ansible groups varnish-web, green and put the above hosts into them. This way you can easily select the hosts for e.g. blue green deployments. You don't have to pass the parameters during host creation, if you have parameters on e.g. domains or hostgroups these are available too for grouping via group_patterns.

If you're grouping your hosts via the above inventory script and you use lots of parameters than having these displayed in the detail page can be useful. You can use the foreman_params_tab plugin for that.

There's also support for triggering ansible runs from within Foreman itself but I've not used that so far.