Ansible Service Restart Failed
Asked Answered
I

1

24

I've been having some trouble with restarting the SSH daemon with Ansible.

I'm using the latest software as of May 11 2015 (Ansible 1.9.1 / Vagrant 1.7.2 / VirtualBox 4.3.26 / Host: OS X 10.10.1 / Guest: ubuntu/trusty64)

tl;dr: There appears to be something wrong with the way I'm invoking the service syntax.

Problem With Original Use Case (Handler)

Playbook

- hosts: all
- remote_user: vagrant
- tasks:

  ...

  - name: Forbid SSH root login
    sudo: yes
    lineinfile: dest=/etc/ssh/sshd_config regexp="^PermitRootLogin" line="permitRootLogin no" state=present
    notify:
      - restart ssh

  ...

- handlers:
  - name: restart ssh
    sudo: yes
    service: name=ssh state=restarted

Output

NOTIFIED: [restart ssh] 

failed: [default] => {"failed": true}

FATAL: all hosts have already failed -- aborting

The nginx handler completed successfully with nearly identical syntax.

Task Also Fails

Playbook

- name: Restart SSH server
  sudo: yes
  service: name=ssh state=restarted

Same output as the handler use case.

Ad Hoc Command Also Fails

Shell

> ansible all -i ansible_inventory -u vagrant -k -m service -a "name=ssh state=restarted"

Inventory

127.0.0.1:8022

Output

127.0.0.1 | FAILED >> {
    "failed": true,
    "msg": ""
}

Shell command in box works

When I SSH in and run the usual command, everything works fine.

> vagrant ssh
> sudo service ssh restart
ssh stop/waiting
ssh start/running, process 7899
> echo $?
0

Command task also works

Output

TASK: [Restart SSH server] ****************************************************
changed: [default] => {"changed": true, "cmd": ["service", "ssh", "restart"], "delta": "0:00:00.060220", "end": "2015-05-11 07:59:25.310183", "rc": 0, "start": "2015-05-11 07:59:25.249963", "stderr": "", "stdout": "ssh stop/waiting\nssh start/running, process 8553", "warnings": ["Consider using service module rather than running service"]}

As we can see in the warning, we're supposed to use the service module, but I'm still not sure where the snag is.

Innings answered 11/5, 2015 at 8:8 Comment(4)
Same issue here with same setup. First investigations seem to show that restart is invoked with '/etc/init.d/ssh restart' (SysV style) instead of 'restart ssh' (upstart style). There is an issue opened on this github.com/ansible/ansible-modules-core/issues/1298Divest
That seems like a bug like @Divest stated. Also unrelated, but there seems to be a typo in permitRootLogin no. Use camelcase.Cassicassia
seems the bug is being taken care of github.com/ansible/ansible-modules-core/commit/…Divest
I confirm that the current devel (2.0.0.14080.ded6e9b) fixed the problem.Divest
H
30

As the comments above state, this is an Ansible issue that will apparently be fixed in the 2.0 release.

I just changed my handler to use the command module and moved on:

- name: restart sshd
  command: service ssh restart
Hotchpotch answered 20/5, 2015 at 11:58 Comment(9)
According to this github.com/ansible/ansible-modules-core/commit/… the bug will be fixed in Ansible 1.9.2 so then service module can be used againHotchpotch
I still experience the bug with ansible-1.9.4-1.fc23.noarch AFAICSPesthouse
Same over here with ansible 2.0.1.0 and supervisord 3.0b2 in Ubuntu 14.04.1Kufic
I was having problems restarting php7.0-fpm. Switching from the service module to command: service php7.0-fpm restart fixed this for me.Salvadorsalvadore
Same problem still on 2.2.1.0 :)Archangel
Had the issue when using 1.9. Recently upgraded to 2.3 and the issue has been resolved since then.Phyllys
Still happening ansible 2.3.0.0Romaine
Yes, still happening in 2.4.1.0 :-( And it's even warning me about it: " [WARNING]: Consider using service module rather than running service" Even though it doesn't work :-DNikitanikki
Thank you for this help. 5 years later, the command can also be systemctl restart sshdBatista

© 2022 - 2024 — McMap. All rights reserved.