diff --git a/defaults/main.yml b/defaults/main.yml index 351d644..cb7905d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,12 +1,16 @@ --- # defaults file for hass +hass_python_package: + - mysqlclient + - homeassistant hass_conf_folder: /var/lib/hass -hass_pacman_name: home-assistant +hass_venv_location: /opt/venv/hass hass_db_name: hass hass_db_user: hass hass_SQl_target_file: / hass_db_password: toto hass_repo: git hass_repo_branch: master -hass_user: [] +hass_username: hass +hass_users: [] diff --git a/handlers/main.yml b/handlers/main.yml index 03dac54..3b6b8b0 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -6,4 +6,5 @@ name: home-assistant state: restarted when: ansible_service_mgr == "systemd" - become: yes \ No newline at end of file + become: yes + diff --git a/tasks/main.yml b/tasks/main.yml index 855196a..ac836d9 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -28,19 +28,45 @@ priv: "{{ hass_db_name }}.*:ALL" state: present -- name: install HASS - pacman: - state: present # not required. choices: absent;latest;present. Desired state of the package. - name: "{{hass_pacman_name}}" # not required. Name or list of names of the packages to install, upgrade, or remove. +- name: create hass user + become: yes + user: + name: "{{hass_username}}" # required. Name of the user to create, remove or modify. + create_home: yes + state: present # not required. choices: absent;present. Whether the account should exist or not, taking action if the state is different from what is stated. + shell: usr/bin/nologin + home: "{{ hass_conf_folder }}" + comment: "Home Assistant daemon user" + + +- name: ensure venv folder exist + file: + name: "{{hass_venv_location}}" + state: directory + owner: "{{hass_username}}" + group: "{{hass_username}}" + mode: 0777 become: yes -- name: add {{user.name}} to hass group + +- name: create venv + pip: + name: "{{hass_python_package}}" + virtualenv: "{{hass_venv_location}}" + virtualenv_command: /usr/bin/python -m venv + become: true + become_user: "{{hass_username}}" + + + +- name: add user to hass group become: true user: name: "{{ item }}" - groups: hass + groups: + - "{{hass_username}}" append: true - with_items: "{{ hass_user }}" + with_items: "{{ hass_users }}" - name: check repo become: yes @@ -99,6 +125,21 @@ name: "auto commit HASS config" # not required. Description of a crontab entry or, if env is set, the name of environment variable. Required if state=absent. Note that if name is not set and state=present, then a new crontab entry will always be created, regardless of existing ones. become: yes +- name: copy systemd file + template: + src: home-assistant.service.j2 + dest: /etc/systemd/system/home-assistant.service + notify: + - restart hass + become: yes + register: hass_service_changed + +- name: Reload systemd + systemd: + daemon_reload: yes + become: yes + when: hass_service_changed.changed + - name: enable HASS service: name: home-assistant # required. Name of the service. diff --git a/templates/home-assistant.service.j2 b/templates/home-assistant.service.j2 new file mode 100644 index 0000000..331b488 --- /dev/null +++ b/templates/home-assistant.service.j2 @@ -0,0 +1,11 @@ +[Unit] +Description=Home Assistant +After=network-online.target + +[Service] +Type=simple +User={{hass_username}} +ExecStart={{hass_venv_location}}/bin/hass -c "{{hass_conf_folder}}" + +[Install] +WantedBy=multi-user.target \ No newline at end of file