Skip to content

Commit

Permalink
create example repo for ansible/molecule#1646
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Varzariu committed Jan 16, 2019
0 parents commit 41f2b4b
Show file tree
Hide file tree
Showing 46 changed files with 1,571 additions and 0 deletions.
512 changes: 512 additions & 0 deletions README.md

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[defaults]
strategy_plugins = plugins/mitogen/ansible_mitogen/plugins/strategy
stdout_callback=debug
stderr_callback=debug
library = /opt/ansible/modules/library
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 15
poll_interval = 15
become_user = root
transport = smart
remote_port = 22
gathering = implicit
roles_path = roles
host_key_checking = False
timeout = 10
jinja2_extensions = jinja2.ext.do
ansible_managed = Ansible managed
display_skipped_hosts = False
deprecation_warnings = True
action_plugins = /usr/share/ansible_plugins/action_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins = /usr/share/ansible_plugins/lookup_plugins
vars_plugins = /usr/share/ansible_plugins/vars_plugins
filter_plugins = ./filter_plugins/filter
callback_whitelist = profile_tasks
nocows = 1
retry_files_enabled = False
[ssh_connection]
control_path = %(directory)s/ssh-%%h
scp_if_ssh = True
[inventory]
unparsed_is_failed = True
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
molecule == 2.19
docker
redis
22 changes: 22 additions & 0 deletions roles/.yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-before: 0
max-spaces-after: 1
line-length: disable
truthy: disable
comments:
require-starting-space: true
comments-indentation: disable
hyphens:
max-spaces-after: 1
indentation:
indent-sequences: consistent
new-line-at-end-of-file: enable
23 changes: 23 additions & 0 deletions roles/generic_loadbalancer/.yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-before: 0
max-spaces-after: 1
line-length: disable
truthy: disable
comments:
require-starting-space: true
comments-indentation: disable
hyphens:
max-spaces-after: 1
indentation:
indent-sequences: consistent
new-line-at-end-of-file: enable

6 changes: 6 additions & 0 deletions roles/generic_loadbalancer/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# defaults file for generic_loadbalancer
generic_loadbalancer:
vip:
- 10.10.10.10
- 10.10.10.11
7 changes: 7 additions & 0 deletions roles/generic_loadbalancer/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# handlers file for generic_loadbalancer

- name: restart keepalived
systemd:
name: keepalived
state: restarted
3 changes: 3 additions & 0 deletions roles/generic_loadbalancer/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- { role: nginx }
9 changes: 9 additions & 0 deletions roles/generic_loadbalancer/molecule/default/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM {{ item.image }}

RUN /bin/sh -c 'if [ -x "$(command -v apt-get)" ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash; fi'
RUN /bin/sh -c 'if [ -x "$(command -v yum)" ]; then touch /var/lib/rpm/* && yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf; fi'
RUN /bin/sh -c 'if [ -x "$(command -v zypper)" ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash; fi'
RUN /bin/sh -c 'if [ -x "$(command -v apk)" ]; then apk update && apk add python sudo bash; fi'
RUN /bin/sh -c 'if [ -x "$(command -v pacman)" ]; then pacman --sync --noconfirm --refresh python2 bash; fi'
RUN /bin/sh -c 'if [ -x "$(command -v dnf)" ]; then dnf makecache fast; dnf --assumeyes install python python-devel python2-dnf bash; fi'
RUN /bin/sh -c 'if [ -x "$(command -v emerge)" ]; then emerge --ask n =dev-lang/python-2\* gentoolkit; fi'
46 changes: 46 additions & 0 deletions roles/generic_loadbalancer/molecule/default/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
*******
Install
*******

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

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

* Ansible 2.2
* Docker Engine
* docker-py

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 docker-engine
# 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 docker-py
$ sudo pip install molecule --pre
93 changes: 93 additions & 0 deletions roles/generic_loadbalancer/molecule/default/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
tasks:
- name: Log into a Docker registry
docker_login:
username: "{{ item.registry.credentials.username }}"
password: "{{ item.registry.credentials.password }}"
email: "{{ item.registry.credentials.email | default(omit) }}"
registry: "{{ item.registry.url }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
with_items: "{{ molecule_yml.platforms }}"
when:
- item.registry is defined
- item.registry.credentials is defined
- item.registry.credentials.username is defined

- name: Create Dockerfiles from image names
template:
src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
with_items: "{{ molecule_yml.platforms }}"
when: not item.pre_build_image | default(false)
register: platforms

