Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions group_vars/all/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---

ansible_managed: "Ansible managed file, do not edit directly"

git_repo: https://github.com/okfde/fragdenstaat_de.git

base_ssh_port: "2228"
Expand Down
2 changes: 1 addition & 1 deletion group_vars/guest/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---

network_vlan_id: 0
network_host_type: "guest"
2 changes: 2 additions & 0 deletions group_vars/hypervisor/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

network_bridge_vlan: "br1"
network_bridge_physical: "br2"

virt_guest_default_password: "{{ secret_virt_guest_default_password }}"
9 changes: 9 additions & 0 deletions group_vars/hypervisor/secrets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$ANSIBLE_VAULT;1.1;AES256
33373064316433636366386230636635346362656364623064333536336330323730343237656232
3665383538393861653436333533643263393432663934310a663533343337626561636661636436
34393635396233323566393161313330643639643163646339343033646261323533646633373563
3933396262633236340a326230656236306261643066643833646362353436303564656533396533
33363339616637633531386661326338626562323639613633316561626335613231623766306561
33626331616536376334333066656335623335303136306132373861646633386263393635333862
61306131343333326632306636383130613439343535363663613365373232646264653362313839
37306365613037663032
3 changes: 3 additions & 0 deletions group_vars/roy/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

ansible_ssh_common_args: "-o ProxyJump=roy.okfn.de"
15 changes: 15 additions & 0 deletions host_vars/edge1.okfn.de/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

# Keep host_data.site_ipv4 for compatibility
host_data:
site_ipv4: "{{ network_networks.private.address4 | split('/') | first }}"

network_networks:
private:
address4: 192.168.100.103/24
public:
mac: 00:50:56:00:FD:9C
address4: 5.9.108.212/27
gateway4: 5.9.108.193
address6: "2a01:4f8:162:44c7::3/64"
gateway6: 'fe80::1'
6 changes: 6 additions & 0 deletions host_vars/roy.okfn.de/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ host_data:
sidecar: false
prometheus_exporter:
- node_exporter

virt_guests:
- name: edge1.okfn.de
cpus: 2
memory: 4096
disk_size: 15
6 changes: 6 additions & 0 deletions inventory
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ schoch.okfn.de

[hypervisor]
roy.okfn.de

[roy]
edge1.okfn.de

[guest:children]
roy
9 changes: 9 additions & 0 deletions playbooks/virtualization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---

- name: Virtualization
hosts: hypervisor
roles:
- role: virt
tags: virt
- role: prometheus_libvirt_exporter
tags: prometheus_libvirt_exporter
17 changes: 17 additions & 0 deletions roles/monitor/tasks/_rewrite_prom_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

# Task is split out of ./monitor.yml to allow for easier reconciliation of scrape targets when adding or removing exporters.
# Include in other roles like this:
# ```
# - name: Reconcile prometheus scrape targets
# ansible.builtin.include_role:
# name: monitor
# tasks_from: _rewrite_prom_config
# delegate_to: fds-mon.okfn.de
# ```
- name: Rewrite prometheus.yml
ansible.builtin.template:
src: prometheus.yml.j2
dest: "{{ monitor_dir }}/prometheus/etc/prometheus.yml"
mode: "0644"
notify: Restart prometheus
4 changes: 4 additions & 0 deletions roles/monitor/templates/prometheus.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ scrape_configs:
- job_name: "celery_exporter"
static_configs:
- targets: [{% for host in groups['all'] %}{% if hostvars[host].prometheus_exporter is defined and 'celery_exporter' in hostvars[host].prometheus_exporter %}"{{ hostvars[host].host_data.site_ipv4 }}:9808"{% endif %}{% if not loop.last %}{% if hostvars[loop.nextitem].prometheus_exporter is defined and 'celery_exporter' in hostvars[loop.nextitem].prometheus_exporter %},{% endif %}{% endif %}{% endfor %}]

- job_name: "libvirt_exporter"
static_configs:
- targets: [{% for host in groups['hypervisor'] %}"{{ hostvars[host].host_data.site_ipv4 }}:9177"{% if not loop.last %},{% endif %}{% endfor %}]
3 changes: 3 additions & 0 deletions roles/network/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---

# Type of host. 'guest' will skip configuration of physical interface and vlan
network_host_type: physical

# Set VLAN ID to configure VLAN for hosts
# For hosts without physical network device (i.e. guest vms), set value to 0 to disable configuration
network_vlan_id: 4000
Expand Down
33 changes: 33 additions & 0 deletions roles/network/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---

argument_specs:
main:
options:
network_host_type:
type: "str"
required: true
choices:
- "physical"
- "guest"
description: "Wether host is a physical machine with phyiscal interfaces or a virtualized guest"

network_vlan_id:
type: "int"
required: true
description: "VLAN identifier, only applicable for physical hosts."

network_bridge_vlan:
type: "str"
required: true
description:
- "Name of the bridge interface to attach to virtual vlan interface, i.e. 'brX'."
- "Leave empty to skip bridge creation"
- "Only applicable to physical hosts"

network_bridge_physical:
type: "str"
required: true
description:
- "Name of the bridge interface to attach to virtual phyiscal interface, i.e. 'brX'."
- "Leave empty to skip bridge creation"
- "Only applicable to physical hosts"
1 change: 1 addition & 0 deletions roles/network/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ansible.builtin.import_tasks: networkd.yml
tags:
- network_networkd
when: network_host_type == 'physical'

