- 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
