Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hms_mqtt_publisher role #14

Merged
merged 7 commits into from
Apr 8, 2024
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ jobs:
with:
ROLE_NAME: proxmox_lxc

test-role-hms-mqtt-publisher:
name: Test hms_mqtt_publisher role
uses: ./.github/workflows/test_role.yml
with:
ROLE_NAME: hms_mqtt_publisher

ansible-test:
name: Run ansible-test
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_role.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ jobs:
ansible-galaxy collection install -r tests/requirements.yml -p /tmp

- name: Run yamllint
run: yamllint .
run: yamllint roles/${{ inputs.ROLE_NAME }}

- name: Run ansible-lint
run: ansible-lint
run: ansible-lint roles/${{ inputs.ROLE_NAME }}

- name: Test syntax
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.cache
.idea
.vscode

bellackn-homelab-*.tar.gz
tests/output
2 changes: 2 additions & 0 deletions changelogs/fragments/v5.1.0_summary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release_summary: |-
Add hms_mqtt_publisher role
4 changes: 3 additions & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: homelab
version: 5.0.1
readme: README.md
authors:
- Nico Bellack <[email protected]>
- Nico Bellack <[email protected]>
description: Holds various utils that are handy for a homelab infrastructure setup.
license:
- MIT
Expand All @@ -13,6 +13,8 @@ tags:
- lxc
- proxmox
- tools
- mqtt
- hms
repository: https://github.com/bellackn/ansible-collection-homelab
documentation: https://github.com/bellackn/ansible-collection-homelab
homepage: https://github.com/bellackn/ansible-collection-homelab
Expand Down
55 changes: 55 additions & 0 deletions roles/hms_mqtt_publisher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
bellackn.hms-mqtt-publisher
===========================

This role deploys a compiled binary of the [hms-mqtt-publisher][1] to a host. It
conveniently sets it up as a systemd daemon.

How to Use
----------

1. Compile the binary for the wanted architecture using `cargo build`.
2. Set `hms_mqtt_publisher_bin_path` to where you've put the compiled binary.
3. Set the config variables for Home Assistant and/or simple MQTT publishing.
4. Execute the role.

Role Variables
--------------

See [defaults/main.yml](defaults/main.yml).

Example Playbook
----------------

```yaml
---
- name: Deploy MQTT publisher to Raspberry Pi
hosts: pi
gather_facts: true
become: true
vars:
hms_mqtt_publisher_bin_path: /tmp/hms-mqtt-publish
hms_mqtt_publisher_inverter_host: 192.168.1.2
hms_mqtt_publisher_homeassistant:
host: homeassistant.local
username: "{{ vault_hms_mqtt_publisher_homeassistant_username }}"
password: "{{ vault_hms_mqtt_publisher_homeassistant_password }}"
hms_mqtt_publisher_simple:
host: mqtt.local
collections:
- bellackn.homelab
roles:
- hms_mqtt_publisher
```

License
-------

MIT

Author Information
------------------

[Nico Bellack](mailto:[email protected])


[1]: https://github.com/DennisOSRM/hms-mqtt-publisher
34 changes: 34 additions & 0 deletions roles/hms_mqtt_publisher/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# The method to use for installing the binary
# TODO: For now, only 'local' is supported
hms_mqtt_publisher_install_method: local

# Path to the compiled binary (used for "local" method)
hms_mqtt_publisher_bin_path: /tmp/hms-mqtt-publish

# The directory into which all necessary files will be deployed
hms_mqtt_publisher_basedir: /opt/hms-mqtt-publisher

# The user who runs the systemd service
hms_mqtt_publisher_svc_user: root
hms_mqtt_publisher_svc_group: root

# Inverter host
hms_mqtt_publisher_inverter_host: ""

# Update interval
hms_mqtt_publisher_update_interval: 30500

# Home Assistant config
hms_mqtt_publisher_homeassistant: {}
# host:
# port:
# username:
# password:

