title: Ansible Service Restart Failed
body: <p>I've been having some trouble with restarting the SSH daemon with Ansible.</p>
  
  <p>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: <a href="https://atlas.hashicorp.com/ubuntu/boxes/trusty64">ubuntu/trusty64</a>)</p>
  
  <p><strong>tl;dr</strong>: There appears to be something wrong with the way I'm invoking the service syntax.</p>
  
  <h1>Problem With Original Use Case (Handler)</h1>
  
  <p><strong>Playbook</strong></p>
  
  <pre><code>- 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
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>NOTIFIED: [restart ssh] 
  
  failed: [default] =&gt; {"failed": true}
  
  FATAL: all hosts have already failed -- aborting
  </code></pre>
  
  <p>The nginx handler completed successfully with nearly identical syntax.</p>
  
  <h1>Task Also Fails</h1>
  
  <p><strong>Playbook</strong></p>
  
  <pre><code>- name: Restart SSH server
    sudo: yes
    service: name=ssh state=restarted
  </code></pre>
  
  <p>Same output as the handler use case.</p>
  
  <h1>Ad Hoc Command Also Fails</h1>
  
  <p><strong>Shell</strong></p>
  
  <pre><code>&gt; ansible all -i ansible_inventory -u vagrant -k -m service -a "name=ssh state=restarted"
  </code></pre>
  
  <p><strong>Inventory</strong></p>
  
  <pre><code>127.0.0.1:8022
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>127.0.0.1 | FAILED &gt;&gt; {
      "failed": true,
      "msg": ""
  }
  </code></pre>
  
  <h1>Shell command in box works</h1>
  
  <p>When I SSH in and run the usual command, everything works fine.</p>
  
  <pre><code>&gt; vagrant ssh
  &gt; sudo service ssh restart
  ssh stop/waiting
  ssh start/running, process 7899
  &gt; echo $?
  0
  </code></pre>
  
  <h1>Command task also works</h1>
  
  <p><strong>Output</strong></p>
  
  <pre><code>TASK: [Restart SSH server] ****************************************************
  changed: [default] =&gt; {"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"]}
  </code></pre>
  
  <p>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.</p>