diff --git a/README.md b/README.md index 3637db8..dd3e39c 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,27 @@ 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 system node exporter, 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_n_e_install: True +prometheus_n_e_version: 1.0.1 +prometheus_n_e_port: 9100 +prometheus_n_e_loglevel: info +prometheus_n_e_opts: '--web.listen-address=":{{ prometheus_n_e_port }}" --log.level={{ prometheus_n_e_loglevel }}' +# List the additional options here +prometheus_n_e_additional_opts: '' +``` 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 } +None License ------- @@ -35,4 +31,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..607d173 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,12 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +prometheus_n_e_install: True +prometheus_n_e_version: 1.0.1 +prometheus_n_e_port: 9100 +prometheus_n_e_loglevel: info +prometheus_n_e_opts: '--web.listen-address=":{{ prometheus_n_e_port }}" --log.level={{ prometheus_n_e_loglevel }}' +# List the additional options here +prometheus_n_e_additional_opts: '' + +prometheus_n_e_firewalld_rules: 'enabled' +prometheus_n_e_firewalld_ports: + - { port: '{{ prometheus_n_e_port }}', protocol: 'tcp', state: '{{ prometheus_n_e_firewalld_rules }}', zone: '{{ firewalld_default_zone }}' } \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..48605cd 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,7 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: systemd reload + command: systemctl daemon-reload + +- name: Restart node exporter + service: name=node_exporter state=restarted + diff --git a/meta/main.yml b/meta/main.yml index 1126a5e..5565670 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,61 +1,29 @@ 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: + - trusty + - 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 + - metrics dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. - diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..c3806f9 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,71 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- block: + - name: Create the user under the node exporter will run + user: name={{ prometheus_n_e_user }} home={{ prometheus_n_e_home }} createhome=no shell=/usr/sbin/nologin system=yes + + - name: Create the prometheus node exporter base directory + file: dest={{ item }} state=directory owner=root group=root + with_items: + - '{{ prometheus_n_e_home }}' + - '{{ prometheus_n_e_dist_dir }}' + + - name: Create the prometheus node exporter log directory + file: dest={{ prometheus_n_e_logdir }} state=directory owner={{ prometheus_n_e_user }} group={{ prometheus_n_e_user }} + + - name: Download the prometheus node exporter + get_url: url={{ prometheus_n_e_download_url }} dest=/srv/ + + - name: Unarchive the prometheus distribution + unarchive: src=/srv/{{ prometheus_n_e_file }} dest={{ prometheus_n_e_dist_dir }} remote_src=yes owner=root group=root + args: + creates: '{{ prometheus_n_e_dist_dir }}/{{ prometheus_n_e_dir }}/node_exporter' + notify: Restart node exporter + + - name: Install the prometheus node exporter upstart script + template: src=node_exporter.upstart.j2 dest=/etc/init/node_exporter.conf mode=0644 owner=root group=root + when: ansible_service_mgr != 'systemd' + + - name: Install the prometheus node exporter systemd unit + template: src=node_exporter.systemd.j2 dest=/etc/systemd/system/node_exporter.service mode=0644 owner=root group=root + when: ansible_service_mgr == 'systemd' + register: systemd_reload_required + + - name: Reload the systemd data + systemd: daemon_reload=yes + when: systemd_reload_required is changed + + - name: Ensure that prometheus node_exporter is started and enabled + service: name=node_exporter state=started enabled=yes + + tags: [ 'prometheus', 'node_exporter' ] + when: prometheus_n_e_install + +- block: + - name: Ensure that prometheus node_exporter is stopped and disabled + service: name=node_exporter state=stopped enabled=no + ignore_errors: True + + - name: Remove prometheus node exporter upstart script + file: dest=/etc/init/node_exporter.conf state=absent + when: ansible_service_mgr != 'systemd' + + - name: Remove the prometheus node exporter systemd unit + file: dest=/etc/systemd/system/node_exporter.service state=absent + when: ansible_service_mgr == 'systemd' + register: systemd_reload_required + + - name: Reload the systemd data + systemd: daemon_reload=yes + when: systemd_reload_required is changed + + tags: [ 'prometheus', 'node_exporter' ] + when: not prometheus_n_e_install + +- name: Manage the prometheus node exporter firewalld rules + block: + - name: Manage the prometheus node exporter firewalld ports + firewalld: port={{ item.port }}/{{ item.protocol }} zone={{ item.zone }} permanent={{ item.permanent | default(True) }} state={{ item.state }} immediate=True + with_items: '{{ prometheus_n_e_firewalld_ports }}' + + when: firewalld_enabled is defined and firewalld_enabled | bool + tags: [ 'prometheus', 'node_exporter', 'firewall', 'firewalld', 'iptables', 'iptables_rules' ] diff --git a/templates/node_exporter.service.j2 b/templates/node_exporter.service.j2 new file mode 100644 index 0000000..bf0d103 --- /dev/null +++ b/templates/node_exporter.service.j2 @@ -0,0 +1,17 @@ +[Unit] +Description=node_exporter - Prometheus exporter for machine metrics. +After=network.target + +[Service] +Type=simple +Restart=on-failure + +User={{ prometheus_n_e_user }} +Group={{ prometheus_n_e_user }} + +ExecStart={{ prometheus_n_e_cmd }} {{ prometheus_n_e_opts }} {{ prometheus_n_e_additional_opts }} --collector.systemd + +[Install] +WantedBy=multi-user.target +Alias=prometheus_node_exporter.service + diff --git a/templates/node_exporter.upstart.j2 b/templates/node_exporter.upstart.j2 new file mode 100644 index 0000000..85315ad --- /dev/null +++ b/templates/node_exporter.upstart.j2 @@ -0,0 +1,12 @@ +description "Prometheus node exporter" +start on (local-filesystems and net-device-up IFACE!=lo) +stop on runlevel [016] + +respawn +respawn limit 10 5 +setuid {{ prometheus_n_e_user }} +setgid {{ prometheus_n_e_user }} + +script + exec {{ prometheus_n_e_cmd }} {{ prometheus_n_e_opts }} {{ prometheus_n_e_additional_opts }} > {{ prometheus_n_e_logdir }}/node_exporter.log 2>&1 +end script diff --git a/vars/main.yml b/vars/main.yml index 3808477..ebd479b 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,9 @@ --- -# vars file for ansible-role-template \ No newline at end of file +prometheus_n_e_dir: 'node_exporter-{{ prometheus_n_e_version }}.linux-amd64' +prometheus_n_e_file: '{{ prometheus_n_e_dir }}.tar.gz' +prometheus_n_e_download_url: 'https://github.com/prometheus/node_exporter/releases/download/v{{ prometheus_n_e_version }}/{{ prometheus_n_e_file }}' +prometheus_n_e_user: prometheus_n_e +prometheus_n_e_home: /opt/prometheus_node_exporter +prometheus_n_e_dist_dir: '{{ prometheus_n_e_home }}/dist' +prometheus_n_e_logdir: '/var/log/prometheus-node-exporter' +prometheus_n_e_cmd: '{{ prometheus_n_e_dist_dir }}/{{ prometheus_n_e_dir }}/node_exporter'