Yes, this is a custom role. Here is the tasks/main.yml.
---
# tasks file for roles/create-user
- name: "Create {{ user_name }} group so we can control the gid"
group:
gid: "{{ gid }}"
name: "{{ user_name }}"
state: present
system: True
become: True
- name: "Create {{ user_name }} user"
user:
comment: "{{ service }} service"
createhome: "{{ create_home }}"
group: "{{ user_name }}"
name: "{{ user_name }}"
shell: /usr/sbin/nologin
state: present
system: True
uid: "{{ uid }}"
become: True
- name: "Add {{ default_user }} to the {{ user_name }} group"
user:
append: True
groups: "{{ user_name }}"
name: "{{ default_user }}"
become: Tru
I makes sure that the user and group exists and they have the passed in UID and GID and adds the “default user” which is my usual login to the group.
On most Debian based Linux instances a user must be a member of the dialout group to have permission to read and write to serial devices, like a Zwave controller. It’s discussed in the Linux installation instructions: openHAB on Linux | openHAB
- They allow the container to access the hardware on the host. In this case ttyUSB0 is the Zwave controller and ttyUSB1 is the Zigbee Coordinator. A container does not have access to any serial devices like this without explicitly giving it permission to access them.
This is not needed in OH 3. It was just a work around to deal with the fact that when the cache was cleared in OH 2, some of us would have openHAB fail to come up properly that first time and require a restart to be operational. I’ve removed that block since moving to OH 3.
The first docker-container task pulls the new version (if there is one) and recreates the container with the given properties. It registers a variable to indicate that a new image was pulled. Only if a new image was pulled it will wait five minutes for OH to completely come back up. Then it runs the pull/restart again. This time there won’t be a new image to pull so the container is just restarted.
But again, this was to work around a bug in OH 2.5 that doesn’t exist in OH 3 so I’ve removed those.
And because this was a temporary fix for a bug, I didn’t spend much time thinking about it or working on it. I just copied the first docker_container task as written.
I’ve also dropped InfluxDB and Grafana so my openHAB tasks have changed significantly. In fact I currently have it so it will install a test version (OH 3) and a production version (2.5) based on the value of a variable. But now that I’m fully on OH 3 all of that stuff is not used any more.
The new openHAB task is:
---
# tasks file for roles/openhab
- name: Debug
debug:
msg: |
openhab_home = {{ openhab_home }}
repo = {{ openhab_conf_repo }}
version = {{ openhab_version }}
- name: Create the openhab user and group
include_role:
name: create-user
vars:
uid: "{{ openhab_uid }}"
gid: "{{ openhab_uid }}"
user_name: openhab
create_home: False
service: openHAB
- block:
- name: Add openhab user to the dialout group
user:
append: True
groups: dialout
name: openhab
- name: Create if necessary and set the permissions on the openHAB data folder
file:
path: "{{ openhab_home }}"
state: directory
owner: openhab
group: openhab
mode: u=rwx,g=rwx,o=rx
become: True
- name: See if config is already present
stat:
path: "{{ openhab_home }}/userdata/etc/version.properties"
register: conf_present
- name: Check to see if its is up
shell: "nc -vz {{ git_host }} {{ git_port }}"
register: git_running
changed_when: False
failed_when: False
- name: Checkout openHAB configs if this is a new install
git:
repo: "{{ openhab_conf_repo }}"
dest: "{{ openhab_home }}"
accept_hostkey: True
when: (git_running['stderr'] is match(".* succeeded!")) and
(not conf_present.stat.exists)
- name: Create missing folders
file:
path: "{{ item }}"
state: directory
owner: openhab
group: openhab
mode: u=rwx,g=rwx,o=rx
loop:
- "{{ openhab_home }}/userdata/cache"
- "{{ openhab_home }}/userdata/logs"
- "{{ openhab_home }}/userdata/persistence"
- "{{ openhab_home }}/userdata/tmp"
become: True
- name: Change ownership of openHAB configs
file:
path: "{{ openhab_home }}"
owner: openhab
group: openhab
recurse: yes
become: True
when: (git_running['stderr'] is match(".* succeeded!")) and
(not conf_present.stat.exists)
# Kept for reference but in OH 3 I've moved to rrd4j and built in charting
#- name: Create the InfluxDB database
# influxdb_database:
# hostname: "{{ influxdb_ip_address }}"
# database_name: "{{ openhab_influxdb_database_name }}"
# state: present
# username: "{{ influxdb_admin_user }}"
# password: "{{ influxdb_admin_password }}"
#
#- name: Create the InfluxDB openHAB user and grant permissions
# influxdb_user:
# hostname: "{{ influxdb_ip_address }}"
# user_name: "{{ influxdb_openhab_user }}"
# user_password: "{{ influxdb_openhab_password }}"
# login_username: "{{ influxdb_admin_user }}"
# login_password: "{{ influxdb_admin_password }}"
# grants:
# - database: "{{ openhab_influxdb_database_name }}"
# privilege: 'ALL'
#
#- name: Create InfluxDB Grafana user and grant read permissions
# influxdb_user:
# hostname: "{{ influxdb_ip_address }}"
# user_name: "{{ influxdb_grafana_user }}"
# user_password: "{{ influxdb_grafana_password }}"
# login_username: "{{ influxdb_admin_user }}"
# login_password: "{{ influxdb_admin_password }}"
# grants:
# - database: "{{ openhab_influxdb_database_name }}"
# privilege: 'READ'
- name: Check the current version of openHAB
shell: grep openhab-distro {{ openhab_home }}/userdata/etc/version.properties | cut -d ' ' -f 4
register: old_version
when: conf_present.stat.exists
changed_when: False
- name: Pull/update the openHAB docker image
docker_container:
detach: True
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0:rwm"
- "/dev/ttyUSB1:/dev/ttyUSB1:rwm"
env:
CRYPTO_POLICY: unlimited
hostname: "{{ ansible_fqdn }}"
image: openhab/openhab:{{ openhab_version }}
log_driver: syslog
name: openhab
network_mode: host
pull: True
restart: False
restart_policy: always
tty: True
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- "{{ openhab_home }}/conf:/openhab/conf"
- "{{ openhab_home }}/userdata:/openhab/userdata"
- "{{ openhab_home }}/addons:/openhab/addons"
register: openhab_pulled
All those openhab variables are defined in defaults. And when I want to deploy a test version on a different machine I override the defaults, primarily changing the openhab_version variable.