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:
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.
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.
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
---
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With