Compare commits

..

10 commits

26 changed files with 318 additions and 378 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
.kitchen/* .kitchen/*
*.pyc

View file

@ -1,37 +0,0 @@
---
# <% os = ENV['OS'] || 'debian9' %>
# <% ansible = ENV['ANSIBLE_VERSION'] || '2.8' %>
# <% command = os == 'centos7' ? '/usr/sbin/init' : '/bin/systemd' %>
driver:
name: docker_cli
transport:
name: docker_cli
provisioner:
name: ansible_playbook
hosts: localhost
require_ansible_repo: false
require_ansible_omnibus: false
require_chef_for_busser: true
ansible_verbose: false
ansible_inventory: ./test/integration/inventory
platforms:
- name: <%= os %>
driver_config:
image: "nishiki/<%= os %>:ansible-<%= ansible %>"
command: <%= command %>
volume:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
security_opt: seccomp=unconfined
# <% if os == 'debian9' %>
provisioner:
extra_vars:
sensu_repository_system: ubuntu
sensu_repository_release: xenial
# <% end %>
suites:
- name: default

View file

@ -1,38 +0,0 @@
---
AllCops:
Exclude:
- db/**/*
- config/**/*
- Vagrantfile
TargetRubyVersion: 2.4
Naming/AccessorMethodName:
Enabled: false
Lint/RescueWithoutErrorClass:
Enabled: false
Metrics/LineLength:
Max: 120
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/AbcSize:
Enabled: false
Style/NumericLiteralPrefix:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Style/CommandLiteral:
Enabled: true
EnforcedStyle: percent_x
Style/Documentation:
Enabled: false

View file

@ -1,32 +1,29 @@
--- ---
sudo: required sudo: required
language: ruby dist: bionic
addons:
apt:
packages:
- python3
- python3-pip
- python3-setuptools
env: env:
- OS=debian9 ANSIBLE_VERSION=2.5 - ANSIBLE_VERSION=2.7.16
- OS=debian9 ANSIBLE_VERSION=2.6 - ANSIBLE_VERSION=2.8.8
- OS=debian9 ANSIBLE_VERSION=2.7 - ANSIBLE_VERSION=2.9.4
- OS=debian9 ANSIBLE_VERSION=2.8
- OS=ubuntu18.04 ANSIBLE_VERSION=2.7
- OS=ubuntu16.04 ANSIBLE_VERSION=2.7
- OS=centos7 ANSIBLE_VERSION=2.7
services: services:
- docker - docker
before_install: before_install:
- bundle install - sudo pip3 install ansible==${ANSIBLE_VERSION}
- sudo pip install --upgrade pip - sudo pip3 install molecule 'molecule[docker]' docker testinfra ansible-lint yamllint
- sudo pip install yamllint
- sudo pip install ansible-lint
- git clone https://github.com/ansible/galaxy-lint-rules.git - git clone https://github.com/ansible/galaxy-lint-rules.git
script: script:
- kitchen conv - ansible --version
- kitchen conv | grep changed=0 - molecule test
- kitchen verify
- ansible-lint -r galaxy-lint-rules/rules .
- yamllint .
notifications: notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/ webhooks: https://galaxy.ansible.com/api/v1/notifications/

View file

@ -5,6 +5,29 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased] ## [Unreleased]
## [1.4.0] - 2020-08-09
### Feat
- add hooks
### Fixed
- add full python3 support
- remove useless no_log variable
## [1.3.0] - 2020-04-04
### Fixed
- add compatibily with new sensu version
- remove no_log warnings with sensu_go libraries
### Changed
- test: replace kitchen to molecule
- add loop label for sensu_go modules
## [1.2.0] - 2019-06-22 ## [1.2.0] - 2019-06-22
- feat: add support for ansible 2.8 - feat: add support for ansible 2.8

View file

@ -1,8 +0,0 @@
source 'https://rubygems.org'
group :development do
gem 'kitchen-ansible'
gem 'kitchen-docker_cli'
gem 'rubocop', '0.50.0'
gem 'test-kitchen'
end

View file

