Skip to content

Commit

Permalink
add ansible role for deployments to native machines (#37)
Browse files Browse the repository at this point in the history
* add ansible role for installing as a systemd service, can be easily extended to service types for other distributions

* fixed README
  • Loading branch information
pankajmt authored and erebe committed Feb 4, 2019
1 parent 5846d75 commit 484e03d
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/README.md
@@ -0,0 +1,40 @@
Role Name
=========

Role to install `cassandra exporter java binary` as a `systemd` service

Role Variables
--------------

| Name | Default Value | Description |
| -------------- | ------------- | -----------------------------------|
| `cassandra_exporter_version` | 2.2.0 | Cassandra exporter package version |
| `cassandra_exporter_binary_url` | https://github.com/criteo/cassandra_exporter/releases/download/{{cassandra_exporter_version}}/cassandra_exporter-{{cassandra_exporter_version}}-all.jar | Cassandra exporter jar download location |
| `cassandra_exporter_config_url` | https://raw.githubusercontent.com/criteo/cassandra_exporter/master/config.yml | Cassandra exporter config download location |
| `cassandra_exporter_user` | cassandra-exp | UNIX user to run the binary |
| `cassandra_exporter_group` | cassandra-exp | UNIX group to run the binary |
| `cassandra_exporter_root_dir` | /opt/cassandra_exporter | Base location where cassandra exporter stuff is downloaded |
| `cassandra_exporter_dist_dir` | /opt/cassandra_exporter/dist | Location for binary and systemd service script |
| `cassandra_exporter_config_dir` | /opt/cassandra_exporter/config | Location for config |


Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
become: true
become_method: sudo
roles:
- role: ansible-cassandra-exporter

Usage
-----

You may install the role locally using `ansible-galaxy install -r requirements.yml -p roles/` where `requirements.yml` reads something like

```
- src: git+https://github.com/criteo/cassandra_exporter/ansible/roles
version: TBD
```
13 changes: 13 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/defaults/main.yml
@@ -0,0 +1,13 @@
---

cassandra_exporter_version: 2.2.0

cassandra_exporter_binary_url: 'https://github.com/criteo/cassandra_exporter/releases/download/{{cassandra_exporter_version}}/cassandra_exporter-{{cassandra_exporter_version}}-all.jar'
cassandra_exporter_config_url: 'https://raw.githubusercontent.com/criteo/cassandra_exporter/master/config.yml'

cassandra_exporter_user: cassandra-exp
cassandra_exporter_group: cassandra-exp

cassandra_exporter_root_dir: /opt/cassandra_exporter
cassandra_exporter_dist_dir: "{{ cassandra_exporter_root_dir }}/dist"
cassandra_exporter_config_dir: "{{ cassandra_exporter_root_dir }}/config"
12 changes: 12 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/handlers/main.yml
@@ -0,0 +1,12 @@
---

- name: reenable cassandra exporter service
command: systemctl reenable prometheus-cassandra-exporter.service

- name: reinit cassandra exporter
command: initctl reload-configuration

- name: restart cassandra exporter
service:
name: prometheus-cassandra-exporter
state: restarted
60 changes: 60 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/meta/main.yml
@@ -0,0 +1,60 @@
galaxy_info:
author: Pankaj M. Tolani
description: DevOps Engineer
company: Autonomous Thingz Pty Ltd

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - GPLv3
# - Apache
# - CC-BY
license: license (GPLv2, CC-BY, etc)

min_ansible_version: 2.4

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

# Optionally specify the branch Galaxy will use when accessing the GitHub
# repo for this role. During role install, if no tags are available,
# Galaxy will use this branch. During import Galaxy will access files on
# this branch. If Travis integration is configured, only notifications for this
# branch will be accepted. Otherwise, in all cases, the repo's default branch
# (usually master) will be used.
#github_branch:

#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: Cassandra, JMX, Exporter, Systemd, Service
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
78 changes: 78 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/tasks/main.yml
@@ -0,0 +1,78 @@
---

- name: 'create cassandra exporter group'
group:
name: '{{ cassandra_exporter_group }}'
system: yes
state: present

- name: 'create cassandra exporter user'
user:
name: '{{ cassandra_exporter_user }}'
system: yes
shell: '/sbin/nologin'
group: '{{ cassandra_exporter_group }}'
createhome: no

- name: 'create cassandra exporter directories'
file:
path: '{{ item }}'
state: directory
owner: '{{ cassandra_exporter_user }}'
group: '{{ cassandra_exporter_group }}'
mode: 0755
with_items:
- '{{ cassandra_exporter_root_dir }}'
- '{{ cassandra_exporter_dist_dir }}'

- name: 'download cassandra exporter binary'
get_url:
url: '{{ cassandra_exporter_binary_url }}'
dest: '{{ cassandra_exporter_dist_dir }}/cassandra.jar'

- name: 'download cassandra exporter config'
get_url:
url: '{{ cassandra_exporter_config_url }}'
dest: '{{ cassandra_exporter_config_dir }}/config.yml'

- name: 'generate the shell script'
template:
src: cassandra_exporter.sh.j2
dest: '{{ cassandra_exporter_dist_dir }}/cassandra_exporter.sh'
owner: root
group: root
mode: 0755
register: prometheus_cassandra_exporter_updated
notify:
- restart cassandra exporter

- name: 'update group and owner for files'
file:
path: '{{ item }}'
state: file
owner: '{{ cassandra_exporter_user }}'
group: '{{ cassandra_exporter_group }}'
with_items:
- '{{ cassandra_exporter_dist_dir }}/cassandra.jar'
- '{{ cassandra_exporter_config_dir }}/config.yml'
when: prometheus_cassandra_exporter_updated is changed

- name: 'create systemd service unit'
template:
src: etc/systemd/system/prometheus-cassandra-exporter.service.j2
dest: /etc/systemd/system/prometheus-cassandra-exporter.service
owner: root
group: root
mode: 0644
when: service_mgr | default(ansible_service_mgr) == 'systemd'
notify:
- reenable cassandra exporter service
- restart cassandra exporter

- meta: flush_handlers

- name: 'ensure prometheus cassandra exporter service is enabled and started'
service:
name: prometheus-cassandra-exporter
state: started
enabled: yes
@@ -0,0 +1,2 @@
#!/bin/bash
java -jar {{ cassandra_exporter_dist_dir }}/cassandra.jar {{ cassandra_exporter_config_dir }}/config.yml
@@ -0,0 +1,15 @@
[Unit]
Description=Prometheus Cassandra Exporter
After=network.target

[Service]
Type=simple
User={{ cassandra_exporter_user }}
Group={{ cassandra_exporter_group }}
ExecStart={{ cassandra_exporter_dist_dir }}/cassandra_exporter.sh

SyslogIdentifier=prometheus_cassandra_exporter
Restart=always

[Install]
WantedBy=multi-user.target
2 changes: 2 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/tests/inventory
@@ -0,0 +1,2 @@
localhost

6 changes: 6 additions & 0 deletions ansible/roles/ansible-cassandra-exporter/tests/test.yml
@@ -0,0 +1,6 @@
---
- hosts: localhost
become: true
become_method: sudo
roles:
- ansible-cassandra-exporter
1 change: 1 addition & 0 deletions ansible/roles/ansible-cassandra-exporter/vars/main.yml
@@ -0,0 +1 @@
---

0 comments on commit 484e03d

Please sign in to comment.