diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 index 7ba209c..0a60553 100644 --- a/molecule/default/Dockerfile.j2 +++ b/molecule/default/Dockerfile.j2 @@ -1,7 +1,14 @@ -FROM {{ item.image }} +# Molecule managed -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 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 +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && 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 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 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; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/default/create.yml b/molecule/default/create.yml index eca41c5..5b453ef 100644 --- a/molecule/default/create.yml +++ b/molecule/default/create.yml @@ -2,14 +2,22 @@ - name: Create hosts: localhost connection: local - gather_facts: False + 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) | from_yaml }}" 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" @@ -20,6 +28,7 @@ - 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 }}" register: docker_images @@ -27,21 +36,49 @@ 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) }}" + 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 + - 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 }}" - hostname: "{{ 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: "molecule_local/{{ item.image }}" state: started - recreate: False - log_driver: none - command: "{{ item.command | default('sleep infinity') }}" + 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) }}" + exposed_ports: "{{ item.exposed_ports | default(omit) }}" + published_ports: "{{ item.published_ports | default(omit) }}" + ulimits: "{{ item.ulimits | default(omit) }}" + networks: "{{ item.networks | default(omit) }}" + dns_servers: "{{ item.dns_servers | 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 index 63b5edf..fa48f36 100644 --- a/molecule/default/destroy.yml +++ b/molecule/default/destroy.yml @@ -2,15 +2,31 @@ - name: Destroy hosts: localhost connection: local - gather_facts: False + gather_facts: false no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" - vars: - molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" - molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}" tasks: - name: Destroy molecule instance(s) docker_container: name: "{{ item.name }}" + docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" state: absent - force_kill: "{{ item.force_kill | default(True) }}" + 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 }}" + + - name: Delete docker network(s) + docker_network: + name: "{{ item }}" + docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" + state: absent + with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 7473504..bb98be5 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -10,27 +10,37 @@ lint: platforms: - name: influxdb - image: influxdb - privileged: True + image: influxdb:1.7-alpine + command: influxd groups: - - influxdb + - tsdb + networks: + - name: telegraf + environment: + INFLUXDB_DB: telegraf - name: telegraf-centos image: milcom/centos7-systemd privileged: True groups: - telegraf + networks: + - name: telegraf - name: telegraf-debian image: minimum2scp/systemd-stretch privileged: True command: /sbin/init groups: - telegraf + networks: + - name: telegraf - name: telegraf-ubuntu image: solita/ubuntu-systemd:bionic privileged: True command: /sbin/init groups: - telegraf + networks: + - name: telegraf provisioner: name: ansible @@ -39,6 +49,12 @@ provisioner: inventory: group_vars: all: + telegraf_agent_output: + - type: influxdb + config: + - urls = ["http://influxdb:8086"] + - database = "telegraf" + - precision = "s" telegraf_plugins_extra: percpu-usage: plugin: cpu @@ -61,9 +77,6 @@ provisioner: - plugin: netstat - plugin: processes - plugin: kernel - - plugin: nginx - config: - - urls = ["http://localhost/nginx_status"] scenario: name: default diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 8537787..f911bae 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -1,4 +1,4 @@ --- -- hosts: all +- hosts: telegraf roles: - role: ansible-telegraf diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index e113160..84d4f42 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -1,6 +1,6 @@ --- -- hosts: all +- hosts: telegraf tasks: - name: "Installing which on CentOS" yum: diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index e8b4efe..7ebba87 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -3,14 +3,13 @@ import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('telegraf') -def test_telegraf_running_and_enabled(Service, SystemInfo): +def test_telegraf_running_and_enabled(Service): telegraf = Service("telegraf") assert telegraf.is_enabled - if SystemInfo.distribution == 'centos': - assert telegraf.is_running + assert telegraf.is_running def test_telegraf_dot_conf(File): @@ -18,7 +17,20 @@ def test_telegraf_dot_conf(File): assert telegraf.user == "telegraf" assert telegraf.group == "telegraf" assert telegraf.mode == 0o640 + assert telegraf.contains('interval = "10s"') assert telegraf.contains('[[inputs.cpu]]') + assert telegraf.contains('percpu = true') + assert telegraf.contains('[[outputs.influxdb]]') + assert telegraf.contains('["http://influxdb:8086"]') + assert telegraf.contains('[[inputs.net]]') + + +def test_telegraf_dot_d_dir(File): + telegraf = File("/etc/telegraf/telegraf.d") + assert telegraf.user == "root" + assert telegraf.group == "root" + assert telegraf.mode == 0o755 + assert telegraf.is_directory def test_telegraf_dot_d(File): diff --git a/tasks/Debian.yml b/tasks/Debian.yml index a7b541f..f4bcef8 100644 --- a/tasks/Debian.yml +++ b/tasks/Debian.yml @@ -9,7 +9,8 @@ apt: name: "apt-transport-https" state: present - when: not apt_https_transport.stat.exists + when: + - not apt_https_transport.stat.exists become: yes - name: Download Telegraf apt key. @@ -25,7 +26,9 @@ filename: "telegraf" state: present become: yes - when: ansible_lsb is defined and ansible_lsb.codename is defined + when: + - ansible_lsb is defined + - ansible_lsb.codename is defined - name: Add Telegraf repository. apt_repository: @@ -33,7 +36,8 @@ filename: "telegraf" state: present become: yes - when: ansible_lsb is not defined or ansible_lsb.codename is not defined + when: + - ansible_lsb is not defined or ansible_lsb.codename is not defined - name: "Install telegraf package | Debian" apt: diff --git a/tasks/configure.yml b/tasks/configure.yml index e548603..64222fa 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -3,14 +3,16 @@ - name: Retrieve ec2 facts ec2_facts: - when: telegraf_agent_aws_tags + when: + - telegraf_agent_aws_tags - name: Retrieve all ec2 tags on the instance ec2_tag: region: '{{ ansible_ec2_placement_region }}' resource: '{{ ansible_ec2_instance_id }}' state: list - when: telegraf_agent_aws_tags + when: + - telegraf_agent_aws_tags register: ec2_tags - name: "Copy the template for versions < 0.10.0" @@ -21,7 +23,8 @@ group: telegraf mode: 0640 become: yes - when: telegraf_agent_version is version_compare('0.10.0', '<') + when: + - telegraf_agent_version is version_compare('0.10.0', '<') notify: "Restart Telegraf" - name: "Copy the template for versions >= 0.10.0" @@ -32,19 +35,24 @@ group: telegraf mode: 0640 become: yes - when: telegraf_agent_version is version_compare('0.10.0', '>=') + when: + - telegraf_agent_version is version_compare('0.10.0', '>=') notify: "Restart Telegraf" - name: "Check if extra plugins directory exists in case of exclusive" - action: stat path=/etc/telegraf/telegraf.d + stat: + path: /etc/telegraf/telegraf.d register: telegraf_directory - when: telegraf_plugins_extra_exclusive + when: + - telegraf_plugins_extra_exclusive - name: "Delete telegraf extra plugin path" file: state: absent path: "/etc/telegraf/telegraf.d/" - when: telegraf_plugins_extra_exclusive and telegraf_directory.stat.exists + when: + - telegraf_plugins_extra_exclusive + - telegraf_directory.stat.exists become: yes notify: "Restart Telegraf" @@ -54,8 +62,10 @@ path: "/etc/telegraf/telegraf.d/" owner: telegraf group: telegraf - mode: 0740 - when: telegraf_plugins_extra_exclusive and telegraf_directory.stat.exists + mode: 0755 + when: + - telegraf_plugins_extra_exclusive + - telegraf_directory.stat.exists become: yes notify: "Restart Telegraf" @@ -69,7 +79,10 @@ with_dict: "{{ telegraf_plugins_extra }}" loop_control: label: "{{ item.key }}" - when: "telegraf_plugins_extra is defined and telegraf_plugins_extra is iterable and item.value.state|default('present') != 'absent'" + when: + - telegraf_plugins_extra is defined + - telegraf_plugins_extra is iterable + - item.value.state|default('present') != 'absent' become: yes notify: "Restart Telegraf" @@ -80,7 +93,10 @@ with_dict: "{{ telegraf_plugins_extra }}" loop_control: label: "{{ item.key }}" - when: "telegraf_plugins_extra is defined and telegraf_plugins_extra is iterable and item.value.state|default('present') == 'absent'" + when: + - telegraf_plugins_extra is defined + - telegraf_plugins_extra is iterable + - item.value.state|default('present') == 'absent' become: yes notify: "Restart Telegraf"