@ -1,130 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
bcrypt_pbkdf (1.0.1)
builder (3.2.3)
ed25519 (1.2.4)
equatable (0.6.0)
erubis (2.7.0)
ffi (1.11.1)
gssapi (1.3.0)
ffi (>= 1.0.1)
gyoku (1.3.1)
builder (>= 2.1.2)
httpclient (2.8.3)
kitchen-ansible (0.50.0)
net-ssh (>= 3)
test-kitchen (>= 1.4)
kitchen-docker_cli (0.19.0)
test-kitchen (>= 1.3)
license-acceptance (1.0.13)
pastel (~> 0.7)
tomlrb (~> 1.2)
tty-box (~> 0.3)
tty-prompt (~> 0.18)
little-plugger (1.1.4)
logging (2.2.2)
little-plugger (~> 1.1)
multi_json (~> 1.10)
mixlib-install (3.11.18)
mixlib-shellout
mixlib-versioning
thor
mixlib-shellout (2.4.4)
mixlib-versioning (1.2.7)
multi_json (1.13.1)
necromancer (0.5.0)
net-scp (2.0.0)
net-ssh (>= 2.6.5, < 6.0.0)
net-ssh (5.2.0)
net-ssh-gateway (2.0.0)
net-ssh (>= 4.0.0)
nori (2.6.0)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
pastel (0.7.3)
equatable (~> 0.6)
tty-color (~> 0.5)
powerpack (0.1.2)
rainbow (2.2.2)
rake
rake (12.3.2)
rubocop (0.50.0)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.10.1)
rubyntlm (0.6.2)
rubyzip (1.2.3)
strings (0.1.5)
strings-ansi (~> 0.1)
unicode-display_width (~> 1.5)
unicode_utils (~> 1.4)
strings-ansi (0.1.0)
test-kitchen (2.2.5)
bcrypt_pbkdf (~> 1.0)
ed25519 (~> 1.2)
license-acceptance (~> 1.0, >= 1.0.11)
mixlib-install (~> 3.6)
mixlib-shellout (>= 1.2, < 3.0)
net-scp (>= 1.1, < 3.0)
net-ssh (>= 2.9, < 6.0)
net-ssh-gateway (>= 1.2, < 3.0)
thor (~> 0.19)
winrm (~> 2.0)
winrm-elevated (~> 1.0)
winrm-fs (~> 1.1)
thor (0.20.3)
tomlrb (1.2.8)
tty-box (0.4.0)
pastel (~> 0.7.2)
strings (~> 0.1.5)
tty-cursor (~> 0.7)
tty-color (0.5.0)
tty-cursor (0.7.0)
tty-prompt (0.19.0)
necromancer (~> 0.5.0)
pastel (~> 0.7.0)
tty-reader (~> 0.6.0)
tty-reader (0.6.0)
tty-cursor (~> 0.7)
tty-screen (~> 0.7)
wisper (~> 2.0.0)
tty-screen (0.7.0)
unicode-display_width (1.6.0)
unicode_utils (1.4.0)
winrm (2.3.2)
builder (>= 2.1.2)
erubis (~> 2.7)
gssapi (~> 1.2)
gyoku (~> 1.0)
httpclient (~> 2.2, >= 2.2.0.2)
logging (>= 1.6.1, < 3.0)
nori (~> 2.0)
rubyntlm (~> 0.6.0, >= 0.6.1)
winrm-elevated (1.1.1)
winrm (~> 2.0)
winrm-fs (~> 1.0)
winrm-fs (1.3.2)
erubis (~> 2.7)
logging (>= 1.6.1, < 3.0)
rubyzip (~> 1.1)
winrm (~> 2.0)
wisper (2.0.0)
PLATFORMS
ruby
DEPENDENCIES
kitchen-ansible
kitchen-docker_cli
rubocop (= 0.50.0)
test-kitchen
BUNDLED WITH
1.16.0

View file

