Skip to content

Commit

Permalink
Merge pull request #1 from arruko/feature/jhg/ANS-001
Browse files Browse the repository at this point in the history
ANS-001
  • Loading branch information
arruko authored Sep 25, 2018
2 parents 5414bbc + 5da1c8c commit 80df773
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 0 deletions.
13 changes: 13 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
logstash_major_ver: 6.x
logstash_repo_key: https://artifacts.elastic.co/GPG-KEY-elasticsearch
logstash_deb_repo: "deb https://artifacts.elastic.co/packages/{{ logstash_major_ver }}/apt stable main"
logstash_listen_port_beats: 5044
logstash_local_syslog_path: /var/log/syslog
logstash_monitor_local_syslog: true
logstash_enabled_on_boot: yes
logstash_min_memory_required: 2048
logstash_elasticsearch_hosts: "{{ groups['elasticsearch'] | map('extract', hostvars, ['ansible_host']) | list }}"
logstash_elasticsearch_inventory_group_name: elasticsearch
logstash_install_plugins:
- logstash-input-beats
7 changes: 7 additions & 0 deletions files/filters/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
filter {
if [type] == "nginx" {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
}
}
16 changes: 16 additions & 0 deletions files/filters/syslog.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
filter {
if [type] == "syslog" {
if [message] =~ /last message repeated [0-9]+ times/ {
drop { }
}
grok {
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
add_field => [ "received_at", "%{@timestamp}" ]
add_field => [ "received_from", "%{host}" ]
}
syslog_pri { }
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
}
3 changes: 3 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: restart logstash
service: name=logstash state=restarted
41 changes: 41 additions & 0 deletions tasks/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- name: config | create Logstash configuration files
template:
src: "{{ item }}.j2"
dest: "/etc/logstash/conf.d/{{ item }}"
owner: root
group: root
mode: 0644
with_items:
- 01-beats-input.conf
- 03-elasticsearch-output.conf
notify: restart logstash

- name: config | create Logstash filters
copy:
src: "filters/{{ item }}"
dest: "/etc/logstash/conf.d/{{ item }}"
owner: root
group: root
mode: 0644
with_items:
- syslog.conf
- nginx.conf
notify: restart logstash

- name: config | create Logstash configuration file for local syslog
template:
src: 02-local-syslog-input.conf.j2
dest: /etc/logstash/conf.d/02-local-syslog-input.conf
owner: root
group: root
mode: 0644
when: logstash_monitor_local_syslog
notify: restart logstash

- name: config | ensure configuration for local syslog is absent if disabled
file:
path: /etc/logstash/conf.d/02-local-syslog-input.conf
state: absent
when: not logstash_monitor_local_syslog
notify: restart logstash
26 changes: 26 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Test distribution
assert:
that: >
ansible_os_family == 'RedHat' or
ansible_os_family == 'Debian'
- name: Include OS Specific setup tasks
include: setup-{{ ansible_os_family }}.yml

- name: Checking To Ensure Node Meets Minimum Specs
pause:
prompt: >
"Node Does Not Meet Minimum Recommend Memory Requirements of
{{ logstash_min_memory_required }}MB"
seconds: 10
when: ansible_memtotal_mb < logstash_min_memory_required

- include: config.yml
- include: plugins.yml

- name: Ensure Logstash is started and enabled on boot.
service:
name: logstash
state: started
enabled: "{{ logstash_enabled_on_boot }}"
23 changes: 23 additions & 0 deletions tasks/plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: plugins | Setting Logstash plugins facts
set_fact:
logstash_plugin_bin: '/usr/share/logstash/bin/logstash-plugin'
logstash_bin: '/usr/share/logstash/bin/logstash'
when: logstash_major_ver == '5.x'

- name: plugins | collecting installed plugins
command: "{{ logstash_plugin_bin }} list"
become: true
register: "logstash_plugins_installed"
changed_when: false

- name: plugins | Currently Installed Plugins
debug: var=logstash_plugins_installed.stdout_lines
changed_when: false

- name: plugins | installing logstash plugins
shell: "{{ logstash_plugin_cmd_vars }} {{ logstash_plugin_bin }} install {{ item }}"
become: true
notify: "restart logstash"
with_items: '{{ logstash_install_plugins }}'
when: item not in logstash_plugins_installed.stdout
34 changes: 34 additions & 0 deletions tasks/setup-Debian.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Debian | add apt-transport-https
apt:
name: apt-transport-https
state: present

- name: Debian | add Elasticsearch apt key.
apt_key:
url: "{{ logstash_repo_key }}"
state: present

- name: Debian | add Logstash repository.
apt_repository:
repo: "{{ logstash_deb_repo }}"
state: present

- name: Debian | check if Logstash is already installed.
stat: path=/etc/init.d/logstash
register: logstash_installed

- name: Debian | update apt cache if repository just added.
apt: update_cache=yes
when: logstash_installed.stat.exists == false

- name: Debian | install Logstash.
apt: pkg=logstash state=present

- name: Debian | add Logstash user to adm group (Debian).
user:
name: logstash
group: logstash
groups: adm
when: ansible_os_family == "Debian"
notify: restart logstash
14 changes: 14 additions & 0 deletions tasks/setup-RedHat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: RHEL | add Elasticsearch GPG key.
rpm_key:
key: http://packages.elasticsearch.org/GPG-KEY-elasticsearch
state: present

- name: RHEL | add Logstash repository.
copy:
src: logstash.repo
dest: /etc/yum.repos.d/logstash.repo
mode: 0644

- name: RHEL | install Logstash.
yum: pkg=logstash state=installed
5 changes: 5 additions & 0 deletions templates/01-beats-input.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
input {
beats {
port => {{ logstash_listen_port_beats }}
}
}
5 changes: 5 additions & 0 deletions templates/02-local-syslog-input.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
input {
file {
path => "{{ logstash_local_syslog_path }}"
}
}
7 changes: 7 additions & 0 deletions templates/03-elasticsearch-output.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output {
elasticsearch {
hosts => {{ logstash_elasticsearch_hosts | to_json }}
index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
document_type => "%{[@metadata][type]}"
}
}

0 comments on commit 80df773

Please sign in to comment.