Skip to content

Commit

Permalink
Merge pull request #2 from blalop/features/1
Browse files Browse the repository at this point in the history
#1 Pi-Hole Exporter
  • Loading branch information
blalop authored Feb 27, 2021
2 parents d7203e3 + 380ffd0 commit ef471b3
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 33 deletions.
33 changes: 12 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@
[![Build Status](https://travis-ci.com/blalop/pihole_role.svg?branch=main)](https://travis-ci.com/blalop/pihole_role)
[![Ansible Galaxy](https://img.shields.io/badge/galaxy-blalop.pihole_role-B62682.svg)](https://galaxy.ansible.com/blalop/pihole_role)

This role installs pihole via installation script. Tested on Debian && Raspbian.

## Requirements

No special requirements.
This role installs [Pi-Hole](https://pi-hole.net/) via installation script and (optionally) [eko](https://github.com/eko)'s [Pi-hole Exporter](https://github.com/eko/pihole-exporter).

Made with [Idealista's cookiecutter template](https://github.com/idealista/cookiecutter-ansible-role).

## Role vars
## Requirements

| Variable | Default | Additional comments |
| :--- | :--- | :--- |
| `pihole_required_packages` | [procps] | |
| `pihole_password` | pihole | Pihole web interface password |
| `pihole_force_reinstall` | false | Forces reinstallation |
| `pihole_lighttpd_port` | 80 | Web interface port |
| `pihole_interface` | eth0 | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_ipv4_address` | 0.0.0.0 | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_ipv6_address` | 0:0:0:0:0:0 | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_dns_1` | 8.8.8.8 | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_dns_2` | 4.4.4.4 | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_query_logging` | true | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_web_server` | true | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_web_interface` | true | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
| `pihole_lighttpd_enabled` | true | [Pihole setup vars](https://discourse.pi-hole.net/t/what-is-setupvars-conf-and-how-do-i-use-it/3533) |
No special requirements. Use it in your playbook like this:

```
- name: Configure services
hosts: pi
tags: services
roles:
- role: pihole
tags: pihole
```

## Testing

Expand Down
42 changes: 40 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---

# Pi-hole
pihole_required_packages:
- procps

pihole_password: pihole

pihole_force_reinstall: false

pihole_password: pihole

## Lighttpd config
pihole_lighttpd_port: 80

Expand All @@ -20,3 +21,40 @@ pihole_query_logging: true
pihole_web_server: true
pihole_web_interface: true
pihole_lighttpd_enabled: true

# Pi-Hole Exporter
pihole_exporter_install: true
pihole_exporter_force_reinstall: false
pihole_exporter_version: v0.0.11
pihole_exporter_arch: linux-arm
pihole_exporter_url: "https://github.com/eko/pihole-exporter/releases/download/{{ pihole_exporter_version }}/pihole_exporter-{{ pihole_exporter_arch }}"

## Options
pihole_exporter_hostname: "127.0.0.1"
pihole_exporter_port: 9617
pihole_exporter_interval: 10s

## User & group
pihole_exporter_user: pihole_exporter
pihole_exporter_group: pihole_exporter

## Files
pihole_exporter_exec_name: pihole_exporter
pihole_exporter_bin_path: /opt/pihole_exporter
pihole_exporter_logs_path: /var/log/pihole_exporter
pihole_exporter_skeleton_paths:
- "{{ pihole_exporter_bin_path }}"
- "{{ pihole_exporter_logs_path }}"

## Service
pihole_exporter_state: started
pihole_exporter_enabled: yes
pihole_exporter_private_tmp: yes
pihole_exporter_documentation_link: https://github.com/eko/pihole-exporter
pihole_exporter_max_files: 32768
pihole_exporter_log_output: "{% if pihole_exporter_log_file is defined %} file:{{ pihole_exporter_log_file }} {% else %} journal{% endif %}"
pihole_exporter_options:
- "port {{ pihole_exporter_port }}"
- "pihole_hostname {{ pihole_exporter_hostname }}"
- "pihole_password {{ pihole_password }}"
- "pihole_port {{ pihole_lighttpd_port }}"
5 changes: 5 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
systemd:
name: lighttpd
state: restarted

- name: restart pihole_exporter
systemd:
name: pihole_exporter
state: restarted
4 changes: 4 additions & 0 deletions molecule/default/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

pihole_exporter_private_tmp: no
pihole_exporter_arch: linux-amd64
2 changes: 2 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ platforms:
- 67/udp
- 80/tcp
- 443/tcp
- 9617/tcp
published_ports:
- 0.0.0.0:8080:80/tcp
- 0.0.0.0:9617:9617/tcp
capabilities:
- SYS_ADMIN
- NET_ADMIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ service:
running: true

http:
"http://localhost:{{ pihole_lighttpd_port }}":
http://localhost:
status: 200

command:
Expand Down
24 changes: 24 additions & 0 deletions molecule/default/tests/test_pihole_exporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---

service:
pihole_exporter:
enabled: true
running: true

http:
http://localhost:9617/metrics:
status: 200
body:
- pihole_status{hostname="127.0.0.1"} 1

file:
/usr/bin/pihole_exporter:
exists: true
owner: root
group: root
filetype: symlink
/opt/pihole_exporter:
exists: true
owner: pihole_exporter
group: pihole_exporter
filetype: directory
19 changes: 10 additions & 9 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.

- name: Verify pihole
hosts:
Expand Down Expand Up @@ -30,12 +27,16 @@
until: download_goss is succeeded
retries: 3

- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "tests/test_*.yml"
- name: Copy Goss Pi-hole tests to remote
copy:
src: tests/test_pihole.yml
dest: "{{ goss_test_directory }}/test_pihole.yml"

- name: Copy Goss Pi-hole Exporter tests to remote
copy:
src: tests/test_pihole_exporter.yml
dest: "{{ goss_test_directory }}/test_pihole_exporter.yml"
when: pihole_exporter_install

- name: Register test files
shell: "ls {{ goss_test_directory }}/test_*.yml"
Expand Down
46 changes: 46 additions & 0 deletions tasks/exporter_install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---

- name: Pi-hole Exporter | Ensure pihole_exporter group
group:
name: "{{ pihole_exporter_group }}"
system: yes
state: present

- name: Pi-hole Exporter | Ensure pihole_exporter user
user:
name: "{{ pihole_exporter_user }}"
group: "{{ pihole_exporter_group }}"
system: yes
shell: /usr/sbin/nologin
createhome: no

- name: Pi-hole Exporter | Ensure skeleton paths
file:
dest: "{{ item }}"
owner: "{{ pihole_exporter_user }}"
group: "{{ pihole_exporter_group }}"
state: directory
with_items:
- "{{ pihole_exporter_skeleton_paths }}"

- name: Pi-hole Exporter | Check pihole_exporter version
command: pihole_exporter --help
register: pihole_exporter_check
changed_when: false
ignore_errors: true

- name: Pi-hole Exporter | Download package
get_url:
url: "{{ pihole_exporter_url }}"
dest: "{{ pihole_exporter_bin_path }}/{{ pihole_exporter_exec_name }}-{{ pihole_exporter_version }}"
owner: "{{ pihole_exporter_user }}"
group: "{{ pihole_exporter_group }}"
mode: "0755"
when: pihole_exporter_force_reinstall or pihole_exporter_check is failed

- name: Pi-hole Exporter | Create symbolic link
file:
src: "{{ pihole_exporter_bin_path }}/{{ pihole_exporter_exec_name }}-{{ pihole_exporter_version }}"
dest: /usr/bin/pihole_exporter
state: link
when: pihole_exporter_force_reinstall or pihole_exporter_check is failed
17 changes: 17 additions & 0 deletions tasks/exporter_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

- name: Pi-hole Exporter | Copy Daemon script
template:
src: pihole_exporter.service.j2
dest: /etc/systemd/system/pihole_exporter.service
mode: 0644
owner: root
group: root
notify: restart pihole_exporter

- name: Pi-hole Exporter | Configuring service
systemd:
name: pihole_exporter
state: "{{ pihole_exporter_state }}"
enabled: "{{ pihole_exporter_enabled }}"
daemon_reload: yes
14 changes: 14 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@
include: config.yml
tags:
- pihole_configure

- name: Pi-hole Exporter | Install
include: exporter_install.yml
when: pihole_exporter_install
tags:
- pihole_exporter
- pihole_exporter_install

- name: Pi-hole Exporter | Service
include: exporter_service.yml
when: pihole_exporter_install
tags:
- pihole_exporter
- pihole_exporter_service
25 changes: 25 additions & 0 deletions templates/pihole_exporter.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{ ansible_managed | comment }}
[Unit]
Description=pihole_exporter
Documentation={{ pihole_exporter_documentation_link }}
Requires=network-online.target
After=network-online.target

[Service]
PrivateTmp={{ pihole_exporter_private_tmp }}
User={{ pihole_exporter_user }}
Group={{ pihole_exporter_group }}
RuntimeDirectory=pihole_exporter
LimitNOFILE={{ pihole_exporter_max_files }}


ExecStart={{ pihole_exporter_exec_name }} {% for option in pihole_exporter_options %}-{{ option }} {% endfor %}

StandardOutput={{ pihole_exporter_log_output }}
StandardError={{ pihole_exporter_log_output }}
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM
Restart=always

[Install]
WantedBy=multi-user.target

0 comments on commit ef471b3

Please sign in to comment.