Ansible Service Restart Failed

Steven Liao picture Steven Liao · May 11, 2015 · Viewed 27.7k times · Source

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.

Answer

Asfand Qazi picture Asfand Qazi · May 20, 2015

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