Giter Club home page Giter Club logo

playbook2image's Introduction

Playbook To Image

A Source-to-Image (S2I) builder image for packaging Ansible playbooks as a self-executing container.

Usage

Prerequisites: an OpenShiftv3 cluster or s2i binary

In this workflow we build a new image with our playbook, setup secrets (private ssh key, for example) and create a job to run our playbook image.

  1. Build: Add your playbook to the image. This will create a new image with your playbook sourcecode
  • Using OpenShift. In this example we instruct the build script to install the OpenShift CLI so it's available from our playbook:

      oc new-build -e INSTALL_OC=true \
         docker.io/aweiteka/playbook2image~https://github.com/PLAYBOOK/REPO.git
    
  • Using docker:

    1. Using the example Dockerfile, create a Dockerfile in the playbook repository.

    2. Build the image

       docker build -t IMAGE_NAME . -f Dockerfile.example
      
  • Using s2i CLI tool:

      sudo s2i build https://github.com/PLAYBOOK/REPO.git docker.io/aweiteka/playbook2image NEW_PLAYBOOK_IMAGE_NAME
    
  1. Run: as an OpenShift Job or with docker via command line
  • Using OpenShift:

    1. Create a secret for our ssh private key

       oc secrets new-sshauth sshkey --ssh-privatekey=$HOME/.ssh/id_rsa
      
    2. Create a new job. Download the sample-job.yaml file, edit and create the job.

       oc create -f sample-job.yaml
      
  • Using Docker (example command):

      sudo docker run \
           -v ~/.ssh/id_rsa:/opt/app-root/src/.ssh/id_rsa \
           -e OPTS="--become --user cloud-user" \
           -e PLAYBOOK_FILE=PATH_TO_PLAYBOOK \
           -e INVENTORY_URL=URL \
           IMAGE_FROM_BUILD_STEP
    

Runtime Environment Variable Options

A container run from a playbook2image image needs at least these configured options:

  1. An inventory. You must specify it using one of these three options: INVENTORY_FILE to point to a local path inside the container, INVENTORY_URL to download a static inventory file, or DYNAMIC_SCRIPT_URL to download a dynamic inventory script.
  2. A playbook to run, set via PLAYBOOK_FILE.
  3. ssh keys mounted into the container (by default these should be in /opt/app-root/src/.ssh).

Below is a list of available options. Ansible itself also allows its configuration to be controlled via environment variables and some of these are specially relevant for playbook2image's use case so they are also highligted here (starting with ANSIBLE_*):

INVENTORY_FILE

Path to the location of the inventory file within the container. It can be a relative path pointing to an inventory provided in the source, or an absolute path to an inventory mounted in the container via a volume.

INVENTORY_URL

URL to inventory file. This is downloaded into the container.

DYNAMIC_SCRIPT_URL

URL to dynamic inventory script. This is downloaded into the container. If the dynamic inventory script is python see PYTHON_REQUIREMENTS.

PLAYBOOK_FILE

Relative path to playbook file relative to project source. This is mounted in the container at /opt/app-root/src/PLAYBOOK_FILE.

ALLOW_ANSIBLE_CONNECTION_LOCAL (optional)

If set to false all ansible_connection=local settings in the inventory will be removed. This can help when you want to use an existing inventory file that uses local connections to a host: it is likely that an ssh connection is a better fit when running from a container.

PYTHON_REQUIREMENTS (optional, default 'requirements.txt')

Relative path to python dependency requirements.txt file to support dynamic inventory script.

ANSIBLE_PRIVATE_KEY_FILE (optional, e.g. '/opt/app-root/src/.ssh/id_rsa/ssh-privatekey')

Container path to mounted private SSH key. For OpenShift this must match the secret volumeMount (see mountPath in sample-job.yaml). For docker this must match the bindmount container path, e.g. -v ~/.ssh/id_rsa:/opt/app-root/src/.ssh/id_rsa.

OPTS (optional)

List of options appended to ansible-playbook command. An example of commonly used options:

OPTS="-vvv --become --user cloud-user"

VAULT_PASS (optional)

ansible-vault passphrase for decrypting files. This is written to a file and used to decrypt ansible-vault files.

ANSIBLE_HOST_KEY_CHECKING=False

Disable host key checking. See documentation

WORK_DIR (optional)

If not specified ansible-playbook will run from ${APP_HOME} directory (/opt/app-root/src) where the target repository is mounted. When relative or absolute path is specified in WORK_DIR, ansible-playbook will be launched from WORK_DIR directory. That might come in handy for example if you have roles or ansible.cfg in non-root of the repository.

Build time Environment Variable Options

These options are passed in when building the application (e.g. oc new-build or docker build or s2i)

INSTALL_OC (optional)

If specified during build (e.g. oc new-build -e INSTALL_OC=true ...) the oc OpenShift client binary is downloaded and installed into the resulting image.

Contribute

S2I project documentation

To run tests you will need to install the s2i binary.

Running tests

sudo make test

playbook2image's People

Contributors

aweiteka avatar brenton avatar codificat avatar jcpowermac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

playbook2image's Issues

Add dynamic inventory script to build

In some cases the dynamic inventory script should land as part of the build, not run. Demonstrate how to add this during the build step so it travels with the image.

Revert locked ansible version workaround

This issue is a placeholder to revert this Dockerfile workaround when openshift/openshift-ansible#3111 is resolved.

# install ansible via pip to lock version
#RUN yum install -y  --setopt=tsflags=nodocs ansible python-pip python-devel && yum clean all -y
RUN yum install -y  --setopt=tsflags=nodocs python-pip python-devel && yum clean all -y
RUN pip install -Iv ansible==2.2.0.0

Add tests to pipeline

Currently the jenkins pipeline tests that the container can build. We should also test the built image.

  1. run an s2i build using the test playbook
  2. run as an openshift/kubernetes job

cc @jcpowermac

Support atomic help / install / run

  • Currently the README.md file is added as /help.1 to support atomic help IMAGE. This help file needs to be man page formatted. Ideally draft a specific file for the docker run use case. See example
  • add install label (depending on use case?)
  • add run label

s2i assemble script fails due to exceeding github API rate limit

Assemble script fails while parsing the OC_BINARY_URL due to hitting API rate limit:

$ python -c "import requests;print [s for s in [r for r in requests.get('https://api.github.com/repos/openshift/origin/releases').json() if not r['prerelease'] and '1.4' in r['name']][0]['assets'] if 'linux-64' in s['browser_download_url']][0]['browser_download_url']"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: string indices must be integers

If we run just the part that returns the json response:

$ python -c "import requests;requests.get('https://api.github.com/repos/openshift/origin/releases').json()"

{u'documentation_url': u'https://developer.github.com/v3/#rate-limiting',
 u'message': u"API rate limit exceeded for 66.187.233.202. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"}

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.