From fe5efa6efc338917b751278e34e47e68dca1da39 Mon Sep 17 00:00:00 2001 From: Paul Czarkowski Date: Sat, 24 Jun 2017 17:17:32 -0500 Subject: [PATCH] add experimental molecule support --- README.md | 28 ++++++++++++++ molecule/mirror/INSTALL.rst | 47 ++++++++++++++++++++++++ molecule/mirror/create.yml | 53 +++++++++++++++++++++++++++ molecule/mirror/destroy.yml | 30 +++++++++++++++ molecule/mirror/molecule.yml | 42 +++++++++++++++++++++ molecule/mirror/playbook.yml | 12 ++++++ molecule/mirror/tests/test_default.py | 14 +++++++ requirements-molecule.txt | 8 ++++ 8 files changed, 234 insertions(+) create mode 100644 molecule/mirror/INSTALL.rst create mode 100644 molecule/mirror/create.yml create mode 100644 molecule/mirror/destroy.yml create mode 100644 molecule/mirror/molecule.yml create mode 100644 molecule/mirror/playbook.yml create mode 100644 molecule/mirror/tests/test_default.py create mode 100644 requirements-molecule.txt diff --git a/README.md b/README.md index 89d16b7..9b34456 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,34 @@ $ deactivate [Vagrant](https://www.vagrantup.com/) is our preferred Development/Testing framework. +### Experimental molecule support + +``` +# clone this repo +$ git clone git@github.com: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 ~//venv + +# activate your new venv like normal +$ source ~//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: diff --git a/molecule/mirror/INSTALL.rst b/molecule/mirror/INSTALL.rst new file mode 100644 index 0000000..99f29ce --- /dev/null +++ b/molecule/mirror/INSTALL.rst @@ -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 diff --git a/molecule/mirror/create.yml b/molecule/mirror/create.yml new file mode 100644 index 0000000..8bd61dc --- /dev/null +++ b/molecule/mirror/create.yml @@ -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 diff --git a/molecule/mirror/destroy.yml b/molecule/mirror/destroy.yml new file mode 100644 index 0000000..0fcefd2 --- /dev/null +++ b/molecule/mirror/destroy.yml @@ -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 diff --git a/molecule/mirror/molecule.yml b/molecule/mirror/molecule.yml new file mode 100644 index 0000000..83cd04b --- /dev/null +++ b/molecule/mirror/molecule.yml @@ -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 diff --git a/molecule/mirror/playbook.yml b/molecule/mirror/playbook.yml new file mode 100644 index 0000000..912995f --- /dev/null +++ b/molecule/mirror/playbook.yml @@ -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 diff --git a/molecule/mirror/tests/test_default.py b/molecule/mirror/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/mirror/tests/test_default.py @@ -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' diff --git a/requirements-molecule.txt b/requirements-molecule.txt new file mode 100644 index 0000000..0618027 --- /dev/null +++ b/requirements-molecule.txt @@ -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