Question:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- 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
1 2 3 4 5 6 |
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
1 2 3 4 |
- name: Restart SSH server sudo: yes service: name=ssh state=restarted |
Same output as the handler use case.
Ad Hoc Command Also Fails
Shell
1 2 |
> ansible all -i ansible_inventory -u vagrant -k -m service -a "name=ssh state=restarted" |
Inventory
1 2 |
127.0.0.1:8022 |
Output
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 |
> vagrant ssh > sudo service ssh restart ssh stop/waiting ssh start/running, process 7899 > echo $? 0 |
Command task also works
Output
1 2 3 |
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:
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:
1 2 3 |
- name: restart sshd command: service ssh restart |