hass/tasks/main.yml

174 lines
4.9 KiB
YAML
Raw Normal View History

2020-01-29 20:56:37 +00:00
---
# tasks file for hass
- name: "Ensure database is present"
become: yes
mysql_db:
name: "{{ hass_db_name }}"
collation: utf8mb4_unicode_ci
encoding: utf8mb4
state: present
register: hass_database_creation
- name: import DATA in database in case of creation
become: yes
mysql_db:
name: "{{ hass_db_name }}"
state: import
target: "{{ hass_SQl_target_file }}"
when: hass_database_creation.changed == true and hass_SQl_target_file is defined
- name: "Ensure db user is present"
become: yes
mysql_user:
name: "{{ hass_db_user }}"
host: localhost
password: "{{ hass_db_password }}"
priv: "{{ hass_db_name }}.*:ALL"
state: present
2020-03-01 15:18:48 +00:00
- name: create hass user
2020-01-29 20:56:37 +00:00
become: yes
2020-03-01 15:18:48 +00:00
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.
2020-03-02 20:19:26 +00:00
shell: /usr/bin/nologin
2020-03-01 15:18:48 +00:00
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
2020-01-29 20:56:37 +00:00
2020-03-01 15:18:48 +00:00
- 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
2020-02-16 15:22:01 +00:00
become: true
2020-02-02 11:45:38 +00:00
user:
name: "{{ item }}"
2020-03-01 15:18:48 +00:00
groups:
- "{{hass_username}}"
2020-02-02 11:45:38 +00:00
append: true
2020-03-01 15:18:48 +00:00
with_items: "{{ hass_users }}"
2020-01-29 20:56:37 +00:00
- name: check repo
2020-01-30 19:29:46 +00:00
become: yes
stat:
path: "{{hass_conf_folder}}/.git" # required. The full path of the file/object to get the facts of.
2020-01-29 20:56:37 +00:00
register: hass_repo_exist
- name: clean conf folder if repo not exist
become: yes
file:
path: "{{hass_conf_folder}}"
state: absent
2020-01-30 19:29:46 +00:00
when: hass_repo_exist.stat.exists == false
2020-01-29 20:56:37 +00:00
- name: recreate conf folder
become: yes
file:
path: "{{hass_conf_folder}}"
state: directory
owner: "{{user.name}}"
2020-01-30 19:29:46 +00:00
when: hass_repo_exist.stat.exists == false
2020-01-29 20:56:37 +00:00
2020-03-02 20:59:21 +00:00
2020-01-29 20:56:37 +00:00
- name: install conf file
git:
2020-03-02 20:59:21 +00:00
accept_hostkey: yes
2020-01-29 20:56:37 +00:00
dest: "{{hass_conf_folder}}" # required. The path of where the repository should be checked out. This parameter is required, unless C(clone) is set to C(no).
2020-02-16 15:22:01 +00:00
repo: "{{hass_repo}}" # required. git, SSH, or HTTP(S) protocol address of the git repository.
2020-01-29 20:56:37 +00:00
clone: yes # not required. If C(no), do not clone the repository if it does not exist locally
version: "{{hass_repo_branch}}"
notify: restart hass
2020-02-16 15:22:01 +00:00
become: true
2020-01-30 19:29:46 +00:00
when: hass_repo_exist.stat.exists == false
2020-01-29 20:56:37 +00:00
- name: change owner
become: yes
file:
state: directory
dest: "{{hass_conf_folder}}"
owner: hass
group: hass
2020-04-14 18:08:23 +00:00
mode: 0770
2020-01-29 20:56:37 +00:00
recurse: yes
2020-05-18 08:34:14 +00:00
- name: create .ssh folder
file:
state: directory
dest: "{{hass_conf_folder}}/.ssh/"
owner: hass
group: hass
mode: 0700
recurse: yes
become: yes
- name: create ssh key pair
openssh_keypair:
path: "{{hass_conf_folder}}/.ssh/id_rsa"
owner: "{{hass_username}}"
become: yes
register: hass_SSH_creation
- name: print ssh public key
debug:
msg: "{{ hass_SSH_creation }}"
2020-01-29 20:56:37 +00:00
- name: copy failtoban config
template:
src: fail2ban/hass.local.j2
dest: /etc/fail2ban/jail.d/hass.local
notify: restart fail2ban
become: yes
2020-01-30 19:29:46 +00:00
- name: create cron for hass
2020-01-29 20:56:37 +00:00
cron:
2020-01-30 19:29:46 +00:00
job: "sh {{hass_conf_folder}}/backup_git.sh"
2020-01-29 20:56:37 +00:00
user: root # not required. The specific user whose crontab should be modified.
2020-01-30 19:29:46 +00:00
minute: "0" # not required. Minute when the job should run ( 0-59, *, */2, etc )
hour: "3"
2020-01-29 20:56:37 +00:00
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
2020-03-01 15:18:48 +00:00
- 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
2020-01-29 20:56:37 +00:00
- name: enable HASS
service:
name: home-assistant # required. Name of the service.
enabled: true # not required. Whether the service should start on boot. B(At least one of state and enabled are required.)
state: started # not required. choices: reloaded;restarted;started;stopped. C(started)/C(stopped) are idempotent actions that will not run commands unless necessary. C(restarted) will always bounce the service. C(reloaded) will always reload. B(At least one of state and enabled are required.) Note that reloaded will start the service if it is not already started, even if your chosen init system wouldn't normally.
become: yes