@ -1,5 +1,5 @@
# Ansible role: Sensu # Ansible role: Sensu
[![Version](https://img.shields.io/badge/latest_version-1.2.0-green.svg)](https://git.yaegashi.fr/nishiki/ansible-role-sensu/releases) [![Version](https://img.shields.io/badge/latest_version-1.4.0-green.svg)](https://git.yaegashi.fr/nishiki/ansible-role-sensu/releases)
[![Build Status](https://travis-ci.org/nishiki/ansible-role-sensu.svg?branch=master)](https://travis-ci.org/nishiki/ansible-role-sensu) [![Build Status](https://travis-ci.org/nishiki/ansible-role-sensu.svg?branch=master)](https://travis-ci.org/nishiki/ansible-role-sensu)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://git.yaegashi.fr/nishiki/ansible-role-sensu/src/branch/master/LICENSE) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://git.yaegashi.fr/nishiki/ansible-role-sensu/src/branch/master/LICENSE)
@ -67,7 +67,6 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
### Backend ### Backend
* `sensu_no_log` - you can disable ansible no_log to print the errors with the backend api (default: `true`)
* `sensu_backend` - enable sensu backend installation (default: `no`) * `sensu_backend` - enable sensu backend installation (default: `no`)
* `sensu_backend_config` - hash with the optional configuration ([see sensu configuration](https://docs.sensu.io/sensu-go/latest/reference/backend/)) * `sensu_backend_config` - hash with the optional configuration ([see sensu configuration](https://docs.sensu.io/sensu-go/latest/reference/backend/))
* `sensu_assets` - array with the asset definitions * `sensu_assets` - array with the asset definitions
@ -131,6 +130,16 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
timeout: 10 timeout: 10
``` ```
* `sensu_hooks` - array with the hook definitions
```
- name: restart-apache
namespace:
- default
command: /etc/init.d/apache2 restart
timeout: 10
```
* `sensu_filters` - array with the filter definitions * `sensu_filters` - array with the filter definitions
``` ```
@ -233,25 +242,13 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
``` ```
## Development ## Development
### Test syntax with yamllint
* install `python` and `python-pip` ### Test with molecule and docker
* install yamllint `pip install yamllint`
* run `yamllint .`
### Test syntax with ansible-lint
* install `python` and `python-pip`
* install yamllint `pip install ansible-lint`
* run `ansible-lint .`
### Tests with docker
* install [docker](https://docs.docker.com/engine/installation/) * install [docker](https://docs.docker.com/engine/installation/)
* install ruby * install `python3` and `python3-pip`
* install bundler `gem install bundler` * install molecule and dependencies `pip3 install molecule 'molecule[docker]' docker ansible-lint testinfra yamllint`
* install dependencies `bundle install` * run `molecule test`
* run the tests `kitchen test`
## License ## License

View file

@ -2,8 +2,6 @@
sensu_agent: yes sensu_agent: yes
sensu_backend: no sensu_backend: no
sensu_no_log: true
sensu_repository_system: '{{ ansible_distribution|lower }}' sensu_repository_system: '{{ ansible_distribution|lower }}'
sensu_repository_release: '{{ ansible_distribution_release }}' sensu_repository_release: '{{ ansible_distribution_release }}'
@ -56,3 +54,4 @@ sensu_filters: []
sensu_users: [] sensu_users: []
sensu_cluster_roles: [] sensu_cluster_roles: []
sensu_mutators: [] sensu_mutators: []
sensu_hooks: []

View file

@ -21,7 +21,7 @@ class SensuAsset:
} }
def has_changed(self, options): def has_changed(self, options):
for option, value in self.options.iteritems(): for option, value in self.options.items():
if (options[option] or value) and options[option] != value: if (options[option] or value) and options[option] != value:
return True return True
@ -49,7 +49,7 @@ def main():
'filters': { 'type': 'list', 'default': [] }, 'filters': { 'type': 'list', 'default': [] },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)
changed = True changed = True

View file

@ -20,10 +20,13 @@ class SensuCheck:
return {} return {}
def labels_has_changed(self, new_labels, old_labels): def labels_has_changed(self, new_labels, old_labels):
if old_labels is None or len(new_labels) != len(old_labels): if not old_labels and not new_labels:
return False
if len(new_labels) != len(old_labels):
return True return True
for old_label, old_value in old_labels.iteritems(): for old_label, old_value in old_labels.items():
if old_label in new_labels and new_labels[old_label] == old_value: if old_label in new_labels and new_labels[old_label] == old_value:
continue continue
return True return True
@ -36,7 +39,7 @@ class SensuCheck:
return True return True
data.pop('metadata') data.pop('metadata')
for option, value in data.iteritems(): for option, value in data.items():
if not option in options: if not option in options:
if value: if value:
return True return True
@ -77,7 +80,7 @@ def main():
'options': { 'type': 'dict', 'default': {} }, 'options': { 'type': 'dict', 'default': {} },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -22,14 +22,14 @@ class SensuClusterRole:
return True return True
for i in range(0, len(self.options['rules'])): for i in range(0, len(self.options['rules'])):
for rule, value in self.options['rules'][i].iteritems(): for rule, value in self.options['rules'][i].items():
if not rule in options['rules'][i]: if not rule in options['rules'][i]:
if value: if value:
return True return True
elif options['rules'][i][rule] != value: elif options['rules'][i][rule] != value:
return True return True
for rule, value in options['rules'][i].iteritems(): for rule, value in options['rules'][i].items():
if not rule in self.options['rules'][i]: if not rule in self.options['rules'][i]:
if value: if value:
return True return True
@ -63,7 +63,7 @@ def main():
'rules': { 'type': 'list', 'required': True }, 'rules': { 'type': 'list', 'required': True },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -18,7 +18,7 @@ class SensuFilter:
self.options.pop('metadata') self.options.pop('metadata')
def has_changed(self, options): def has_changed(self, options):
for option, value in self.options.iteritems(): for option, value in self.options.items():
if not option in options: if not option in options:
if value: if value:
return True return True
@ -55,7 +55,7 @@ def main():
'runtime_assets': { 'type': 'list', 'default': [] }, 'runtime_assets': { 'type': 'list', 'default': [] },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -18,7 +18,7 @@ class SensuHandler:
self.options.pop('metadata') self.options.pop('metadata')
def has_changed(self, options): def has_changed(self, options):
for option, value in self.options.iteritems(): for option, value in self.options.items():
if not option in options: if not option in options:
if value: if value:
return True return True
@ -56,7 +56,7 @@ def main():
'options': { 'type': 'dict', 'default': {} }, 'options': { 'type': 'dict', 'default': {} },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

95
library/sensugo_hook.py Normal file
View file

@ -0,0 +1,95 @@
#!/usr/bin/python
from ansible.module_utils.basic import *
from ansible.module_utils.sensu_api import *
class SensuHook:
def __init__(self, api, name, namespace):
self.api = api
self.name = name
self.namespace = namespace
self.exists = False
def get_data(self):
status_code, data = self.api.get('namespaces/{}/hooks/{}'.format(self.namespace, self.name))
if status_code == 200:
self.exists = True
return data
return {}
def has_changed(self, options):
data = self.get_data()
data.pop('metadata')
for option, value in data.items():
if not option in options:
if value:
return True
elif options[option] != value:
return True
return False
def create(self, options):
options.update({
'metadata': {
'name': self.name,
'namespace': self.namespace
}
})
self.api.put(
'namespaces/{}/hooks/{}'.format(self.namespace, self.name),
options
)
def delete(self):
self.api.delete(
'namespaces/{}/hooks/{}'.format(self.namespace, self.name)
)
def main():
fields = {
'name': { 'type': 'str', 'required': True },
'namespaces': { 'type': 'list', 'default': ['default'] },
'command': { 'type': 'str', 'required': True },
'timeout': { 'type': 'int', 'default': 10 },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
}
module = AnsibleModule(argument_spec=fields)
changed = False
options = {
'command': module.params['command'],
'timeout': module.params['timeout']
}
api = SensuApi(
module.params['api_url'],
module.params['api_user'],
module.params['api_password']
)
api.auth()
for namespace in module.params['namespaces']:
hook = SensuHook(
api,
module.params['name'],
namespace
)
hook.get_data()
if module.params['state'] == 'present':
if not hook.exists or hook.has_changed(options):
hook.create(options)
changed = True
elif hook.exists:
hook.delete()
changed = True
module.exit_json(changed=changed)
if __name__ == '__main__':
main()

View file

@ -18,7 +18,7 @@ class SensuMutator:
self.options.pop('metadata') self.options.pop('metadata')
def has_changed(self, options): def has_changed(self, options):
for option, value in self.options.iteritems(): for option, value in self.options.items():
if not option in options: if not option in options:
if value: if value:
return True return True
@ -54,7 +54,7 @@ def main():
'options': { 'type': 'dict', 'default': {} }, 'options': { 'type': 'dict', 'default': {} },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -31,7 +31,7 @@ def main():
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -21,7 +21,7 @@ class SensuUser:
pass pass
def has_changed(self, options): def has_changed(self, options):
for option, value in self.options.iteritems(): for option, value in self.options.items():
if options[option] != value: if options[option] != value:
return True return True
@ -55,10 +55,10 @@ def main():
fields = { fields = {
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'groups': { 'type': 'list', 'default': [] }, 'groups': { 'type': 'list', 'default': [] },
'password': { 'type': 'str', 'default': None }, 'password': { 'type': 'str', 'default': None, 'no_log': True },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' }, 'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' }, 'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' }, 'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] } 'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
} }
module = AnsibleModule(argument_spec=fields) module = AnsibleModule(argument_spec=fields)

View file

@ -19,7 +19,7 @@ class SensuApi:
if r.status_code == 401: if r.status_code == 401:
raise Exception('Authentification has failed') raise Exception('Authentification has failed')
self.headers = { 'Authorization': r.json()['access_token'] } self.headers = { 'Authorization': 'Bearer %s' % (r.json()['access_token']) }
def get(self, path): def get(self, path):
r = requests.get( r = requests.get(

View file

@ -1,8 +1,11 @@
--- ---
- hosts: default - name: Converge
connection: local hosts: all
roles:
- ansible-role-sensu
vars: vars:
sensu_no_log: false sensu_repository_system: ubuntu
sensu_repository_release: xenial
sensu_backend: yes sensu_backend: yes
sensu_agent_redact: sensu_agent_redact:
- supersecret - supersecret
@ -49,6 +52,12 @@
namespaces: namespaces:
- production - production
- dev - dev
sensu_hooks:
- name: restart-apache
command: /etc/init.d/apache2 restart
namespaces:
- production
- dev
sensu_checks: sensu_checks:
- name: ping - name: ping
command: ping -c 1 127.0.0.1 command: ping -c 1 127.0.0.1
@ -67,6 +76,3 @@
- list - list
resources: resources:
- '*' - '*'
roles:
- ansible-role-sensu

View file

@ -0,0 +1,18 @@
---
driver:
name: docker
platforms:
- name: debian10
image: nishiki/debian10:molecule
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
command: /bin/systemd
capabilities:
- SYS_ADMIN
lint: |
set -e
yamllint .
ansible-lint
verifier:
name: testinfra

View file

@ -0,0 +1,64 @@
import re
import testinfra.utils.ansible_runner
def test_packages(host):
for package_name in ['sensu-go-agent', 'sensu-go-cli', 'sensu-go-backend']:
package = host.package(package_name)
assert package.is_installed
def test_services(host):
for service_name in ['sensu-backend', 'sensu-agent']:
service = host.service(service_name)
assert service.is_running
assert service.is_enabled
def test_sockets(host):
for port in [3000, 8080, 8081]:
socket = host.socket('tcp://0.0.0.0:%d' % (port))
assert socket.is_listening
def test_configure_sensuctl(host):
cmd = host.run('sensuctl configure -n --password "P@ssw0rd!" --url http://127.0.0.1:8080 --username admin --format tabular')
assert cmd.succeeded
def test_sensu_namespace(host):
cmd = host.run('sensuctl namespace list')
assert cmd.succeeded
assert 'production' in cmd.stdout
assert 'dev' in cmd.stdout
def test_sensu_user(host):
cmd = host.run('sensuctl user list')
assert cmd.succeeded
assert re.search('johndoe.*\\s+devops,users\\s+.*true', cmd.stdout)
def test_sensu_entity(host):
cmd = host.run('sensuctl entity info debian10 --format json')
assert cmd.succeeded
assert '"supersecret": "REDACTED"' in cmd.stdout
def test_sensu_check(host):
for namespace in ['production', 'dev']:
cmd = host.run('sensuctl asset list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('superasset.*\\s+.*test.sh\\s+cf83e13', cmd.stdout)
cmd = host.run('sensuctl mutator list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('transform.*\\s+.*/path/value_to_csv', cmd.stdout)
cmd = host.run('sensuctl handler list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('mail.*\\s+pipe\\s+.*echo test | mail -s coucou\\s+', cmd.stdout)
cmd = host.run('sensuctl check list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('ping.*\\s+ping -c 1 127.0.0.1\\s+60\\s+.*\\s+linux\\s+', cmd.stdout)
cmd = host.run('sensuctl filter list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('state_changed.*\\s+allow\\s+\\(event.check.occurrences == 1\\)', cmd.stdout)
cmd = host.run('sensuctl hook list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('restart-apache.*\\s+/etc/init.d/apache2 restart\\s+10\\s+false', cmd.stdout)

View file

@ -1,7 +1,9 @@
--- ---
- name: install backend dependencies - name: install backend dependencies
package: package:
name: python-requests name:
- python-requests
- python3-requests
register: result register: result
retries: 3 retries: 3
delay: 1 delay: 1

View file

@ -37,6 +37,24 @@
delay: 5 delay: 5
tags: sensu tags: sensu
- name: init backend
command:
cmd: sensu-backend init
creates: /etc/sensu/.backend-init
environment:
SENSU_BACKEND_CLUSTER_ADMIN_USERNAME: '{{ sensu_api_user }}'
SENSU_BACKEND_CLUSTER_ADMIN_PASSWORD: '{{ sensu_api_password }}'
tags: sensu
- name: hold init
copy:
content: ''
dest: /etc/sensu/.backend-init
owner: root
group: root
mode: 0600
tags: sensu
- name: manage cluster roles - name: manage cluster roles
sensugo_cluster_role: sensugo_cluster_role:
name: '{{ item.name }}' name: '{{ item.name }}'
@ -46,8 +64,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_cluster_roles }}' loop: '{{ sensu_cluster_roles }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage users - name: manage users
@ -60,8 +79,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_users }}' loop: '{{ sensu_users }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage namespaces - name: manage namespaces
@ -72,8 +92,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_namespaces }}' loop: '{{ sensu_namespaces }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage assets - name: manage assets
@ -87,8 +108,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_assets }}' loop: '{{ sensu_assets }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage filters - name: manage filters
@ -103,8 +125,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_filters }}' loop: '{{ sensu_filters }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage mutators - name: manage mutators
@ -117,8 +140,9 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_mutators }}' loop: '{{ sensu_mutators }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage handlers - name: manage handlers
@ -133,8 +157,24 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_handlers }}' loop: '{{ sensu_handlers }}'
loop_control:
label: '{{ item.name }}'
run_once: true
tags: sensu
- name: manage hooks
sensugo_hook:
name: '{{ item.name }}'
namespaces: '{{ item.namespaces|default(["default"]) }}'
command: '{{ item.command }}'
timeout: '{{ item.timeout|default(10) }}'
api_url: '{{ sensu_api_url }}'
api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_hooks }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu
- name: manage checks - name: manage checks
@ -152,6 +192,7 @@
api_user: '{{ sensu_api_user }}' api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}' api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_checks }}' loop: '{{ sensu_checks }}'
loop_control:
label: '{{ item.name }}'
run_once: true run_once: true
no_log: '{{ sensu_no_log }}'
tags: sensu tags: sensu

