Fix merge issues

This commit is contained in:
Werner Dijkerman
2018-11-22 20:12:57 +01:00
10 changed files with 163 additions and 47 deletions

View File

@ -1,7 +1,7 @@
--- ---
# defaults file for ansible-telegraf # defaults file for ansible-telegraf
telegraf_agent_version: 1.7.3 telegraf_agent_version: 1.9.0
telegraf_agent_version_patch: 1 telegraf_agent_version_patch: 1
telegraf_agent_hostname: "{{ ansible_fqdn }}" telegraf_agent_hostname: "{{ ansible_fqdn }}"
telegraf_agent_interval: 10 telegraf_agent_interval: 10

View File

@ -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; \ {% if item.registry is defined %}
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python python-devel python2-dnf bash && dnf clean all; \ FROM {{ item.registry.url }}/{{ item.image }}
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; \ {% else %}
elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \ FROM {{ item.image }}
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; fi {% 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

View File

@ -2,14 +2,22 @@
- name: Create - name: Create
hosts: localhost hosts: localhost
connection: local connection: local
gather_facts: False gather_facts: false
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" 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: 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 - name: Create Dockerfiles from image names
template: template:
src: "{{ molecule_scenario_directory }}/Dockerfile.j2" src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
@ -20,6 +28,7 @@
- name: Discover local Docker images - name: Discover local Docker images
docker_image_facts: docker_image_facts:
name: "molecule_local/{{ item.item.name }}" 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 }}" with_items: "{{ platforms.results }}"
register: docker_images register: docker_images
@ -27,21 +36,49 @@
docker_image: docker_image:
path: "{{ molecule_ephemeral_directory }}" path: "{{ molecule_ephemeral_directory }}"
name: "molecule_local/{{ item.item.image }}" 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) }}" 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 }}" with_items: "{{ platforms.results }}"
when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0 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) - name: Create molecule instance(s)
docker_container: docker_container:
name: "{{ item.name }}" 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 }}" image: "molecule_local/{{ item.image }}"
state: started state: started
recreate: False recreate: false
log_driver: none log_driver: json-file
command: "{{ item.command | default('sleep infinity') }}" command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
privileged: "{{ item.privileged | default(omit) }}" privileged: "{{ item.privileged | default(omit) }}"
security_opts: "{{ item.security_opts | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}" volumes: "{{ item.volumes | default(omit) }}"
tmpfs: "{{ item.tmpfs | default(omit) }}"
capabilities: "{{ item.capabilities | 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 }}" 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 }}"

View File

@ -2,15 +2,31 @@
- name: Destroy - name: Destroy
hosts: localhost hosts: localhost
connection: local connection: local
gather_facts: False gather_facts: false
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
tasks: tasks:
- name: Destroy molecule instance(s) - name: Destroy molecule instance(s)
docker_container: docker_container:
name: "{{ item.name }}" name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
state: absent state: absent
force_kill: "{{ item.force_kill | default(True) }}" force_kill: "{{ item.force_kill | default(true) }}"
register: server
with_items: "{{ molecule_yml.platforms }}" 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 }}"

View File

@ -9,17 +9,38 @@ lint:
config-file: molecule/default/yaml-lint.yml config-file: molecule/default/yaml-lint.yml
platforms: platforms:
- name: influxdb
image: influxdb:1.7-alpine
command: influxd
groups:
- tsdb
networks:
- name: telegraf
environment:
INFLUXDB_DB: telegraf
- name: telegraf-centos - name: telegraf-centos
image: milcom/centos7-systemd image: milcom/centos7-systemd
privileged: True privileged: True
groups:
- telegraf
networks:
- name: telegraf
- name: telegraf-debian - name: telegraf-debian
image: minimum2scp/systemd-stretch image: minimum2scp/systemd-stretch
privileged: True privileged: True
command: /sbin/init command: /sbin/init
groups:
- telegraf
networks:
- name: telegraf
- name: telegraf-ubuntu - name: telegraf-ubuntu
image: solita/ubuntu-systemd:bionic image: solita/ubuntu-systemd:bionic
privileged: True privileged: True
command: /sbin/init command: /sbin/init
groups:
- telegraf
networks:
- name: telegraf
provisioner: provisioner:
name: ansible name: ansible
@ -28,6 +49,12 @@ provisioner:
inventory: inventory:
group_vars: group_vars:
all: all:
telegraf_agent_output:
- type: influxdb
config:
- urls = ["http://influxdb:8086"]
- database = "telegraf"
- precision = "s"
telegraf_plugins_extra: telegraf_plugins_extra:
percpu-usage: percpu-usage:
plugin: cpu plugin: cpu
@ -50,9 +77,6 @@ provisioner:
- plugin: netstat - plugin: netstat
- plugin: processes - plugin: processes
- plugin: kernel - plugin: kernel
- plugin: nginx
config:
- urls = ["http://localhost/nginx_status"]
scenario: scenario:
name: default name: default

View File

@ -1,4 +1,4 @@
--- ---
- hosts: all - hosts: telegraf
roles: roles:
- role: ansible-telegraf - role: ansible-telegraf