- name: Import resolved tasks
ansible.builtin.import_tasks: resolved.yml
Expand Down
1 change: 0 additions & 1 deletion roles/network/tasks/networkd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
notify: restart systemd-networkd

- name: Ensure netdev configuration for vlan exist
when: network_vlan_id != 0
notify: restart systemd-networkd
block:
- name: Ensure netdev configuration for vlan exist
Expand Down
2 changes: 0 additions & 2 deletions roles/network/templates/ethernet.network.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
Name={{ network_physical_interface }}

[Network]
{% if network_vlan_id != 0 %}
VLAN={{ network_physical_interface }}.{{ network_vlan_id }}
{% endif %}
{# If we configure a bridge for this interface, the IP address and gateway are defined in 'ethernet_bridge_network.j2' #}
{% if network_bridge_physical is defined and network_bridge_physical | length > 0 %}
Bridge={{ network_bridge_physical }}
Expand Down
3 changes: 3 additions & 0 deletions roles/prometheus_libvirt_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# prometheus-libvirt-exporter

This role installs the promtheus-libvirt-exporter binary from https://github.com/inovex/prometheus-libvirt-exporter and creates a systemd unit file. We do this manually since the ubuntu package of the same name is outdated and unmaintained.
4 changes: 4 additions & 0 deletions roles/prometheus_libvirt_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

# TODO: Track dependency with renovate
prometheus_libvirt_exporter_version: "2.6.2"
10 changes: 10 additions & 0 deletions roles/prometheus_libvirt_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

- name: Restart prometheus-libvirt-exporter
ansible.builtin.systemd:
name: prometheus-libvirt-exporter
state: restarted

- name: Reload daemon
ansible.builtin.systemd:
daemon_reload: true
9 changes: 9 additions & 0 deletions roles/prometheus_libvirt_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---

argument_specs:
main:
options:
prometheus_libvirt_exporter_version:
type: "str"
required: true
description: "Version of prometheus-libvirt-exporter."
93 changes: 93 additions & 0 deletions roles/prometheus_libvirt_exporter/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---

- name: Install prometheus-libvirt-exporter systemd file
ansible.builtin.template:
src: prometheus.service.j2
dest: /etc/systemd/system/prometheus-libvirt-exporter.service
mode: "0644"
notify: Reload daemon

- name: Flush handlers
ansible.builtin.meta: flush_handlers

# TODO: Extract logic into generic role for verifying, fetching and extracting raw binaries from upstream repositories
- name: Check if prometheus-libvirt-exporter is installed
ansible.builtin.stat:
path: "/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64"
register: _exporter_installed

- name: Install prometheus-libvirt-exporter
when: >
_exporter_installed.stat.exists is false
notify: Restart prometheus-libvirt-exporter
block:
- name: Create tmpdir
ansible.builtin.tempfile:
state: directory
check_mode: false
register: _download_exporter

- name: Download prometheus-libvirt-exporter
ansible.builtin.get_url:
url: "https://github.com/inovex/prometheus-libvirt-exporter/releases/download/v{{ prometheus_libvirt_exporter_version }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64.tar.gz"
checksum: "sha256:https://github.com/inovex/prometheus-libvirt-exporter/releases/download/v{{ prometheus_libvirt_exporter_version }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}_checksums.txt"
dest: "{{ _download_exporter.path }}"
mode: "0644"
check_mode: false

- name: Unarchive prometheus-libvirt-exporter
ansible.builtin.unarchive:
remote_src: true
src: "{{ _download_exporter.path }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64.tar.gz"
dest: "{{ _download_exporter.path }}"
check_mode: false

- name: Install prometheus-libvirt-exporter binary
ansible.builtin.copy:
remote_src: true
src: "{{ _download_exporter.path }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64/prometheus-libvirt-exporter"
dest: "/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64"
mode: "0755"

always:
- name: Remove tmpdir
ansible.builtin.file:
path: "{{ _download_exporter }}"
state: absent

- name: Find outdated versions of prometheus-libvirt-exporter
ansible.builtin.find:
paths: "/usr/local/bin"
file_type: file
excludes:
- "prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}"
patterns: "prometheus-libvirt-exporter.*"
use_regex: true
register: _prometheus_outdated

- name: Remove outdated binaries
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ _prometheus_outdated.files | default([]) | map(attribute='path') | sort }}"

- name: Ensure prometheus-libvirt-exporter is running
ansible.builtin.systemd:
name: prometheus-libvirt-exporter
state: started
enabled: true

- name: Configure firewall for prometheus-libvirt-exporter
community.general.ufw:
port: "9177"
proto: tcp
from_ip: 192.168.100.5
rule: allow

- name: Reconcile prometheus scrape targets
ansible.builtin.include_role:
name: monitor
tasks_from: _rewrite_prom_config.yml
apply:
delegate_to: fds-mon.okfn.de
delegate_facts: true
29 changes: 29 additions & 0 deletions roles/prometheus_libvirt_exporter/templates/prometheus.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ ansible_managed | comment }}
[Unit]
Description=Prometheus-libvirt-exporter service (host and vm metrics exposed for prometheus, written in Go with pluggable metric collectors)
Requires=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64 --web.listen-address ":9177"
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
KillSignal=SIGINT

# Security
PrivateTmp=yes
ProtectClock=yes
ProtectControlGroups=yes
ProtectHome=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
SystemCallArchitectures=native

[Install]
WantedBy=multi-user.target
Loading
Loading