parent
f7b202ad7f
commit
7e2f7e5749
@ -1,2 +1,3 @@ |
||||
- import_playbook: ansible.yml |
||||
- import_playbook: hv.yml |
||||
- import_playbook: containers.yml |
@ -0,0 +1,6 @@ |
||||
- hosts: localhost |
||||
gather_facts: no |
||||
serial: 1 |
||||
strategy: linear |
||||
roles: |
||||
- ansible |
@ -1,4 +1,5 @@ |
||||
ansible_connection: docker |
||||
hypervisor_hostname: hv |
||||
|
||||
container_default_config: |
||||
ansible_connection: docker |
||||
ansible_user: root |
||||
ansible_docker_extra_args: "-H tcp://{{ hostvars[hypervisor_hostname]['ansible_host'] ~ ':' ~ docker_remote_port }}" |
||||
|
@ -0,0 +1,25 @@ |
||||
- name: determine distribution |
||||
setup: |
||||
gather_subset: |
||||
- distribution |
||||
|
||||
|
||||
- name: install docker cli |
||||
package: |
||||
name: docker-cli |
||||
when: ansible_distribution == 'Alpine' |
||||
|
||||
|
||||
- block: |
||||
- name: add docker repository |
||||
include_tasks: add_docker_repo.yml |
||||
|
||||
|
||||
- name: install docker cli |
||||
apt: |
||||
name: |
||||
- docker-ce-cli |
||||
force_apt_get: yes |
||||
state: latest |
||||
|
||||
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
@ -0,0 +1,118 @@ |
||||
- name: set container mount dirs |
||||
set_fact: |
||||
container_config_mount: /fluentd/etc/ |
||||
|
||||
|
||||
- name: set additional exposed ports |
||||
set_fact: |
||||
container_role_config: |
||||
exposed_ports: "{{ fluentd_syslog_port }}" |
||||
when: fluentd_syslog_port is defined |
||||
|
||||
|
||||
- name: set fluentd sections |
||||
set_fact: |
||||
fluentd_filters: |
||||
- __postfix: 'nginx.**' |
||||
_type: parser |
||||
key_name: message |
||||
parse: |
||||
_type: json |
||||
json_parser: yajl |
||||
|
||||
fluentd_matches: |
||||
- __postfix: 'nginx.**' |
||||
_type: sql |
||||
adapter: mysql2 |
||||
_log_level: trace |
||||
port: 3306 |
||||
host: "{{ hostvars['mysql']['container_address'] | d(hostvars['mysql']['ansible_host']) }}" |
||||
database: "{{ fluentd_database_name }}" |
||||
username: "{{ fluentd_database_user }}" |
||||
password: "{{ fluentd_database_pass }}" |
||||
flush_interval: 1s |
||||
table: |
||||
table: "{{ fluentd_database_table }}" |
||||
column_mapping: "time_iso8601:time_iso8601,\ |
||||
request_time:request_time,\ |
||||
upstream_response_time:upstream_response_time,\ |
||||
remote_addr:remote_addr,\ |
||||
remote_user:remote_user,\ |
||||
time_local:time_local,\ |
||||
request:request,\ |
||||
status:status,\ |
||||
body_bytes_sent:body_bytes_sent,\ |
||||
http_referer:http_referer,\ |
||||
http_user_agent:http_user_agent,\ |
||||
geoip_country_code:geoip_country_code" |
||||
|
||||
|
||||
- block: |
||||
- name: initialize ansible mysql connection |
||||
include_tasks: init_mysql.yml |
||||
|
||||
|
||||
- name: create fluentd database |
||||
mysql_db: |
||||
name: "{{ fluentd_database_name }}" |
||||
encoding: utf8 |
||||
login_host: "{{ hostvars['mysql']['container_address'] | d(hostvars['mysql']['ansible_host']) }}" |
||||
login_user: "{{ hostvars['mysql']['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars['mysql']['mysql_root_password'] | d(omit) }}" |
||||
|
||||
|
||||
- name: create fluentd database user |
||||
mysql_user: |
||||
name: "{{ fluentd_database_user }}" |
||||
password: "{{ fluentd_database_pass }}" |
||||
plugin: mysql_native_password |
||||
host: "{{ container_address | d(ansible_host) }}" |
||||
priv: "{{ fluentd_database_name }}.*:ALL" |
||||
|
||||
login_host: "{{ hostvars['mysql']['container_address'] | d(hostvars['mysql']['ansible_host']) }}" |
||||
login_user: "{{ hostvars['mysql']['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars['mysql']['mysql_root_password'] | d(omit) }}" |
||||
register: result |
||||
|
||||
|
||||
- name: flush privileges |
||||
mysql_query: |
||||
query: FLUSH PRIVILEGES |
||||
login_host: "{{ hostvars['mysql']['container_address'] | d(hostvars['mysql']['ansible_host']) }}" |
||||
login_user: "{{ hostvars['mysql']['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars['mysql']['mysql_root_password'] | d(omit) }}" |
||||
when: result.changed |
||||
|
||||
|
||||
- name: initialize mysql database |
||||
mysql_query: |
||||
query: | |
||||
CREATE TABLE IF NOT EXISTS {{ fluentd_database_table | quote }} ( |
||||
time_iso8601 DATETIME, |
||||
request_time FLOAT, |
||||
upstream_response_time FLOAT, |
||||
remote_addr TEXT, |
||||
remote_user TEXT, |
||||
time_local TEXT, |
||||
request TEXT, |
||||
status INTEGER, |
||||
body_bytes_sent INTEGER, |
||||
http_referer TEXT, |
||||
http_user_agent TEXT, |
||||
geoip_country_code TEXT |
||||
); |
||||
login_db: "{{ fluentd_database_name }}" |
||||
login_host: "{{ hostvars['mysql']['container_address'] | d(hostvars['mysql']['ansible_host']) }}" |
||||
login_user: "{{ hostvars['mysql']['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars['mysql']['mysql_root_password'] | d(omit) }}" |
||||
changed_when: no |
||||
|
||||
delegate_to: localhost |
||||
|
||||
|
||||
- name: template fluentd config |
||||
template: |
||||
src: fluentd.j2 |
||||
dest: "{{ (conf_dir, 'fluent.conf') | path_join }}" |
||||
lstrip_blocks: yes |
||||
force: yes |
@ -0,0 +1,41 @@ |
||||
{% macro fluentd_option_block(name, values, padding = 2) -%} |
||||
{% if values is mapping -%} |
||||
{% set postfix = '' if values.__postfix is not defined else (' ' + values.__postfix) -%} |
||||
|
||||
<{{ name ~ postfix}}> |
||||
{% for option in values | dict2items -%} |
||||
{% if option.key != '__postfix' -%} |
||||
{{ '' if padding == 0 else (' ' * padding) -}} |
||||
{% if option.value is mapping -%} |
||||
{{ fluentd_option_block(option.key, option.value, padding + 2) -}} |
||||
{% else -%} |
||||
{% if option.key[0] == '_' -%} |
||||
{{ '@' ~ option.key[1:] }} {{ option.value }} |
||||
{% else -%} |
||||
{{ option.key }} {{ option.value }} |
||||
{% endif -%} |
||||
{% endif -%} |
||||
{% endif -%} |
||||
{% endfor -%} |
||||
{{ '' if padding == 0 else (' ' * (padding - 2)) -}}</{{ name }}> |
||||
{% endif -%} |
||||
{% endmacro -%} |
||||
|
||||
|
||||
|
||||
{{ fluentd_option_block('system', fluentd_system | d(None)) }} |
||||
|
||||
|
||||
{% for source in (fluentd_sources | d([])) + (groups['all'] | map('extract', hostvars) | selectattr('fluentd_source', 'mapping') | map(attribute='fluentd_source') | list) -%} |
||||
{{ fluentd_option_block('source', source) }} |
||||
{% endfor -%} |
||||
|
||||
|
||||
{% for filter in (fluentd_filters | d([])) -%} |
||||
{{ fluentd_option_block('filter', filter) }} |
||||
{% endfor -%} |
||||
|
||||
|
||||
{% for match in (fluentd_matches | d([])) -%} |
||||
{{ fluentd_option_block('match', match) }} |
||||
{% endfor -%} |
@ -0,0 +1,12 @@ |
||||
- name: set container cfg |
||||
set_fact: |
||||
container_logs_mount: /var/lib/mysql/mysql-bin |
||||
container_role_config: |
||||
env: |
||||
MYSQL_ROOT_PASSWORD: "{{ mysql_root_password }}" |
||||
|
||||
|
||||
- name: create mysql dir |
||||
file: |
||||
path: /opt/mariadb |
||||
state: directory |
@ -0,0 +1,37 @@ |
||||
- name: fail if exporter host is missing |
||||
fail: |
||||
msg: mysql_target_host is not string |
||||
when: mysql_target_host is not string |
||||
|
||||
|
||||
- block: |
||||
- include_tasks: init_mysql.yml |
||||
|
||||
|
||||
- name: create exporter user |
||||
mysql_user: |
||||
name: "{{ mysql_exporter_username }}" |
||||
password: "{{ mysql_exporter_password }}" |
||||
plugin: mysql_native_password |
||||
host: "{{ container_address | d(ansible_host) }}" |
||||
resource_limits: |
||||
MAX_USER_CONNECTIONS: 3 |
||||
priv: |
||||
'*.*': 'SELECT,BINLOG MONITOR,PROCESS' |
||||
'performance_schema.*': SELECT |
||||
|
||||
login_host: "{{ hostvars[mysql_target_host]['container_address'] | d(hostvars[mysql_target_host]['ansible_host']) }}" |
||||
login_user: "{{ hostvars[mysql_target_host]['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars[mysql_target_host]['mysql_root_password'] | d(omit) }}" |
||||
register: result |
||||
|
||||
|
||||
- name: flush privileges |
||||
mysql_query: |
||||
query: FLUSH PRIVILEGES |
||||
login_host: "{{ hostvars[mysql_target_host]['container_address'] | d(hostvars[mysql_target_host]['ansible_host']) }}" |
||||
login_user: "{{ hostvars[mysql_target_host]['mysql_root_username'] | d('root') }}" |
||||
login_password: "{{ hostvars[mysql_target_host]['mysql_root_password'] | d(omit) }}" |
||||
when: result.changed |
||||
|
||||
delegate_to: localhost |
@ -0,0 +1,7 @@ |
||||
- name: set container cfg |
||||
set_fact: |
||||
container_role_config: |
||||
env: |
||||
DATA_SOURCE_NAME: "{{ mysql_exporter_username ~ ':' ~ mysql_exporter_password ~ '@tcp(' ~ |
||||
(hostvars[mysql_target_host]['container_address'] | d(hostvars[mysql_target_host]['ansible_host'])) ~ |
||||
':3306)/' }}" |
@ -1,4 +1,4 @@ |
||||
- name: set container cfg |
||||
set_fact: |
||||
container_role_config: |
||||
command: "-nginx.scrape-uri=http://{{ hostvars['nginx']['ansible_host'] }}/stub_status" |
||||
command: "-nginx.scrape-uri=http://{{ hostvars['nginx']['container_address'] | d(hostvars['nginx']['ansible_host']) }}/stub_status" |
||||
|
@ -0,0 +1,44 @@ |
||||
- name: determine distribution |
||||
setup: |
||||
gather_subset: |
||||
- distribution |
||||
|
||||
|
||||
- block: |
||||
- name: ensure apt can access https repos |
||||
apt: |
||||
name: |
||||
- ca-certificates |
||||
- curl |
||||
- gnupg |
||||
- lsb-release |
||||
force_apt_get: yes |
||||
state: latest |
||||
|
||||
|
||||
- name: add keyrings dir |
||||
file: |
||||
state: directory |
||||
path: "{{ gpg_keyrings_dir }}" |
||||
|
||||
|
||||
- name: download docker gpg key |
||||
get_url: |
||||
url: "{{ docker_repo_url ~ '/gpg' }}" |
||||
dest: "{{ gpg_keyrings_dir }}/docker.asc" |
||||
mode: a+r |
||||
|
||||
|
||||
- name: add apt repo |
||||
apt_repository: |
||||
repo: "deb [arch=amd64 signed-by={{ (gpg_keyrings_dir ~ '/docker.asc') | quote }}] \ |
||||
{{ docker_repo_url }} {{ ansible_distribution_release }} stable" |
||||
|
||||
|
||||
- name: update repository index |
||||
apt: |
||||
force_apt_get: yes |
||||
update_cache: yes |
||||
changed_when: no |
||||
|
||||
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
@ -0,0 +1,13 @@ |
||||
- name: initialize mysql connection on ansible controller |
||||
block: |
||||
- name: get ansible controller os distribution |
||||
setup: |
||||
gather_subset: |
||||
- distribution |
||||
|
||||
|
||||
- name: install pymysql |
||||
package: |
||||
name: "{{ { 'alpine': 'py3-pymysql', 'debian': 'python3-pymysql' }[ansible_distribution | lower] | d('pymysql') }}" |
||||
|
||||
delegate_to: localhost |
@ -0,0 +1,14 @@ |
||||
FROM fluent/fluentd:edge |
||||
|
||||
USER root |
||||
|
||||
RUN apk add --no-cache --update --virtual .build-deps build-base ruby-dev && \ |
||||
apk add mariadb-dev mariadb-connector-c mariadb-connector-c-dev && \ |
||||
gem install mysql2 && \ |
||||
gem install fluent-plugin-record-modifier && \ |
||||
gem install fluent-plugin-sql && \ |
||||
gem sources --clear-all && \ |
||||
apk del .build-deps && \ |
||||
rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem |
||||
|
||||
USER fluent |
Loading…
Reference in new issue