|
| 1 | +# Integrating Pre-existing Playbooks with APBs |
| 2 | + |
| 3 | +1. [Introduction](#introduction-to-playbook-integration) |
| 4 | +1. [Distinguishing Actions](#distinguishing-actions) |
| 5 | + 1. [Ansible 'when' Statement](#ansible-when-statement) |
| 6 | +1. [Pre-existing Playbook Installation](#pre-existing-playbook-installation) |
| 7 | + 1. [Ansible Galaxy](#ansible-galaxy) |
| 8 | + 1. [RPMs](#rpms) |
| 9 | + 1. [GitHub Source](#github-source) |
| 10 | +1. [Running the Pre-existing Playbooks](#running-the-pre-existing-playbooks) |
| 11 | + 1. [Shim Playbook](#shim-playbook) |
| 12 | + |
| 13 | +## Introduction to Playbook Separation |
| 14 | +APBs expect playbooks to be named after an APB action like |
| 15 | +`playbooks/provision.yml` locally inside the container. This can prove |
| 16 | +problematic when trying to use a pre-existing playbook or a well tested |
| 17 | +production playbook that you don't want to change to create an APB. But, since |
| 18 | +Ansible allows playbooks to import other playbooks, roles, and tasks, we can |
| 19 | +create `playbooks/provision.yml` locally and use it to point to pre-existing |
| 20 | +playbooks without having to change those playbooks to fit the APB directory |
| 21 | +structure. This allows for the developer to keep production playbook code where |
| 22 | +it is and import that code into the APB's container from Ansible Galaxy, rpms, |
| 23 | +or git repos. |
| 24 | + |
| 25 | +There are three things required for integration: |
| 26 | + 1. Ansible playbooks that distinguish between APB actions |
| 27 | + 2. Installing the pre-existing playbooks in the APB container |
| 28 | + 3. The APB uses a playbook to call the pre-existing playbooks |
| 29 | + |
| 30 | +## Distinguishing Actions |
| 31 | +Since there are multiple action an APB can use, pre-existing |
| 32 | +playbooks will need to distinguish which action to perform. The APB will |
| 33 | +provide to the playbooks with the variable `apb_action` for this purpose. |
| 34 | + |
| 35 | +#### Ansible 'when' Statement |
| 36 | +Ansible has the `when:` to only run tasks if a condition is met. Add these |
| 37 | +to individual tasks, roles, or playbooks to distinguish actions. |
| 38 | + |
| 39 | +```yaml |
| 40 | +--- |
| 41 | +- name: Provision mysql |
| 42 | + shell: oc create -f mysql-rc.yaml |
| 43 | + when: "{{ apb_action }}" == "provision" |
| 44 | +``` |
| 45 | +
|
| 46 | +At the playbook level: |
| 47 | +
|
| 48 | +```yaml |
| 49 | +- hosts: all |
| 50 | + tasks: |
| 51 | + - import_role: |
| 52 | + name: provision mysql |
| 53 | + tasks_from: "{{ apb_action }}" |
| 54 | +``` |
| 55 | +
|
| 56 | +## Pre-existing Playbook Installation |
| 57 | +The playbooks need to be on the container file system so it can be |
| 58 | +referenced. Here are a few ways to install a pre-existing playbook inside |
| 59 | +the Dockerfile: |
| 60 | +
|
| 61 | +#### Ansible Galaxy |
| 62 | +Using [kubevirt-apb](https://github.com/ansibleplaybookbundle/kubevirt-apb) as an example, we'll install the [kubevirt-ansible](https://github.com/kubevirt/kubevirt-ansible/) playbooks using ansible-galaxy. |
| 63 | +
|
| 64 | +From the [Dockerfile](https://github.com/ansibleplaybookbundle/kubevirt-apb/blob/master/Dockerfile): |
| 65 | +```bash |
| 66 | +... |
| 67 | + |
| 68 | +COPY requirements.yml /opt/ansible/requirements.yml |
| 69 | +COPY inventory /etc/ansible/hosts |
| 70 | + |
| 71 | +RUN ansible-galaxy install -r /opt/ansible/requirements.yml |
| 72 | +RUN chmod -R g=u /opt/{ansible,apb} /etc/ansible/roles |
| 73 | + |
| 74 | +USER apb |
| 75 | +``` |
| 76 | + |
| 77 | +#### RPMs |
| 78 | +If you distribute your production playbook as an RPM, install the RPM in the APB |
| 79 | +container. |
| 80 | + |
| 81 | +```bash |
| 82 | +... |
| 83 | + |
| 84 | +RUN yum install -y production-mysql-playbooks |
| 85 | +... |
| 86 | +``` |
| 87 | + |
| 88 | +#### GitHub Source |
| 89 | +Finally, you can install the playbooks right from source by cloning from git or |
| 90 | +curling a tarball. |
| 91 | + |
| 92 | +```bash |
| 93 | +... |
| 94 | + |
| 95 | +RUN git clone https://github.com/beekhof/galera-ansible |
| 96 | +... |
| 97 | +``` |
| 98 | + |
| 99 | +## Running the Pre-existing Playbooks |
| 100 | +Now that the pre-existing playbooks are local to the APB, create playbooks |
| 101 | +for the actions and import playbooks, roles, and tasks from the pre-existing |
| 102 | +playbooks. |
| 103 | + |
| 104 | +#### Shim playbook |
| 105 | +Create playbooks ```provision.yml``` and ```deprovision.yml```, then start |
| 106 | +making calls to the pre-existing playbooks. |
| 107 | + |
| 108 | +playbooks/provision.yml |
| 109 | +```yaml |
| 110 | +- name: Provision Production mysql |
| 111 | + hosts: localhost |
| 112 | + gather_facts: false |
| 113 | + connection: local |
| 114 | + roles: |
| 115 | + - role: ansible.kubernetes-modules |
| 116 | + install_python_requirements: no |
| 117 | + - role: ansibleplaybookbundle.asb-modules |
| 118 | + - role: mysql-production |
| 119 | + vars: |
| 120 | + apb_action: "provision" |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +Here's an example of importing an entire playbook: |
| 125 | + |
| 126 | +playbooks/provision.yml |
| 127 | +```yaml |
| 128 | +- name: Provision KubeVirt |
| 129 | + hosts: localhost |
| 130 | + gather_facts: false |
| 131 | + connection: local |
| 132 | + roles: |
| 133 | + - role: ansible.kubernetes-modules |
| 134 | + install_python_requirements: no |
| 135 | + - role: ansibleplaybookbundle.asb-modules |
| 136 | + |
| 137 | +- import_playbook: "/etc/ansible/roles/kubevirt-ansible/playbooks/kubevirt.yml" |
| 138 | + vars: |
| 139 | + apb_action: "provision" |
| 140 | +``` |
0 commit comments