complete-computing-environment/dynamic_ansible_bender_play...

3.0 KiB

Dynamic Ansible Bender Playbooks

This can be used by CCE Server modules to ease container builds. See Universal Aggregator and My "Now Playing" on Fediverse for implementation details.

feed this a bunch of values encoded in yaml, easier than using click. If any of the keys are missing, this will throw an exception, nice. This will emit an Org Babel link which can be used to execute the bender. See Universal Aggregator for a sample of how these image playbooks are built. This is super janky and brittle and I should probably make it more robust as I move container builds on to it…

import sys
import os
import yaml
import pathlib

configuration_as_yaml = sys.stdin.read()
config = yaml.safe_load(configuration_as_yaml)

service_name = config["service_name"]
friendly_name = config["friendly"]
reqs = config["build_reqs"]
task_tags = config["task_tags"]
roles = config["build_roles"]
template = config["template"]
cmd = config["cmd"]

tout = None
with open(os.path.expanduser(template), "r") as f:
  t_str = f.read()
  tout = yaml.safe_load(t_str)

for playbook in tout:
  playbook["vars"]["build_reqs"] = reqs
  playbook["name"] = "build " + friendly_name

  playbook["roles"] = [{
    "name": role,
    "tags": task_tags,
  } for role in roles]

  playbook["vars"]["ansible_bender"]["target_image"]["name"] = service_name
  playbook["vars"]["ansible_bender"]["target_image"]["cmd"] = cmd

service_dir = os.path.expanduser(os.path.join("~/org/cce/containers/", service_name))
pathlib.Path(service_dir).mkdir(parents=True, exist_ok=True)
pb_path = os.path.join(service_dir, "build.yml")
with open(pb_path, 'w') as f:
  f.write(yaml.dump(tout))

cmd = "ANSIBLE_ROLES_PATH=~/org/cce/roles ansible-bender build {} &".format(pb_path)

print("[[shell:%s][Execute =%s=]]" % (cmd, cmd))

this uses a number of templates:

a git repo that can be cloned, make install'd.

---
- hosts: all
  vars:
    local_account: root
    build_dir: /tmp/build
    ansible_bender:
      base_image: fedora:33
      target_image:
        name: rrix/ua
        labels:
          build_user: "{{ansible_user}}"

  pre_tasks:
  - name: build deps installed
    dnf:
      state: installed
      name: '{{build_reqs}}'

  - name: build dir exists
    file:
      state: directory
      path: '{{build_dir}}'

  post_tasks: 
  - name: erase build deps
    tags:
    - postbuild
    dnf:
      state: absent
      name: '{{build_reqs}}'
      autoremove: yes

  - name: erase checkout
    file:
      state: absent
      path: '{{build_dir}}'

  - name: /var/cache absent
    file:
      state: absent
      path: /var/cache

  - name: /root/.cache absent
    file:
      state: absent
      path: /root/.cache