Ansible module to stop and start `ssh` service
Asked Answered
F

7

7

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.

Frink answered 19/6, 2020 at 9:51 Comment(4)
Okay, now you lost me. What is your goal in the end? Because what you are doing in your question and what I am suggesting in my answer would result in a restarted and therefore running ssh daemon. Isn't that the outcome you need?Wheen
Take this code: - hosts: localhost become: yes tasks: - name: Stop and Start ssh service: name: ssh state: stopped and check the status using "service ssh status" . It will show sshd is running not stopped. Similarly on stage restarted it will not stop-start the service.Frink
Hi Vivek Gaur, if you have cleared teh stop and start ssh service handson. please assist me to clear the handson. As i am also facing the same issue that you have specifie.Sheryllshetland
Refer my YouTube video youtu.be/dlwSit9K1lkFrink
W
12

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.

Wheen answered 19/6, 2020 at 17:3 Comment(5)
I tried this, but the service is not stopped and later started. It keeps running.Frink
Please don't get me wrong: But that is what you are going for aren't you? If you want the service stopped, you would only perform a stop like state: stopped. Or am I missing something here?Wheen
If I use state: stopped then also service does not stop. It keeps running. I simply want service to STOP first and then, START.Frink
If I replace service module with command: service ssh restart then, it STOPS and then, STARTS. This thing cant be achieved with service: name: ssh state: restarted.Frink
Alright, so we agree that a restart works as expected, but stopping and then starting does not seem to work properly, correct? Can you check the sshd logs, what exactly happens to the service during the Ansible run? Do me a favor and configure your inventory, so that your localhost uses the local connection rather than ssh and check again.Wheen
S
1

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
Sacrificial answered 9/7, 2020 at 15:6 Comment(0)
J
1
---
- 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.

Jabiru answered 4/2, 2021 at 7:40 Comment(0)
S
1

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

Skyway answered 10/2, 2022 at 2:3 Comment(0)
F
0

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

Flannery answered 19/6, 2020 at 9:51 Comment(1)
Hi Carlito_15, as per your instruction i have pasted the below code in main.yml - name: "Stop ssh" service: name: ssh state: stopped - name: "start ssh" service: name: ssh state: started after that i haved run the play book successfully and then excuted the sudo service ssh stop command in terminal and then i have executed sudo service ssh start command and then submitted the test . But i have not passed the handson. Please suggest me if i have missed anything or i have done anything wrong and assist me in clearing the handson.Sheryllshetland
F
0

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.

Flannery answered 9/7, 2020 at 17:13 Comment(2)
Same issue is with me. I tried with command then it is stopped otherwise not. I tried many solutions but none of them works.Frink
Bro can you help me with this, Ansible Choral | 5 | Ansible Roles problem. if you have done it already.Flannery
R
0

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
Relive answered 26/1, 2022 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.