From 527ce7850edfdd7f042205bef1365569d2c8bdde Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Sat, 18 Jul 2020 15:40:26 +0200 Subject: [PATCH] Import the old role. --- README.md | 31 ++++++-------- defaults/main.yml | 11 ++++- handlers/main.yml | 6 ++- meta/main.yml | 71 +++++++++++---------------------- tasks/main.yml | 60 +++++++++++++++++++++++++++- templates/prometheus.default.j2 | 9 +++++ templates/prometheus.service.j2 | 18 +++++++++ templates/prometheus.yml.j2 | 28 +++++++++++++ vars/main.yml | 22 +++++++++- 9 files changed, 187 insertions(+), 69 deletions(-) create mode 100644 templates/prometheus.default.j2 create mode 100644 templates/prometheus.service.j2 create mode 100644 templates/prometheus.yml.j2 diff --git a/README.md b/README.md index 3637db8..98eac5a 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,26 @@ Role Name ========= -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +A role that installs the Prometheus open-source systems monitoring and alerting toolkit, Role Variables -------------- -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +The most important variables are listed below: + +``` yaml +prometheus_install: True +prometheus_version: 2.19.2 +prometheus_loglevel: info +prometheus_http_port: 9090 +prometheus_opts: '--storage.tsdb.retention=360d' +``` Dependencies ------------ -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -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 - roles: - - { role: username.rolename, x: 42 } +* nginx +* letsencrypt-acme-sh-client License ------- @@ -35,4 +30,4 @@ EUPL-1.2 Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Andrea Dell'Amico, diff --git a/defaults/main.yml b/defaults/main.yml index 95d3c70..3b462a1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,11 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +prometheus_install: True +prometheus_version: 2.19.2 +prometheus_user: prometheus +prometheus_home: /opt/prometheus +prometheus_dist_dir: '{{ prometheus_home }}/dist' +prometheus_confdir: '/opt/prometheus/conf' +prometheus_cmd: '{{ prometheus_dist_dir }}/{{ prometheus_dir }}/prometheus' +prometheus_loglevel: info +prometheus_http_port: 9090 +prometheus_opts: '--storage.tsdb.retention=360d' diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..caaedfb 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,6 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: Restart prometheus + service: name=prometheus state=restarted + +- name: Reload prometheus + service: name=prometheus state=reloaded diff --git a/meta/main.yml b/meta/main.yml index 1126a5e..bccce2c 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,61 +1,38 @@ galaxy_info: - author: your name - description: your description + author: Andrea Dell'Amico + description: Systems Architect company: ISTI-CNR - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning - # Some suggested licenses: - # - BSD (default) - # - MIT - # - GPLv2 - # - GPLv3 - # - Apache - # - CC-BY - license: EUPL-1.2 + license: EUPL 1.2+ min_ansible_version: 2.8 - # 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 + platforms: + - name: Ubuntu + versions: + - bionic + - name: EL + versions: + - 7 + - 8 - galaxy_tags: [] - # 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. + galaxy_tags: + - monitoring + - alerting + - metrics -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. +dependencies: + - src: git+https://gitea-s2i2s.isti.cnr.it/ISTI-ansible-roles/ansible-role-nginx.git + version: master + name: nginx + state: latest + - src: git+https://gitea-s2i2s.isti.cnr.it/ISTI-ansible-roles/ansible-role-letsencrypt-acme-sh-client.git + version: master + name: letsencrypt-acme-sh-client + state: latest diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..55be596 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,60 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- block: + - name: Create the user under prometheus will run + user: name={{ prometheus_user }} home={{ prometheus_home }} createhome=no shell=/usr/sbin/nologin system=yes + + - name: Create the prometheus server base and conf directories + file: dest={{ item }} state=directory owner=root group=root + with_items: + - '{{ prometheus_home }}' + - '{{ prometheus_confdir }}' + - '{{ prometheus_dist_dir }}' + + - name: Create the prometheus directory structure + file: dest={{ prometheus_home }}/{{ item }} state=directory owner={{ prometheus_user }} group={{ prometheus_user }} + with_items: + - data + - logs + + - name: Download prometheus + get_url: url={{ prometheus_download_url }} dest=/srv/ + + - name: Unarchive the prometheus distribution + unarchive: src=/srv/{{ prometheus_file }} dest={{ prometheus_dist_dir }} remote_src=yes + args: + creates: '{{ prometheus_dist_dir }}/{{ prometheus_dir }}/prometheus' + notify: Restart prometheus + + - name: Install the prometheus configuration + template: src=prometheus.yml.j2 dest={{ prometheus_confdir }}/prometheus.yml force=no + notify: Reload prometheus + + - name: Install the prometheus defaults + template: src=prometheus.default.j2 dest=/etc/default/prometheus mode=0644 owner=root group=root + + - name: Install the prometheus server systemd unit + template: src=prometheus.service.j2 dest=/etc/systemd/system/prometheus.service mode=0644 owner=root group=root + register: systemd_reload_required + + - name: Reload the systemd data + systemd: daemon_reload=yes + when: systemd_reload_required is changed + + - name: Ensure that prometheus is started and enabled + service: name=prometheus state=started enabled=yes + + tags: prometheus + when: prometheus_install + +- block: + - name: Ensure that prometheus is stopped and disabled + service: name=prometheus state=stopped enabled=no + + - name: Remove the prometheus init script + file: dest=/etc/init/prometheus.conf state=absent + + - name: Remove all the prometheus files + file: dest={{ prometheus_home }} state=absent + + tags: prometheus + when: not prometheus_install diff --git a/templates/prometheus.default.j2 b/templates/prometheus.default.j2 new file mode 100644 index 0000000..f213f81 --- /dev/null +++ b/templates/prometheus.default.j2 @@ -0,0 +1,9 @@ +GOMAXPROCS={{ ansible_processor_vcpus }} +PROMETHEUS_CMD={{ prometheus_cmd }} +PROMETHEUS_LOGDIR={{ prometheus_home }}/logs +PROMETHEUS_DATADIR={{ prometheus_home }}/data +PROMETHEUS_LOGLEVEL={{ prometheus_loglevel }} +PROMETHEUS_CONF={{ prometheus_confdir }}/prometheus.yml +PROMETHEUS_OPTS="{{ prometheus_opts }}" +PROMETHEUS_STARTUP_OPTS="--config.file={{ prometheus_confdir }}/prometheus.yml --storage.tsdb.path={{ prometheus_home }}/data {{ prometheus_opts }} --log.level={{ prometheus_loglevel }}" + diff --git a/templates/prometheus.service.j2 b/templates/prometheus.service.j2 new file mode 100644 index 0000000..eea2fc0 --- /dev/null +++ b/templates/prometheus.service.j2 @@ -0,0 +1,18 @@ +[Unit] +Description=Prometheus - Prometheus metrics collector. +Documentation=https://prometheus.io/docs/introduction/overview/ +After=network.target + +[Service] +Type=simple +User={{ prometheus_user }} +Group={{ prometheus_user }} +EnvironmentFile=/etc/default/prometheus +ExecStart={{ prometheus_cmd }} $PROMETHEUS_STARTUP_OPTS +ExecReload=/bin/kill -HUP $MAINPID +Restart=on-failure + +[Install] +WantedBy=multi-user.target +Alias=prometheus_server.service + diff --git a/templates/prometheus.yml.j2 b/templates/prometheus.yml.j2 new file mode 100644 index 0000000..9a12e23 --- /dev/null +++ b/templates/prometheus.yml.j2 @@ -0,0 +1,28 @@ +global: + scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. + # scrape_timeout is set to the global default (10s). + +# Alertmanager configuration +alerting: + alertmanagers: + - static_configs: + - targets: + # - alertmanager:9093 + +# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. +rule_files: + # - "first_rules.yml" + # - "second_rules.yml" + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: 'prometheus' + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ['localhost:9090'] diff --git a/vars/main.yml b/vars/main.yml index 3808477..c314e37 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,22 @@ --- -# vars file for ansible-role-template \ No newline at end of file +prometheus_dir: 'prometheus-{{ prometheus_version }}.linux-amd64' +prometheus_file: '{{ prometheus_dir }}.tar.gz' +prometheus_download_url: 'https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/{{ prometheus_file }}' + +nginx_letsencrypt_managed: True +nginx_use_common_virthost: True +nginx_virthosts: + - virthost_name: '{{ ansible_fqdn }}' + listen: '{{ http_port }}' + server_name: '{{ ansible_fqdn }}' + server_aliases: '' + index: index.html + ssl_enabled: True + ssl_only: True + ssl_letsencrypt_certs: '{{ nginx_letsencrypt_managed }}' + root: '{{ nginx_webroot }}' + server_tokens: 'off' + proxy_standard_setup: True + locations: + - location: / + target: http://localhost:{{ prometheus_http_port }}