# ansible service not work but command work

**URL:** <https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641>\
**Category:** Ansible Project\
**Created:** [February 8, 2019, 4:16pm UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641 "2019-02-08T16:16:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 8, 2019, 4:16pm UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/1 "2019-02-08T16:16:58Z")

</div>

Hi,

When i use service in ansible same this task :

- name: “stop nginx”  
service:  
name: nginx  
state: stopped

Same with “become: yes” and “pattern: my\_pattern\_file”

but with line “command: sudo systemctl stop nginx” it’s working !! WHY ?

Thanks you for you answers guy ! 🙂

---

<div class="post-metadata">

**Author:** ![Stefan\_Schmid](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Stefan\_Schmid](https://forum.ansible.com/u/Stefan_Schmid)\
**Post date:** [February 8, 2019, 5:27pm UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/2 "2019-02-08T17:27:16Z")

</div>

Run the Ansible command with -vvvv and post the  
output.

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 11, 2019, 9:01am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/3 "2019-02-11T09:01:25Z")

</div>

So,

When I run my playbook with this task :

- name: “stop nginx”  
service:  
name: nginx  
state: stopped

I have this message :

changed: [edeefreffe] =\> {“changed”: true, “msg”: “Service nginx not found on host, assuming it will exist on full run”}

but with this command ansible it’s work :

- name: “Arret du nginx”  
command: sudo systemctl stop nginx

Someone have a answer please ??!

Thanks you very much !! 🙂

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 11, 2019, 9:17am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/4 "2019-02-11T09:17:49Z")

</div>

And this same task in handler works :

- name: “stop nginx”  
service:  
name: nginx  
state: stopped

It’s very strange, i think that it’s bug in ansible ?

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [February 11, 2019, 9:47am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/5 "2019-02-11T09:47:53Z")

</div>

What happens when you use:

- name: "stop nginx"  
systemd:  
name: nginx  
state: stopped

I see that you are using `systemctl` in the command module but using `service` module in the task. Change it to systemd like above and tell us what happens.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [February 11, 2019, 10:14am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/6 "2019-02-11T10:14:49Z")

</div>

Did you put become\_user to root when you try with become: yes ?

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 11, 2019, 10:19am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/7 "2019-02-11T10:19:43Z")

</div>

What happens when you use:

- name: “stop nginx”  
systemd:  
name: nginx  
state: stopped

==\> It’s the same problem, the message error is :

> changed: [edeefreffe] =\> {“changed”: true, “msg”: “Service nginx not found on host, assuming it will exist on full run”}

Same in specify become or become\_user

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 11, 2019, 10:44am UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/8 "2019-02-11T10:44:37Z")

</div>

New probleme:

When i run this task it’s works :

- name: “stopper nginx”  
systemd:  
name: /my\_pattern/nginx  
state: stopped

But there is a warning message:

[WARNING]: The service (my\_pattern/nginx) is actually an init script but the system is  
managed by systemd

It’s normaly ?

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [February 11, 2019, 4:47pm UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/9 "2019-02-11T16:47:23Z")

</div>

Why did you change

name: nginx

to /my\_pattern/nginx

?

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 11, 2019, 5:09pm UTC](https://forum.ansible.com/t/ansible-service-not-work-but-command-work/29641/10 "2019-02-11T17:09:27Z")

</div>

Because this task works:

command: /home/…/…/nginx stop

but i want a syntax ansible.

Thx for answer guy !! 🙂