View File

@ -1,6 +1,6 @@
--- ---
- hosts: all - hosts: telegraf
tasks: tasks:
- name: "Installing which on CentOS" - name: "Installing which on CentOS"
yum: yum:

View File

@ -3,14 +3,13 @@ import os
import testinfra.utils.ansible_runner import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( 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") telegraf = Service("telegraf")
assert telegraf.is_enabled assert telegraf.is_enabled
if SystemInfo.distribution == 'centos': assert telegraf.is_running
assert telegraf.is_running
def test_telegraf_dot_conf(File): def test_telegraf_dot_conf(File):
@ -18,7 +17,20 @@ def test_telegraf_dot_conf(File):
assert telegraf.user == "telegraf" assert telegraf.user == "telegraf"
assert telegraf.group == "telegraf" assert telegraf.group == "telegraf"
assert telegraf.mode == 0o640 assert telegraf.mode == 0o640
assert telegraf.contains('interval = "10s"')
assert telegraf.contains('[[inputs.cpu]]') 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): def test_telegraf_dot_d(File):
@ -30,6 +42,6 @@ def test_telegraf_dot_d(File):
assert telegraf.contains('totalcpu = false') assert telegraf.contains('totalcpu = false')
def test_telegraf_package(Package, SystemInfo): def test_telegraf_package(Package):
telegraf = Package('telegraf') telegraf = Package('telegraf')
assert telegraf.is_installed assert telegraf.is_installed

View File

@ -9,7 +9,8 @@
apt: apt:
name: "apt-transport-https" name: "apt-transport-https"
state: present state: present
when: not apt_https_transport.stat.exists when:
- not apt_https_transport.stat.exists
become: yes become: yes
- name: Download Telegraf apt key. - name: Download Telegraf apt key.
@ -25,7 +26,9 @@
filename: "telegraf" filename: "telegraf"
state: present state: present
become: yes 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. - name: Add Telegraf repository.
apt_repository: apt_repository:
@ -33,7 +36,8 @@
filename: "telegraf" filename: "telegraf"
state: present state: present
become: yes 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" - name: "Install telegraf package | Debian"
apt: apt:

View File

@ -3,14 +3,16 @@
- name: Retrieve ec2 facts - name: Retrieve ec2 facts
ec2_facts: ec2_facts:
when: telegraf_agent_aws_tags when:
- telegraf_agent_aws_tags
- name: Retrieve all ec2 tags on the instance - name: Retrieve all ec2 tags on the instance
ec2_tag: ec2_tag:
region: '{{ ansible_ec2_placement_region }}' region: '{{ ansible_ec2_placement_region }}'
resource: '{{ ansible_ec2_instance_id }}' resource: '{{ ansible_ec2_instance_id }}'
state: list state: list
when: telegraf_agent_aws_tags when:
- telegraf_agent_aws_tags
register: ec2_tags register: ec2_tags
- name: "Copy the template for versions < 0.10.0" - name: "Copy the template for versions < 0.10.0"
@ -21,7 +23,8 @@
group: telegraf group: telegraf
mode: 0640 mode: 0640
become: yes 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" notify: "Restart Telegraf"
- name: "Copy the template for versions >= 0.10.0" - name: "Copy the template for versions >= 0.10.0"
@ -32,19 +35,24 @@
group: telegraf group: telegraf
mode: 0640 mode: 0640
become: yes 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" notify: "Restart Telegraf"
- name: "Check if extra plugins directory exists in case of exclusive" - 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 register: telegraf_directory
when: telegraf_plugins_extra_exclusive when:
- telegraf_plugins_extra_exclusive
- name: "Delete telegraf extra plugin path" - name: "Delete telegraf extra plugin path"
file: file:
state: absent state: absent
path: "/etc/telegraf/telegraf.d/" 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 become: yes
notify: "Restart Telegraf" notify: "Restart Telegraf"
@ -54,8 +62,10 @@
path: "/etc/telegraf/telegraf.d/" path: "/etc/telegraf/telegraf.d/"
owner: telegraf owner: telegraf
group: telegraf group: telegraf
mode: 0740 mode: 0755
when: telegraf_plugins_extra_exclusive and telegraf_directory.stat.exists when:
- telegraf_plugins_extra_exclusive
- telegraf_directory.stat.exists
become: yes become: yes
notify: "Restart Telegraf" notify: "Restart Telegraf"
@ -69,7 +79,10 @@
with_dict: "{{ telegraf_plugins_extra }}" with_dict: "{{ telegraf_plugins_extra }}"
loop_control: loop_control:
label: "{{ item.key }}" 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 become: yes
notify: "Restart Telegraf" notify: "Restart Telegraf"
@ -80,7 +93,10 @@
with_dict: "{{ telegraf_plugins_extra }}" with_dict: "{{ telegraf_plugins_extra }}"
loop_control: loop_control:
label: "{{ item.key }}" 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 become: yes
notify: "Restart Telegraf" notify: "Restart Telegraf"