# Simple MQTT config:
hms_mqtt_publisher_simple: {}
# host:
# port:
# username:
# password:
7 changes: 7 additions & 0 deletions roles/hms_mqtt_publisher/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Enable and (re-)start systemd service
ansible.builtin.systemd:
name: hms-mqtt-publish
state: restarted
enabled: true
daemon_reload: true
11 changes: 11 additions & 0 deletions roles/hms_mqtt_publisher/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
galaxy_info:
role_name: hms_mqtt_publisher
namespace: bellackn
author: Nico Bellack
description: Deploys the hms-mqtt-publisher as systemd service
license: MIT
min_ansible_version: '2.9'
galaxy_tags:
- hms
- mqtt
40 changes: 40 additions & 0 deletions roles/hms_mqtt_publisher/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
- name: Create base directory
ansible.builtin.file:
path: "{{ hms_mqtt_publisher_basedir }}"
state: directory
owner: "{{ hms_mqtt_publisher_svc_user }}"
group: "{{ hms_mqtt_publisher_svc_group }}"
mode: "0774"

# TODO: Mirror the compiled binaries somewhere online/add 'url/mirror' method
- name: Copy binary to host
ansible.builtin.copy:
src: "{{ hms_mqtt_publisher_bin_path }}"
dest: "{{ hms_mqtt_publisher_basedir }}/hms-mqtt-publish"
owner: "{{ hms_mqtt_publisher_svc_user }}"
group: "{{ hms_mqtt_publisher_svc_group }}"
mode: "0774"
notify:
- Enable and (re-)start systemd service
when: hms_mqtt_publisher_install_method == "local"

- name: Copy config to host
ansible.builtin.template:
src: config.toml.j2
dest: "{{ hms_mqtt_publisher_basedir }}/config.toml"
owner: "{{ hms_mqtt_publisher_svc_user }}"
group: "{{ hms_mqtt_publisher_svc_group }}"
mode: "0600"
notify:
- Enable and (re-)start systemd service

- name: Deploy systemd service file
ansible.builtin.template:
src: hms-mqtt-publish.service.j2
dest: /etc/systemd/system/hms-mqtt-publish.service
owner: "{{ hms_mqtt_publisher_svc_user }}"
group: "{{ hms_mqtt_publisher_svc_group }}"
mode: "0644"
notify:
- Enable and (re-)start systemd service
18 changes: 18 additions & 0 deletions roles/hms_mqtt_publisher/templates/config.toml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
inverter_host = "{{ hms_mqtt_publisher_inverter_host }}"
update_interval = {{ hms_mqtt_publisher_update_interval }}

{% if hms_mqtt_publisher_homeassistant %}
[home_assistant]
host = "{{ hms_mqtt_publisher_homeassistant.host }}"
port = {{ hms_mqtt_publisher_homeassistant.port | default('1883')}}
username = "{{ hms_mqtt_publisher_homeassistant.username | default('') }}"
password = "{{ hms_mqtt_publisher_homeassistant.password | default('') }}"
{% endif %}

{% if hms_mqtt_publisher_simple %}
[simple_mqtt]
host = "{{ hms_mqtt_publisher_simple.host }}"
port = {{ hms_mqtt_publisher_simple.port | default('1883') }}
username = "{{ hms_mqtt_publisher_simple.username | default('') }}"
password = "{{ hms_mqtt_publisher_simple.password | default('') }}"
{% endif %}
14 changes: 14 additions & 0 deletions roles/hms_mqtt_publisher/templates/hms-mqtt-publish.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=HMS MQTT Publisher (cf. GitHub: DennisOSRM/hms-mqtt-publisher)
After=multi-user.target

[Service]
ExecStart={{ hms_mqtt_publisher_basedir }}/hms-mqtt-publish
WorkingDirectory={{ hms_mqtt_publisher_basedir }}
Restart=on-failure
Type=exec
User={{ hms_mqtt_publisher_svc_user }}
Group={{ hms_mqtt_publisher_svc_group }}

[Install]
WantedBy=multi-user.target
2 changes: 2 additions & 0 deletions roles/hms_mqtt_publisher/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
localhost
3 changes: 3 additions & 0 deletions roles/hms_mqtt_publisher/tests/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
roles: []
collections: []
5 changes: 5 additions & 0 deletions roles/hms_mqtt_publisher/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Test role syntax
hosts: test
roles:
- hms_mqtt_publisher
2 changes: 1 addition & 1 deletion roles/proxmox_lxc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ MIT
Author Information
------------------

[Nico Bellack](mailto:[email protected])
[Nico Bellack](mailto:[email protected])

Acknowledgements
----------------
Expand Down
Loading