Starting/Stopping Homeassistant via Ansible does not work

Anyone use Ansible to deploy/maintain Homeassistant?

TL;DR - I can start/stop my homeassistant service when logged in, but when I use Ansible’s service (or systemd) module, I get a permission denied error if I’m trying to start HA if it’s stopped or stop HA when it’s started. Starting when its started or stopping when it’s stopped works, but of course, doesn’t help things.

I’m not sure whether this is a HA issue, Ansible, or systemd issue. If I’m logged in as the homeassistant user, I can start/stop homeassistant service just fine.

sudo systemctl stop home-assistant@homeassistant

I configured the user (in /etc/sudoers.d) to not require a password when the start/stop/restart call is made by the homeassistant user.

However, if I use Ansible, I get an issue where Ansible only receives a success when the state doesn’t change (i.e., starting when HA is running or stopping when HA is stopped). Any attempt to start when it’s stopped or stop when it’s started fails with a permission denied error. And in fact, in verbose mode, Ansible shows that it’s just reporting that from further down the stack, like maybe systemd. The payload that Ansible makes is the same. The result of that call is the only thing that’s different.

This is my homeassistant systemd unit file:

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
User=homeassistant
ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

Journalctl does not show anything when I launch via ansible-playbook. However, in /var/log/messages, I see this:

Jun 11 00:08:53 192.168.1.10 ansible-systemd: Invoked with no_block=False name= enabled=True daemon_reload=False state=started user=False masked=None
Jun 11 00:09:02 192.168.1.10 ansible-setup: Invoked with filter=* gather_subset=['all'] fact_path=/etc/ansible/facts.d gather_timeout=10

I’m wondering if that home assistant user is not getting to the systemd call.

I asked this question on Stackoverflow, but haven’t gotten a solid response about it. Anyone here know what’s going on?

Judging by the information you shared on SO, I think your problem is ansible - the error you are seeing is that ansible can’t run the command as superuser and deepak has given you what should be the correct answer - running your playbook ansible-playbook -u homeassistant playbooks/stop-homeassistant.yml -K and then you should type your sudo password at the prompt. It’s also possible you are logging in as a user that is not in /etc/sudoers.

I’d suggest you first resolve your sudo issues and if then there’s a problem bring us up to speed with the new error you are getting.

No, I get the same result when I use Deepak’s answer - start/start and stop/stop works, but starting when HA is stopped or stopping when HA is started does not work. Only difference is now I have to enter in my password.

I do feel that it has something to do with Ansible, though, or maybe systemd. It doesn’t make a lot of sense that I don’t have any permission issues when I’m logged in as homeassistant.