# service module doesnt work

**URL:** <https://forum.ansible.com/t/service-module-doesnt-work/25055>\
**Category:** Ansible Project\
**Created:** [July 22, 2017, 7:38am UTC](https://forum.ansible.com/t/service-module-doesnt-work/25055 "2017-07-22T07:38:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurav1212](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Gaurav1212](https://forum.ansible.com/u/Gaurav1212)\
**Post date:** [July 22, 2017, 7:38am UTC](https://forum.ansible.com/t/service-module-doesnt-work/25055/1 "2017-07-22T07:38:10Z")

</div>

Hi,

I am trying to restart the cron and httpd services, but unable to do so with ansible.

Output shows OK, but changed=0. See below output.

[root@slc09vev ansible]# ansible-playbook restart.cron.pb.yml

PLAY [This is to call the restart\_cron role] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [slc11zrn]  
ok: [slc09vev]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=1 changed=0 unreachable=0 failed=0  
host2 : ok=1 changed=0 unreachable=0 failed=0

Here is my task file:

[root@slc09vev ansible]# cat roles/restart\_cron/tasks/cron\_restart.yml

- name:“restart crond service”
- service:  
name:crond state:restarted

What could be the reason for this?

---

<div class="post-metadata">

**Author:** ![Mike\_Eriksson](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mike\_Eriksson](https://forum.ansible.com/u/Mike_Eriksson)\
**Post date:** [July 22, 2017, 8:20am UTC](https://forum.ansible.com/t/service-module-doesnt-work/25055/2 "2017-07-22T08:20:12Z")

</div>

Gaurav,

It doesn’t look like the ‘restart crond service’ tasks runs at all - how are you calling it from the ‘restart.cron.pb.yml’ playbook?

I believe that the OK you get is from the ‘Gathering Facts’ task since that’s all that looks like it’s running.

Cheers, Mike

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [July 22, 2017, 8:51am UTC](https://forum.ansible.com/t/service-module-doesnt-work/25055/3 "2017-07-22T08:51:24Z")

</div>

Your task is never executed, you can see that in the output (there is  
no mention whatsoever of "restart crond service").

Additionally, the task as you have output it here would never run  
because of syntax errors (even if you had it correctly included) -  
because it's not valid yaml. It would need to look something similar  
to (note the spaces and newlines):

- name: "restart crond service"  
- service:  
&nbsp;&nbsp;&nbsp;&nbsp;name: crond  
&nbsp;&nbsp;&nbsp;&nbsp;state: restarted

Best regards

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 24, 2017, 1:11pm UTC](https://forum.ansible.com/t/service-module-doesnt-work/25055/4 "2017-07-24T13:11:14Z")

</div>

Your syntax is invalid:

- name:"restart crond service"  
- service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:crond state:restarted

You never get to the service task as you have an 'empty task' with  
just the name ... and then you structure yaml format as it were k=v,  
you probably want the following:

- name:"restart crond service"  
&nbsp;&nbsp;service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: crond  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: restarted

or

- name:"restart crond service"  
&nbsp;&nbsp;service: name=crond state=restarted