View file

@ -1,90 +0,0 @@
require 'serverspec'
set :backend, :exec
puts
puts '================================'
puts %x(ansible --version)
puts '================================'
%w[
sensu-go-agent
sensu-go-cli
sensu-go-backend
].each do |package|
describe package(package) do
it { should be_installed }
end
end
%w[
sensu-agent
sensu-backend
].each do |service|
describe service(service) do
it { should be_enabled }
it { should be_running }
it { should be_running.under('systemd') }
end
end
[3000, 8080, 8081].each do |port|
describe port(port) do
it { should be_listening.with('tcp6') }
end
end
describe command(
'sensuctl configure -n --password "P@ssw0rd!" ' \
'--url http://127.0.0.1:8080 --username admin --format tabular'
) do
its(:exit_status) { should eq 0 }
end
describe command('sensuctl namespace list') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match 'production' }
its(:stdout) { should match 'dev' }
end
describe command('sensuctl user list') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/johndoe.*\s+devops,users\s+.*true/) }
end
describe command('sensuctl entity info localhost --format json') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/"supersecret": "REDACTED"/) }
end
%w[production dev].each do |namespace|
describe command("sensuctl asset list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) }
end
describe command("sensuctl mutator list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(%r{transform.*\s+.*/path/value_to_csv}) }
end
describe command("sensuctl handler list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) }
end
describe command("sensuctl check list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/ping.*\s+ping -c 1 127.0.0.1\s+60\s+.*\s+linux\s+/) }
end
describe command("sensuctl filter list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/state_changed.*\s+allow\s+event\.check\.occurrences == 1/) }
end
end
describe command('sensuctl cluster-role list') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/view.*\s+1/) }
end

View file

@ -1,2 +0,0 @@
[default]
localhost