'Ansible module to stop and start `ssh` service

Question:

This scenario is used to explain the usage of modules in Ansible.

For this you have to stop and start a service named ssh.

Tasks to be done:- Write a task in main.yml file present in fresco_module\tasks folder.

The task is to stop and start the service named ssh using the service module in Ansible.

Note:

  • Run project install to install ansible.mainplaybook.yml file is provided to ansible-playbook.
  • Use the localhost for the inventory for ansible-playbook.

My Code:

- hosts: localhost
  become: yes
  tasks:
  - name: Stop and Start ssh
    service:
      name: ssh
      state: "{{ item }}"
    with_items:
      - stopped
      - started

Output:

PLAY [localhost] *******************************************************************************

TASK [Gathering Facts] *************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host localhost should use /usr/bin/python3,
 but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future 
Ansible release will default to using the discovered platform python for this host. See 
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more 
information. This feature will be removed in version 2.12. Deprecation warnings can be disabled
 by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost]

TASK [Stop and Start ssh] **********************************************************************
changed: [localhost] => (item=stopped)
ok: [localhost] => (item=started)

PLAY RECAP *************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Issue: The service is already running after Ansible stopped it, which looks like sshd was never stopped in the first place.

Command used to check the status: service ssh status. I used this command with state:stopped also but the sshd is still running. I have been facing this issue for so long. I tried with state:restarted also.



Solution 1:[1]

Hi Vivek and welcome to the community!

This should be an easy one. You can tell Ansible to restart the service directly without stopping and starting it in two separate steps.

The following code should work:

- hosts: localhost
  become: yes
  tasks:
  - name: Stop and Start ssh
    service:
      name: ssh
      state: restarted

This way Ansible ensures, that the ssh service was stopped an started - in short: restarted. You don't even need the with_items loop.

Solution 2:[2]

Ive tried the below and getting the same "sshd running " output , but the issue here i think is they want us to have both stop and start under one task. Also we are not allowed to use the restarted state :/

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


-

  name: "start ssh"
  service:
    name: ssh
    state: started

Solution 3:[3]

---
- hosts: localhost
  connection: local
  become: true
  become_method: sudo
  tasks:
    - name: stop a service
      service:
        name: ssh
        state: stopped
    - name: start a service
      service:
        name: ssh
        state: started

Add become-method sudo and task as stop and start the service.

Solution 4:[4]

Even i tried this problem with many other modules, like systemd, still i was not able to stop the service. But using command module, and passing 'sudo service ssh stop', i was able to stop the service. but still not passed the problem. Even tried felix's answer before, still not able to pass.

And also if anybody can help me with "Ansible Choral | 5 | Ansible Roles problem" would be great. even in that problem after getting 100% fs score not able to pass.

Solution 5:[5]

Just run the playbook for stopping starting ssh service without restart. or use this

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


-

  name: "start ssh"
  service:
    name: ssh
    state: started

After running playbook successfully. Just stop the service by sudo serivce ssh stop and then start the service sudo service ssh start. then just submit the test. you will pass the handson

Solution 6:[6]

Just write below command in your main yaml file. This will first stop the ssh service and then start it again.

- name: Stop service ssh, if started
  ansible.builtin.service:
    name: ssh
    state: stopped

- name: Start service ssh, if not started
  ansible.builtin.service:
    name: ssh
    state: started

Solution 7:[7]

On AWS EC2 instances the ssh service is sshd.

- name: restart ssh daemon
  hosts: all
  remote_user: ec2-user
  become: yes
  become_method: sudo
  tasks:
  - name: Stop and Start ssh
    service:
      name: sshd
      state: restarted

In the above YAML, replacing sshd with ssh will fail with "msg": "Could not find the requested service ssh

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thorian93
Solution 2 felix
Solution 3 Dharman
Solution 4 Carlito_15
Solution 5
Solution 6 Nilesh
Solution 7 Saurabh