Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add experimental molecule support #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ $ deactivate

[Vagrant](https://www.vagrantup.com/) is our preferred Development/Testing framework.

### Experimental molecule support

```
# clone this repo
$ git clone [email protected]:ibm/cuttle.git

# install pip, hopefully your system has it already
# install virtualenv
$ pip install virtualenv

# create a new virtualenv so python is happy
$ virtualenv --system-site-packages --no-wheel ~/<username>/venv

# activate your new venv like normal
$ source ~/<username>/venv/bin/activate

# install ursula-cli, the correct version of ansible, and all other deps
$ cd cuttle
$ pip install -r requirements-molecule.txt

# run molecule to deploy dev env for mirror
$ molecule converge --scenario-name mirror

# cleanup
$ molecule destroy --scenario-name mirror
$ deactivate
```

### Example Usage

ursula-cli understands how to interact with vagrant using the `--provisioner` flag:
Expand Down
47 changes: 47 additions & 0 deletions molecule/mirror/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
*******
Install
*******

This set of playbooks have specific dependencies on Ansible due to the modules
being used.

Requirements
============

* Ansible 2.2
* Vagrant
* Virtualbox, Parallels, VMware Fusion, VMware Workstation or VMware Desktop
* python-vagrant

Install OS dependencies on CentOS 7

.. code-block:: bash

$ sudo yum install -y epel-release
$ sudo yum install -y gcc python-pip python-devel openssl-devel
# If installing Molecule from source.
$ sudo yum install libffi-devel git

Install OS dependencies on Ubuntu 16.x

.. code-block:: bash

$ sudo apt-get update
$ sudo apt-get install -y python-pip libssl-dev vagrant virtualbox
# If installing Molecule from source.
$ sudo apt-get install -y libffi-dev git

Install OS dependencies on Mac OS

.. code-block:: bash

$ brew install python
$ brew install git

Install using pip:

.. code-block:: bash

$ sudo pip install ansible
$ sudo pip install python-vagrant
$ sudo pip install molecule --pre
53 changes: 53 additions & 0 deletions molecule/mirror/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
- hosts: localhost
connection: local
vars:
molecule_file: "{{ lookup('env','MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env','MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
tasks:
- name: Create molecule instance(s)
molecule_vagrant:
instance_name: "{{ molecule_file | molecule_instance_with_scenario_name(item.name) }}"
provider_memory: 1024
provider_cpus: 1
instance_interfaces:
- auto_config: true
ip: 172.16.0.115
network_name: private_network
type: static
- auto_config: true
ip: 172.16.1.115
network_name: private_network
type: static
platform_box: "{{ item.box }}"
state: up
register: server
with_items: "{{ molecule_yml.platforms }}"

# Mandatory configuration for Molecule to function.

- name: Populate instance config dict
set_fact:
instance_conf_dict: {
'instance': "{{ item.Host }}",
'address': "{{ item.HostName }}",
'user': "{{ item.User }}",
'port': "{{ item.Port }}",
'identity_file': "{{ item.IdentityFile }}", }
with_items: "{{ server.results }}"
register: instance_config_dict
when: server.changed | bool

- name: Convert instance config dict to a list
set_fact:
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
when: server.changed | bool

- name: Dump instance config
copy:
# NOTE(retr0h): Workaround for Ansible 2.2.
# https://github.com/ansible/ansible/issues/20885
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool
30 changes: 30 additions & 0 deletions molecule/mirror/destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---

- hosts: localhost
connection: local
vars:
molecule_file: "{{ lookup('env','MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env','MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
molecule_vagrant:
instance_name: "{{ molecule_file | molecule_instance_with_scenario_name(item.name) }}"
platform_box: "{{ item.box }}"
state: destroy
register: server
with_items: "{{ molecule_yml.platforms }}"

# Mandatory configuration for Molecule to function.

- name: Populate instance config
set_fact:
instance_conf: {}

- name: Dump instance config
copy:
# NOTE(retr0h): Workaround for Ansible 2.2.
# https://github.com/ansible/ansible/issues/20885
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool
42 changes: 42 additions & 0 deletions molecule/mirror/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
dependency:
name: galaxy
driver:
name: vagrant
lint:
name: yamllint
platforms:
- name: mirror01
box: bento/ubuntu-14.04
groups:
- mirror
provisioner:
name: ansible
options:
extra-vars: "@../../envs/example/vagrant.yml"
config_options:
defaults:
hash_behaviour: merge
vars_plugins: ../../plugins/vars
connection_plugins: ../../plugins/connection
callback_plugins: ../../plugins/callbacks
filter_plugins: ../../plugins/filters
var_defaults_file: ../../envs/example/defaults.yml
connection_options:
ansible_become: true
lint:
name: ansible-lint
playbooks:
setup: create.yml
converge: ../../site.yml
teardown: destroy.yml
inventory:
links:
group_vars: ../../../envs/example/mirror/group_vars/
#host_vars: ../host_vars/
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8
12 changes: 12 additions & 0 deletions molecule/mirror/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- hosts: all
gather_facts: no
tasks:
- name: Install python for Ansible
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
become: yes
changed_when: False

- hosts: all
roles:
- role: bar
14 changes: 14 additions & 0 deletions molecule/mirror/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_hosts_file(host):
f = host.file('/etc/hosts')

assert f.exists
assert f.user == 'root'
assert f.group == 'root'
8 changes: 8 additions & 0 deletions requirements-molecule.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#urllib3>=1.19.1
#six>=1.10.0
#jinja2==2.8.1
ansible>=2.3,<2.4
#python-heatclient
python-vagrant
#requests[security]
molecule