- name: Discover local Docker images
docker_image_facts:
name: "molecule_local/{{ item.item.name }}"
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
with_items: "{{ platforms.results }}"
when: not item.pre_build_image | default(false)
register: docker_images

- name: Build an Ansible compatible image
docker_image:
path: "{{ molecule_ephemeral_directory }}"
name: "molecule_local/{{ item.item.image }}"
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}"
force: "{{ item.item.force | default(true) }}"
pull: "{{ item.item.pull | default(omit) }}"
with_items: "{{ platforms.results }}"
when:
- platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
- not item.item.pre_build_image | default(false)

- name: Create docker network(s)
docker_network:
name: "{{ item }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
state: present
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"

- name: Create molecule instance(s)
docker_container:
name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
hostname: "{{ item.hostname | default(item.name) }}"
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}"
state: started
recreate: false
log_driver: json-file
command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
privileged: "{{ item.privileged | default(omit) }}"
security_opts: "{{ item.security_opts | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
tmpfs: "{{ item.tmpfs | default(omit) }}"
capabilities: "{{ item.capabilities | default(omit) }}"
sysctls: "{{ item.sysctls| default(omit) }}"
exposed_ports: "{{ item.exposed_ports | default(omit) }}"
published_ports: "{{ item.published_ports | default(omit) }}"
ulimits: "{{ item.ulimits | default(omit) }}"
networks: "{{ item.networks | default(omit) }}"
network_mode: "{{ item.network_mode | default(omit) }}"
dns_servers: "{{ item.dns_servers | default(omit) }}"
env: "{{ item.env | default(omit) }}"
restart_policy: "{{ item.restart_policy | default(omit) }}"
restart_retries: "{{ item.restart_retries | default(omit) }}"
register: server
with_items: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: docker_jobs
until: docker_jobs.finished
retries: 300
with_items: "{{ server.results }}"
56 changes: 56 additions & 0 deletions roles/generic_loadbalancer/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
dependency:
name: gilt
driver:
name: docker
lint:
name: yamllint
options:
config-file: ../.yamllint
platforms:
- name: glb-001.example.com
image: centos:latest
command: /sbin/init
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
capabilities:
- SYS_ADMIN
- NET_ADMIN
- NET_BIND_SERVICE
sysctls:
net.ipv4.ip_nonlocal_bind: 1
groups:
- generic_loadbalancer
- name: glb-002.example.com
image: centos:latest
command: /sbin/init
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
capabilities:
- SYS_ADMIN
- NET_ADMIN
- NET_BIND_SERVICE
sysctls:
net.ipv4.ip_nonlocal_bind: 1
groups:
- generic_loadbalancer
provisioner:
name: ansible
config_options:
defaults:
jinja2_extensions: jinja2.ext.do
callback_whitelist: profile_tasks
lint:
name: ansible-lint
playbooks:
side_effect: side_effect.yml
scenario:
name: default
verifier:
name: goss
lint:
name: yamllint
options:
config-file: ../.yamllint
5 changes: 5 additions & 0 deletions roles/generic_loadbalancer/molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Converge
hosts: generic_loadbalancer
roles:
- role: generic_loadbalancer
25 changes: 25 additions & 0 deletions roles/generic_loadbalancer/molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
- name: Prepare
hosts: generic_loadbalancer
gather_facts: False
tasks:
- name: install dependencies
yum:
name: "{{ item }}"
state: present
with_items:
- iproute
- sysvinit-tools
- epel-release

- name: create facts dir
file:
path: /etc/ansible/facts.d
state: directory

- name: set fact
copy:
content: |
[run]
true = 1
dest: /etc/ansible/facts.d/molecule.fact
13 changes: 13 additions & 0 deletions roles/generic_loadbalancer/molecule/default/side_effect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: kill nginx on glb-001
gather_facts: false
hosts:
- glb-001.example.com
become: true
tasks:
- name: stop nginx
service:
name: nginx
state: stopped
- name: wait for keepalived to move IP
shell: sleep 5
16 changes: 16 additions & 0 deletions roles/generic_loadbalancer/molecule/default/tests/test_default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
package:
nginx:
installed: true
keepalived:
installed: true
user:
nginx:
exists: true
groups:
- nginx
home: /var/lib/nginx
shell: /sbin/nologin
group:
nginx:
exists: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
service:
nginx:
enabled: true
running: false
port:
tcp:80:
listening: false

eth0:
exists: true
addrs: []
Loading

0 comments on commit 41f2b4b

Please sign in to comment.