From d5a5a7a5d2ba18ba2524f847f2eef81c75043154 Mon Sep 17 00:00:00 2001 From: Pablo Estigarribia Date: Wed, 2 Jan 2019 17:37:22 -0300 Subject: [PATCH] adding first tests for ansible_nagios4_server_config and ubuntu 1604 support --- ansible.cfg | 18 ++++++ molecule/default/Dockerfile.j2 | 24 ++++++++ molecule/default/INSTALL.rst | 16 +++++ molecule/default/create.yml | 59 +++++++++++++++++++ molecule/default/destroy.yml | 27 +++++++++ molecule/default/molecule.yml | 75 ++++++++++++++++++++++++ molecule/default/playbook.yml | 6 ++ molecule/default/prepare.yml | 5 ++ molecule/default/requirements.yml | 4 ++ molecule/default/tests/test_default.py | 14 +++++ molecule/default/tests/test_default.pyc | Bin 0 -> 1021 bytes requirements.yml | 4 ++ run_local_molecule.sh | 7 +++ run_local_molecule_basic.sh | 26 ++++++++ run_local_molecule_destroy.sh | 10 ++++ tasks/multi_os.yml | 6 ++ vars/RedHat.yml | 14 ++--- vars/Ubuntu-18.yml | 15 +++++ vars/Ubuntu.yml | 2 - 19 files changed, 323 insertions(+), 9 deletions(-) create mode 100644 ansible.cfg create mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/default/INSTALL.rst create mode 100644 molecule/default/create.yml create mode 100644 molecule/default/destroy.yml create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/playbook.yml create mode 100644 molecule/default/prepare.yml create mode 100644 molecule/default/requirements.yml create mode 100644 molecule/default/tests/test_default.py create mode 100644 molecule/default/tests/test_default.pyc create mode 100644 requirements.yml create mode 100644 run_local_molecule.sh create mode 100644 run_local_molecule_basic.sh create mode 100644 run_local_molecule_destroy.sh create mode 100644 vars/Ubuntu-18.yml diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..dd24de7 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,18 @@ +# config file for ansible -- http://ansible.com/ +# ============================================== + +# nearly all parameters can be overridden in ansible-playbook +# or with command line flags. ansible will read ANSIBLE_CONFIG, +# ansible.cfg in the current working directory, .ansible.cfg in +# the home directory or /etc/ansible/ansible.cfg, whichever it +# finds first + +[defaults] +roles_path = ../:/etc/ansible/roles:~/.ansible/roles +host_key_checking = False + +retry_files_enabled = False +callback_whitelist = profile_tasks + +[ssh_connection] +pipelining = False diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000..491096f --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,24 @@ +# Molecule managed + +FROM {{ item.image }} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then 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 && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; fi + +RUN if [ $(command -v dnf) ]; then dnf -y update && dnf clean all; fi + +RUN if [ $(command -v dnf) ]; then dnf -y install systemd hostname && dnf clean all && \ +(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \ +rm -f /lib/systemd/system/multi-user.target.wants/*;\ +rm -f /etc/systemd/system/*.wants/*;\ +rm -f /lib/systemd/system/local-fs.target.wants/*; \ +rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ +rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ +rm -f /lib/systemd/system/basic.target.wants/*;\ +rm -f /lib/systemd/system/anaconda.target.wants/*; fi + +# VOLUME [ "/sys/fs/cgroup" ] +# CMD ["/usr/sbin/init"] diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..e26493b --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,16 @@ +******* +Install +******* + +Requirements +============ + +* Docker Engine +* docker-py + +Install +======= + +.. code-block:: bash + + $ sudo pip install docker-py diff --git a/molecule/default/create.yml b/molecule/default/create.yml new file mode 100644 index 0000000..bfb2149 --- /dev/null +++ b/molecule/default/create.yml @@ -0,0 +1,59 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}" + molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - 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 }}" + register: platforms + + - name: Discover local Docker images + docker_image_facts: + name: "molecule_local/{{ item.item.name }}" + with_items: "{{ platforms.results }}" + register: docker_images + + - name: Build an Ansible compatible image + docker_image: + path: "{{ molecule_ephemeral_directory }}" + name: "molecule_local/{{ item.item.image }}" + dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}" + force: "{{ item.item.force | default(true) }}" + with_items: "{{ platforms.results }}" + when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0 + + - name: Create molecule instance(s) + docker_container: + name: "{{ item.name }}" + hostname: "{{ item.name }}" + image: "molecule_local/{{ item.image }}" + state: started + recreate: false + log_driver: syslog + command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}" + privileged: "{{ item.privileged | default(omit) }}" + volumes: "{{ item.volumes | default(omit) }}" + capabilities: "{{ item.capabilities | default(omit) }}" + ports: "{{ item.exposed_ports | 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 }}" diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml new file mode 100644 index 0000000..3ce7478 --- /dev/null +++ b/molecule/default/destroy.yml @@ -0,0 +1,27 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Destroy molecule instance(s) + docker_container: + name: "{{ item.name }}" + state: absent + force_kill: "{{ item.force_kill | default(true) }}" + register: server + with_items: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: docker_jobs + until: docker_jobs.finished + retries: 300 + with_items: "{{ server.results }}" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..7e3a0b9 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,75 @@ +--- +dependency: + name: galaxy + options: + ignore-certs: True + ignore-errors: True + role-file: requirements.yml +driver: + name: docker +lint: + name: yamllint +platforms: + + - name: ansible_test-01 + image: paulfantom/ubuntu-molecule:16.04 + #privileged: True + command: /sbin/init + capabilities: + - SYS_ADMIN + volumes: + - "/sys/fs/cgroup:/sys/fs/cgroup:ro" + groups: + - group1 + + - name: ansible_test-01_2 + image: paulfantom/ubuntu-molecule:18.04 + #privileged: True + command: /sbin/init + capabilities: + - SYS_ADMIN + volumes: + - "/sys/fs/cgroup:/sys/fs/cgroup:ro" + groups: + - group1 + + - name: ansible_test-02 + image: ubuntu:trusty + groups: + - groupold + + - name: ansible_test-03 + image: centos/systemd + command: /sbin/init + capabilities: + - SYS_ADMIN + volumes: + - "/sys/fs/cgroup:/sys/fs/cgroup:ro" + #privileged: True + groups: + - group1 + +provisioner: + name: ansible + config_options: + defaults: + callback_whitelist: profile_tasks + inventory: + group_vars: + master: + burpsrcext: "zip" + burp_version: "master" + burp_remove_clients: + - name: client_to_remove + - name: other_client_to_remove + burp_server_port_per_operation_bool: true + lint: + name: ansible-lint + +scenario: + name: default + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..e61e351 --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,6 @@ +--- +- name: Converge + hosts: all + roles: + - role: ansible-role-nagios + - role: ansible_nagios4_server_config diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml new file mode 100644 index 0000000..5358b3b --- /dev/null +++ b/molecule/default/prepare.yml @@ -0,0 +1,5 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: [] diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml new file mode 100644 index 0000000..74fb649 --- /dev/null +++ b/molecule/default/requirements.yml @@ -0,0 +1,4 @@ +# from GitHub, overriding the name and specifying a specific tag +- src: https://github.com/CoffeeITWorks/ansible-role-nagios.git + version: master + name: ansible-role-nagios diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/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/molecule/default/tests/test_default.pyc b/molecule/default/tests/test_default.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6cb13885d34eb737362538b4b6e3c4c2a62a8450 GIT binary patch literal 1021 zcmdr~&2H2%5FRJnUs3i{pW5|#i`eh>`6t{?ge-! z9)XA9$OFLG*|ep+0ZBX_e;I!>{>I_2t#~}n&e06 zha`P>Jo}ixI+`Zv(ic*ejOJiD4r#ViGS`&KS40UG46Z331G@_tz?Xg*x