ansible module stop and start service ssh

1.6k Views Asked by At

I am trying to clear a hands on in HackerRank, where the task is to stop and start the service named ssh using service module. I have used the below code.

- name: "Stop ssh"
  service:
    name: ssh
    state: stopped
- name: "start ssh"
  service:
    name: ssh
    state: started

Can you please guide me to clear the hands on.

1

There are 1 best solutions below

0
On

The service handling ssh on Linux is called sshd like a lot of other services, where the d stands for daemon.

So your correct tasks would be:

- name: Stop ssh
  service:
    name: sshd
    state: stopped
- name: Start ssh
  service:
    name: sshd
    state: started

This said, since Ansible connects through ssh, I am unsure how this will really react when the service is stopped.

Most of the linux services, though do also have a restart instruction, that you can also use in Ansible, with the state value restarted.

- name: Restart ssh
  service:
    name: sshd
    state: restarted