diff --git a/docker/docker.yml b/docker/docker.yml new file mode 100644 index 0000000..df2cca4 --- /dev/null +++ b/docker/docker.yml @@ -0,0 +1,6 @@ +- name: Install docker + hosts: all + become: yes + roles: + - role: docker-common + tags: docker diff --git a/docker/roles/docker-common/tasks/main.yml b/docker/roles/docker-common/tasks/main.yml index cac4f8e..2ed514c 100644 --- a/docker/roles/docker-common/tasks/main.yml +++ b/docker/roles/docker-common/tasks/main.yml @@ -11,48 +11,42 @@ - podman-docker - containerd - runc - become: yes -- name: Update apt cache +- name: Update APT cache apt: update_cache: yes - become: yes -- name: Install required packages +- name: Install dependencies apt: name: - ca-certificates - curl + - gnupg state: present - become: yes -- name: Create directory for Docker keyring +- name: Create directory for GPG keys file: path: /etc/apt/keyrings state: directory mode: '0755' - become: yes - name: Download Docker GPG key get_url: url: https://download.docker.com/linux/debian/gpg dest: /etc/apt/keyrings/docker.asc mode: '0644' - become: yes - name: Add Docker repository - apt_repository: - repo: "deb [arch={{ ansible_architecture }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable" - state: present - filename: docker.list - become: yes + shell: | + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \ + https://download.docker.com/linux/debian $(lsb_release -cs) stable" \ + | tee /etc/apt/sources.list.d/docker.list > /dev/null -- name: Update apt cache after adding Docker repository +- name: Update APT cache after adding Docker repository apt: update_cache: yes - become: yes -- name: Install Docker packages +- name: Install Docker and required packages apt: name: - docker-ce @@ -61,11 +55,9 @@ - docker-buildx-plugin - docker-compose-plugin state: present - become: yes -- name: Ensure Docker service is running and enabled - service: +- name: Enable and start Docker service + systemd: name: docker - state: started enabled: yes - become: yes \ No newline at end of file + state: started diff --git a/monitoring/main.yml b/monitoring/main.yml new file mode 100644 index 0000000..9e1e7f1 --- /dev/null +++ b/monitoring/main.yml @@ -0,0 +1,13 @@ +- name: Installing prometheus + become: yes + hosts: all + roles: + - role: prometheus-common + tags: prometheus + +- name: Installing grafana + become: yes + hosts: all + roles: + - role: grafana-common + tags: grafana \ No newline at end of file diff --git a/monitoring/roles/grafana-common/README.md b/monitoring/roles/grafana-common/README.md new file mode 100644 index 0000000..16fcda8 --- /dev/null +++ b/monitoring/roles/grafana-common/README.md @@ -0,0 +1,24 @@ +# grafana + +## Описание роли + +Эта роль предназначена для установки и настройки Grafana на целевой машине. Она скачивает нужный deb-пакет, устанавливает все необходимые зависимости, выполняет установку Grafana, а также настраивает и запускает сервис Grafana. + +## Требования + +- Ansible версии 2.9 или выше +- Операционная система на базе Debian (Ubuntu, Debian и другие) + +## Переменные роли + +Следующие переменные могут быть настроены в файле `vars/main.yml`, `defaults/main.yml` или переданы непосредственно в playbook: + +- `grafana_version`: Версия Grafana для установки. По умолчанию `"11.4.0"`. +- `grafana_arch`: Архитектура системы для пакета Grafana. По умолчанию `"amd64"`. +- `destination_directory`: Каталог для загрузки пакета. По умолчанию `"/home/user"`. + +Эти переменные можно изменить в соответствии с требованиями вашей системы. + +## Зависимости + +Нет. diff --git a/monitoring/roles/grafana-common/defaults/main.yml b/monitoring/roles/grafana-common/defaults/main.yml new file mode 100644 index 0000000..4eab591 --- /dev/null +++ b/monitoring/roles/grafana-common/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for grafana-common diff --git a/monitoring/roles/grafana-common/handlers/main.yml b/monitoring/roles/grafana-common/handlers/main.yml new file mode 100644 index 0000000..e0191e1 --- /dev/null +++ b/monitoring/roles/grafana-common/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for grafana-common diff --git a/monitoring/roles/grafana-common/meta/main.yml b/monitoring/roles/grafana-common/meta/main.yml new file mode 100644 index 0000000..ea68190 --- /dev/null +++ b/monitoring/roles/grafana-common/meta/main.yml @@ -0,0 +1,34 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + 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. + +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/monitoring/roles/grafana-common/tasks/main.yml b/monitoring/roles/grafana-common/tasks/main.yml new file mode 100644 index 0000000..6ca0849 --- /dev/null +++ b/monitoring/roles/grafana-common/tasks/main.yml @@ -0,0 +1,34 @@ +--- +- name: Download Grafana deb package + ansible.builtin.get_url: + url: "https://dl.grafana.com/oss/release/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + dest: "{{ destination_directory }}" +- name: Update repos and upgrade system + apt: + update_cache: yes + upgrade: dist + autoremove: yes + +- name: Install dependencies + apt: + name: + - adduser + - libfontconfig1 + - musl + state: present + +- name: Install Grafana from deb package + ansible.builtin.apt: + deb: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + state: present + +- name: Remove downloaded deb package + ansible.builtin.file: + path: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + state: absent + +- name: Enable and start Grafana service + ansible.builtin.systemd: + name: grafana-server + enabled: yes + state: started diff --git a/monitoring/roles/grafana-common/tests/inventory b/monitoring/roles/grafana-common/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/monitoring/roles/grafana-common/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/monitoring/roles/grafana-common/tests/test.yml b/monitoring/roles/grafana-common/tests/test.yml new file mode 100644 index 0000000..0bb8afa --- /dev/null +++ b/monitoring/roles/grafana-common/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - grafana-common diff --git a/monitoring/roles/grafana-common/vars/main.yml b/monitoring/roles/grafana-common/vars/main.yml new file mode 100644 index 0000000..2ebc9c4 --- /dev/null +++ b/monitoring/roles/grafana-common/vars/main.yml @@ -0,0 +1,4 @@ +--- +grafana_version: 11.4.0 +grafana_arch: amd64 +destination_directory: /home/user diff --git a/monitoring/roles/prometheus-common/README.md b/monitoring/roles/prometheus-common/README.md new file mode 100644 index 0000000..3efa1b1 --- /dev/null +++ b/monitoring/roles/prometheus-common/README.md @@ -0,0 +1,39 @@ +# prometheus-common + +## Описание роли + +Эта роль устанавливает и настраивает Prometheus на целевой машине, обеспечивая создание всех необходимых директорий и зависимостей, скачивание и установку Prometheus, а также развертывание файлов конфигурации и systemd-сервиса. Роль также гарантирует, что Prometheus будет запущен как фоновый сервис и настроен на автозапуск при старте системы. + +## Требования + +- Версия Ansible 2.9 или выше +- Операционная система на базе Debian (Ubuntu, Debian и другие) +- Указание версии Prometheus в файле `vars/main.yml` + +## Переменные роли + +Следующие переменные могут быть настроены в файлах `vars/main.yml` или `defaults/main.yml`, или переданы напрямую в роль: + +- `prometheus_user`: Пользователь, под которым будет работать Prometheus. По умолчанию `prometheus`. +- `prometheus_group`: Группа, под которой будет работать Prometheus. По умолчанию `prometheus`. +- `prometheus_install_dir`: Каталог, в который будет установлен Prometheus. По умолчанию `/usr/local/bin`. +- `prometheus_data_dir`: Каталог для хранения данных Prometheus. По умолчанию `/var/lib/prometheus`. +- `prometheus_config_dir`: Каталог для хранения конфигурации Prometheus. По умолчанию `/etc/prometheus`. +- `prometheus_version`: Версия Prometheus для установки. По умолчанию `2.35.0`. + +Эти значения можно изменить в соответствии с требованиями вашей системы или инфраструктуры. + +## Зависимости + +Нет. + +## Пример использования + +Вот пример playbook, который использует эту роль: + +```yaml +--- +- hosts: servers + become: yes + roles: + - prometheus-common diff --git a/monitoring/roles/prometheus-common/defaults/main.yml b/monitoring/roles/prometheus-common/defaults/main.yml new file mode 100644 index 0000000..8b6910b --- /dev/null +++ b/monitoring/roles/prometheus-common/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for prometheus-common diff --git a/monitoring/roles/prometheus-common/files/prometheus.service b/monitoring/roles/prometheus-common/files/prometheus.service new file mode 100644 index 0000000..64e6864 --- /dev/null +++ b/monitoring/roles/prometheus-common/files/prometheus.service @@ -0,0 +1,17 @@ +[Unit] +Description=Background service of Prometheus +Wants=network-online.target +After=network-online.target + +[Service] +User=prometheus +Group=prometheus +Type=simple +ExecStart=/usr/local/bin/prometheus \ + --config.file /etc/prometheus/prometheus.yml \ + --storage.tsdb.path /var/lib/prometheus/ \ + --web.console.templates=/etc/prometheus/consoles \ + --web.console.libraries=/etc/prometheus/console_libraries + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/monitoring/roles/prometheus-common/files/prometheus.yml b/monitoring/roles/prometheus-common/files/prometheus.yml new file mode 100644 index 0000000..4c5a9bd --- /dev/null +++ b/monitoring/roles/prometheus-common/files/prometheus.yml @@ -0,0 +1,7 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s +scrape_configs: + - job_name: "prometheus" + static_configs: + - targets: ["192.168.2.34:9100"] diff --git a/monitoring/roles/prometheus-common/handlers/main.yml b/monitoring/roles/prometheus-common/handlers/main.yml new file mode 100644 index 0000000..5925f9d --- /dev/null +++ b/monitoring/roles/prometheus-common/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: restart prometheus + systemd: + name: prometheus + state: restarted diff --git a/monitoring/roles/prometheus-common/meta/main.yml b/monitoring/roles/prometheus-common/meta/main.yml new file mode 100644 index 0000000..ea68190 --- /dev/null +++ b/monitoring/roles/prometheus-common/meta/main.yml @@ -0,0 +1,34 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + 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. + +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/monitoring/roles/prometheus-common/tasks/main.yml b/monitoring/roles/prometheus-common/tasks/main.yml new file mode 100644 index 0000000..84a7f3f --- /dev/null +++ b/monitoring/roles/prometheus-common/tasks/main.yml @@ -0,0 +1,86 @@ +--- +- name: Install dependencies + apt: + name: + - wget + - tar + - curl + state: present + update_cache: yes + when: ansible_os_family == "Debian" + +- name: Create prometheus user and group + user: + name: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + system: yes + create_home: no + +- name: Create required directories + file: + path: "{{ item }}" + state: directory + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + mode: '0755' + with_items: + - "{{ prometheus_install_dir }}" + - "{{ prometheus_data_dir }}" + - "{{ prometheus_config_dir }}" + +- name: Download Prometheus + get_url: + url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz" + dest: "/tmp/prometheus-{{ prometheus_version }}.tar.gz" + +- name: Extract Prometheus + unarchive: + src: "/tmp/prometheus-{{ prometheus_version }}.tar.gz" + dest: "{{ prometheus_install_dir }}" + remote_src: yes + +- name: Move binaries to /usr/local/bin + command: + cmd: mv "{{ prometheus_install_dir }}/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}" /usr/local/bin/ + with_items: + - "prometheus" + - "promtool" + +- name: Set ownership of Prometheus files + file: + path: "{{ item }}" + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + recurse: yes + with_items: + - "{{ prometheus_install_dir }}" + - "{{ prometheus_data_dir }}" + - "{{ prometheus_config_dir }}" + +- name: Copy prometheus.yml configuration + copy: + src: prometheus.yml + dest: "{{ prometheus_config_dir }}/prometheus.yml" + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + mode: '0644' + notify: + - restart prometheus + +- name: Copy systemd service file + copy: + src: prometheus.service + dest: /etc/systemd/system/prometheus.service + owner: root + group: root + mode: '0644' + +- name: Reload systemd + systemd: + daemon_reload: yes + +- name: Enable Prometheus service + systemd: + name: prometheus + enabled: yes + state: started diff --git a/monitoring/roles/prometheus-common/tests/inventory b/monitoring/roles/prometheus-common/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/monitoring/roles/prometheus-common/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/monitoring/roles/prometheus-common/tests/test.yml b/monitoring/roles/prometheus-common/tests/test.yml new file mode 100644 index 0000000..583eacd --- /dev/null +++ b/monitoring/roles/prometheus-common/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - prometheus-common diff --git a/monitoring/roles/prometheus-common/vars/main.yml b/monitoring/roles/prometheus-common/vars/main.yml new file mode 100644 index 0000000..7abcb3b --- /dev/null +++ b/monitoring/roles/prometheus-common/vars/main.yml @@ -0,0 +1,9 @@ +--- +prometheus_version: "3.1.0" +prometheus_user: "prometheus" +prometheus_group: "prometheus" +prometheus_install_dir: "/opt/prometheus" +prometheus_data_dir: "/var/lib/prometheus" +prometheus_config_dir: "/etc/prometheus" +prometheus_bin: "/usr/local/bin/prometheus" +prometheus_web_listen_address: ":9090" diff --git a/prometheus/main.yml b/prometheus/main.yml new file mode 100644 index 0000000..9e1e7f1 --- /dev/null +++ b/prometheus/main.yml @@ -0,0 +1,13 @@ +- name: Installing prometheus + become: yes + hosts: all + roles: + - role: prometheus-common + tags: prometheus + +- name: Installing grafana + become: yes + hosts: all + roles: + - role: grafana-common + tags: grafana \ No newline at end of file diff --git a/prometheus/roles/grafana-common/README.md b/prometheus/roles/grafana-common/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/prometheus/roles/grafana-common/README.md @@ -0,0 +1,38 @@ +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. + +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. + +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 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/prometheus/roles/grafana-common/defaults/main.yml b/prometheus/roles/grafana-common/defaults/main.yml new file mode 100644 index 0000000..4eab591 --- /dev/null +++ b/prometheus/roles/grafana-common/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for grafana-common diff --git a/prometheus/roles/grafana-common/handlers/main.yml b/prometheus/roles/grafana-common/handlers/main.yml new file mode 100644 index 0000000..e0191e1 --- /dev/null +++ b/prometheus/roles/grafana-common/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for grafana-common diff --git a/prometheus/roles/grafana-common/meta/main.yml b/prometheus/roles/grafana-common/meta/main.yml new file mode 100644 index 0000000..ea68190 --- /dev/null +++ b/prometheus/roles/grafana-common/meta/main.yml @@ -0,0 +1,34 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + 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. + +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/prometheus/roles/grafana-common/tasks/main.yml b/prometheus/roles/grafana-common/tasks/main.yml new file mode 100644 index 0000000..6ca0849 --- /dev/null +++ b/prometheus/roles/grafana-common/tasks/main.yml @@ -0,0 +1,34 @@ +--- +- name: Download Grafana deb package + ansible.builtin.get_url: + url: "https://dl.grafana.com/oss/release/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + dest: "{{ destination_directory }}" +- name: Update repos and upgrade system + apt: + update_cache: yes + upgrade: dist + autoremove: yes + +- name: Install dependencies + apt: + name: + - adduser + - libfontconfig1 + - musl + state: present + +- name: Install Grafana from deb package + ansible.builtin.apt: + deb: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + state: present + +- name: Remove downloaded deb package + ansible.builtin.file: + path: "{{ destination_directory }}/grafana_{{ grafana_version }}_{{ grafana_arch }}.deb" + state: absent + +- name: Enable and start Grafana service + ansible.builtin.systemd: + name: grafana-server + enabled: yes + state: started diff --git a/prometheus/roles/grafana-common/tests/inventory b/prometheus/roles/grafana-common/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/prometheus/roles/grafana-common/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/prometheus/roles/grafana-common/tests/test.yml b/prometheus/roles/grafana-common/tests/test.yml new file mode 100644 index 0000000..0bb8afa --- /dev/null +++ b/prometheus/roles/grafana-common/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - grafana-common diff --git a/prometheus/roles/grafana-common/vars/main.yml b/prometheus/roles/grafana-common/vars/main.yml new file mode 100644 index 0000000..2ebc9c4 --- /dev/null +++ b/prometheus/roles/grafana-common/vars/main.yml @@ -0,0 +1,4 @@ +--- +grafana_version: 11.4.0 +grafana_arch: amd64 +destination_directory: /home/user diff --git a/prometheus/roles/prometheus-common/README.md b/prometheus/roles/prometheus-common/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/prometheus/roles/prometheus-common/README.md @@ -0,0 +1,38 @@ +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. + +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. + +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 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/prometheus/roles/prometheus-common/defaults/main.yml b/prometheus/roles/prometheus-common/defaults/main.yml new file mode 100644 index 0000000..8b6910b --- /dev/null +++ b/prometheus/roles/prometheus-common/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for prometheus-common diff --git a/prometheus/roles/prometheus-common/files/prometheus.service b/prometheus/roles/prometheus-common/files/prometheus.service new file mode 100644 index 0000000..64e6864 --- /dev/null +++ b/prometheus/roles/prometheus-common/files/prometheus.service @@ -0,0 +1,17 @@ +[Unit] +Description=Background service of Prometheus +Wants=network-online.target +After=network-online.target + +[Service] +User=prometheus +Group=prometheus +Type=simple +ExecStart=/usr/local/bin/prometheus \ + --config.file /etc/prometheus/prometheus.yml \ + --storage.tsdb.path /var/lib/prometheus/ \ + --web.console.templates=/etc/prometheus/consoles \ + --web.console.libraries=/etc/prometheus/console_libraries + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/prometheus/roles/prometheus-common/files/prometheus.yml b/prometheus/roles/prometheus-common/files/prometheus.yml new file mode 100644 index 0000000..4c5a9bd --- /dev/null +++ b/prometheus/roles/prometheus-common/files/prometheus.yml @@ -0,0 +1,7 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s +scrape_configs: + - job_name: "prometheus" + static_configs: + - targets: ["192.168.2.34:9100"] diff --git a/prometheus/roles/prometheus-common/handlers/main.yml b/prometheus/roles/prometheus-common/handlers/main.yml new file mode 100644 index 0000000..5925f9d --- /dev/null +++ b/prometheus/roles/prometheus-common/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: restart prometheus + systemd: + name: prometheus + state: restarted diff --git a/prometheus/roles/prometheus-common/meta/main.yml b/prometheus/roles/prometheus-common/meta/main.yml new file mode 100644 index 0000000..ea68190 --- /dev/null +++ b/prometheus/roles/prometheus-common/meta/main.yml @@ -0,0 +1,34 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + 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. + +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/prometheus/roles/prometheus-common/tasks/main.yml b/prometheus/roles/prometheus-common/tasks/main.yml new file mode 100644 index 0000000..84a7f3f --- /dev/null +++ b/prometheus/roles/prometheus-common/tasks/main.yml @@ -0,0 +1,86 @@ +--- +- name: Install dependencies + apt: + name: + - wget + - tar + - curl + state: present + update_cache: yes + when: ansible_os_family == "Debian" + +- name: Create prometheus user and group + user: + name: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + system: yes + create_home: no + +- name: Create required directories + file: + path: "{{ item }}" + state: directory + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + mode: '0755' + with_items: + - "{{ prometheus_install_dir }}" + - "{{ prometheus_data_dir }}" + - "{{ prometheus_config_dir }}" + +- name: Download Prometheus + get_url: + url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz" + dest: "/tmp/prometheus-{{ prometheus_version }}.tar.gz" + +- name: Extract Prometheus + unarchive: + src: "/tmp/prometheus-{{ prometheus_version }}.tar.gz" + dest: "{{ prometheus_install_dir }}" + remote_src: yes + +- name: Move binaries to /usr/local/bin + command: + cmd: mv "{{ prometheus_install_dir }}/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}" /usr/local/bin/ + with_items: + - "prometheus" + - "promtool" + +- name: Set ownership of Prometheus files + file: + path: "{{ item }}" + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + recurse: yes + with_items: + - "{{ prometheus_install_dir }}" + - "{{ prometheus_data_dir }}" + - "{{ prometheus_config_dir }}" + +- name: Copy prometheus.yml configuration + copy: + src: prometheus.yml + dest: "{{ prometheus_config_dir }}/prometheus.yml" + owner: "{{ prometheus_user }}" + group: "{{ prometheus_group }}" + mode: '0644' + notify: + - restart prometheus + +- name: Copy systemd service file + copy: + src: prometheus.service + dest: /etc/systemd/system/prometheus.service + owner: root + group: root + mode: '0644' + +- name: Reload systemd + systemd: + daemon_reload: yes + +- name: Enable Prometheus service + systemd: + name: prometheus + enabled: yes + state: started diff --git a/prometheus/roles/prometheus-common/tests/inventory b/prometheus/roles/prometheus-common/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/prometheus/roles/prometheus-common/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/prometheus/roles/prometheus-common/tests/test.yml b/prometheus/roles/prometheus-common/tests/test.yml new file mode 100644 index 0000000..583eacd --- /dev/null +++ b/prometheus/roles/prometheus-common/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - prometheus-common diff --git a/prometheus/roles/prometheus-common/vars/main.yml b/prometheus/roles/prometheus-common/vars/main.yml new file mode 100644 index 0000000..7abcb3b --- /dev/null +++ b/prometheus/roles/prometheus-common/vars/main.yml @@ -0,0 +1,9 @@ +--- +prometheus_version: "3.1.0" +prometheus_user: "prometheus" +prometheus_group: "prometheus" +prometheus_install_dir: "/opt/prometheus" +prometheus_data_dir: "/var/lib/prometheus" +prometheus_config_dir: "/etc/prometheus" +prometheus_bin: "/usr/local/bin/prometheus" +prometheus_web_listen_address: ":9090"