# tell 'service' module to continue with playbook, if service isn't found

**URL:** <https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878>\
**Category:** Ansible Project\
**Created:** [October 15, 2021, 9:20am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878 "2021-10-15T09:20:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 15, 2021, 9:20am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/1 "2021-10-15T09:20:34Z")

</div>

I am trying to install postgresql via a playbook and want to make sure the service is not running prior installation on the target (rhel7/8 & debian10) machines.

However my task below

- name: stop postgresql.service from systemd if running  
service:  
name: postgresql.service  
state: stopped

# enabled: false

become: yes

is failing (‘Could not find the requested service postgresql.service: host’)

so, I guess I have to build something that directs the target to ignore that and continues with the playbook. I just can’t see anything

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 15, 2021, 9:41am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/2 "2021-10-15T09:41:11Z")

</div>

> I am trying to install postgresql via a playbook and want to make sure the service is not running prior installation on the target (rhel7/8 & debian10) machines.
> 
> However my task below
> 
> ```
> \- name: stop postgresql\.service from systemd if running
> service:
> name: postgresql\.service
> state: stopped
> \# enabled: false
> become: yes
> 
> ```
> 
> is failing ('Could not find the requested service postgresql.service: host')
> 
> so, I guess I have to build something that directs the target to ignore that and continues with the playbook. I just can't see anything

You can check the status of the postgresql service as follows:

&nbsp;&nbsp;- service\_facts:

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

TASK [service\_facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [papas]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [papas] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "postgresql",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "sysv",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "running"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 15, 2021, 10:04am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/3 "2021-10-15T10:04:12Z")

</div>

are those arguments to the the "service" module? how exaclty would I weave those into my task? I tried

&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
&nbsp;&nbsp;&nbsp;&nbsp;service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# enabled: false  
&nbsp;&nbsp;&nbsp;&nbsp;become: yes

but that did not work

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 15, 2021, 10:07am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/4 "2021-10-15T10:07:13Z")

</div>

No, use my tasks verbatim.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 15, 2021, 10:40am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/5 "2021-10-15T10:40:41Z")

</div>

hm ... pasting this verbatim to my plabook does not survice a --symtax-check

I figured out soome sort of sense-making output with adding 2 tasks based on what you posted.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 15, 2021, 11:01am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/6 "2021-10-15T11:01:32Z")

</div>

> > > > > I am trying to install postgresql via a playbook and want to make sure the service is not running prior installation on the target (rhel7/8 & debian10) machines.
> > > > > 
> > > > > However my task below
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp; - name: stop postgresql.service from systemd if running  
> > > > > &nbsp;&nbsp;&nbsp; service:  
> > > > > &nbsp;&nbsp;&nbsp; name: postgresql.service  
> > > > > &nbsp;&nbsp;&nbsp; state: stopped  
> > > > > &nbsp;&nbsp;&nbsp; # enabled: false  
> > > > > &nbsp;&nbsp;&nbsp; become: yes
> > > > > 
> > > > > is failing ('Could not find the requested service postgresql.service: host')
> > > > > 
> > > > > so, I guess I have to build something that directs the target to ignore that and continues with the playbook. I just can't see anything
> > > > 
> > > > You can check the status of the postgresql service as follows:
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
> > > > 
> > > > TASK [service\_facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > ok: [papas]
> > > > 
> > > > TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > ok: [papas] =\> {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "postgresql",  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "sysv",  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "running"  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > }
> > > 
> > > are those arguments to the the "service" module? how exaclty would I weave those into my task? I tried
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# enabled: false  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes
> > > 
> > > but that did not work
> > 
> > No, use my tasks verbatim.
> 
> hm ... pasting this verbatim to my plabook does not survice a --symtax-check

What is the diagnosis of the syntax check?

> I figured out soome sort of sense-making output with adding 2 tasks based on what you posted.
> 
> ############################################################  
> &nbsp;&nbsp;&nbsp;- name: gather service facts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:
> 
> &nbsp;&nbsp;&nbsp;- name: return postgres running or not  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
> ############################################################
> 
> however ... what do I do whith that output ('state: absent' in my test scenario)?
> 
> Maybe I need to put this to work as some sort of condition deciding whether to run a 'service' task disabling the postgresql.service or not?

Correct. For example:

when: ansible\_facts.services.postgresql.state == 'running'

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 15, 2021, 11:37am UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/7 "2021-10-15T11:37:38Z")

</div>



---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 15, 2021, 2:11pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/8 "2021-10-15T14:11:32Z")

</div>

> > > > > > > I am trying to install postgresql via a playbook and want to make sure the service is not running prior installation on the target (rhel7/8 & debian10) machines.
> > > > > > > 
> > > > > > > However my task below
> > > > > > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; - name: stop postgresql.service from systemd if running  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; service:  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; name: postgresql.service  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; state: stopped  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; # enabled: false  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; become: yes
> > > > > > > 
> > > > > > > is failing ('Could not find the requested service postgresql.service: host')
> > > > > > > 
> > > > > > > so, I guess I have to build something that directs the target to ignore that and continues with the playbook. I just can't see anything
> > > > > > 
> > > > > > You can check the status of the postgresql service as follows:
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
> > > > > > 
> > > > > > TASK [service\_facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > > > ok: [papas]
> > > > > > 
> > > > > > TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > > > ok: [papas] =\> {  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "postgresql",  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "sysv",  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "running"  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > > > }
> > > > > 
> > > > > are those arguments to the the "service" module? how exaclty would I weave those into my task? I tried
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# enabled: false  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes
> > > > > 
> > > > > but that did not work
> > > > 
> > > > No, use my tasks verbatim.
> > > 
> > > hm ... pasting this verbatim to my plabook does not survice a --symtax-check
> > 
> > What is the diagnosis of the syntax check?
> 
> ######################################################################  
> play -v --limit [host] postgres\_install.yml --syntax-check  
> Using /path/to/ansible.cfg as config file  
> ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:  
> JSON: Expecting value: line 1 column 1 (char 0)
> 
> Syntax Error while loading YAML.  
> &nbsp;&nbsp;&nbsp;did not find expected key
> 
> The error appears to be in '/home/gwagner/repos/ansible/step/step\_server\_env/postgres\_install\_test\_external.yml': line 130, column 4, but may  
> be elsewhere in the file depending on the exact syntax problem.
> 
> The offending line appears to be:  
> &nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:  
> &nbsp;&nbsp;&nbsp;&nbsp;^ here  
> ######################################################################

Could be an indentation problem.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 15, 2021, 2:16pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/9 "2021-10-15T14:16:36Z")

</div>

I am still stuck with this:

theses 3 TASKS should take care of checking the status of postgresql.server and (if running) stop it

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 15, 2021, 2:22pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/10 "2021-10-15T14:22:28Z")

</div>

> > > > > > > > I am trying to install postgresql via a playbook and want to make sure the service is not running prior installation on the target (rhel7/8 & debian10) machines.
> > > > > > > > 
> > > > > > > > However my task below
> > > > > > > > 
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; - name: stop postgresql.service from systemd if running  
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; service:  
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; name: postgresql.service  
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; state: stopped  
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; # enabled: false  
> > > > > > > > &nbsp;&nbsp;&nbsp;&nbsp; become: yes
> > > > > > > > 
> > > > > > > > is failing ('Could not find the requested service postgresql.service: host')
> > > > > > > > 
> > > > > > > > so, I guess I have to build something that directs the target to ignore that and continues with the playbook. I just can't see anything
> > > > > > > 
> > > > > > > You can check the status of the postgresql service as follows:
> > > > > > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:
> > > > > > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
> > > > > > > 
> > > > > > > TASK [service\_facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > > > > ok: [papas]
> > > > > > > 
> > > > > > > TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > > > > > > ok: [papas] =\> {  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "postgresql",  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "sysv",  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "running"  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > > > > }
> > > > > > 
> > > > > > are those arguments to the the "service" module? how exaclty would I weave those into my task? I tried
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# enabled: false  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes
> > > > > > 
> > > > > > but that did not work
> > > > > 
> > > > > No, use my tasks verbatim.
> > > > 
> > > > hm ... pasting this verbatim to my plabook does not survice a --symtax-check
> > > 
> > > What is the diagnosis of the syntax check?
> > 
> > ######################################################################  
> > play -v --limit [host] postgres\_install.yml --syntax-check  
> > Using /path/to/ansible.cfg as config file  
> > ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:  
> > JSON: Expecting value: line 1 column 1 (char 0)
> > 
> > Syntax Error while loading YAML.  
> > &nbsp;&nbsp;&nbsp;did not find expected key
> > 
> > The error appears to be in '/home/gwagner/repos/ansible/step/step\_server\_env/postgres\_install\_test\_external.yml': line 130, column 4, but may  
> > be elsewhere in the file depending on the exact syntax problem.
> > 
> > The offending line appears to be:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:  
> > &nbsp;&nbsp;&nbsp;&nbsp;^ here  
> > ######################################################################
> > 
> > > > I figured out soome sort of sense-making output with adding 2 tasks based on what you posted.
> > > > 
> > > > ############################################################  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;- name: gather service facts  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;- name: return postgres running or not  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"  
> > > > ############################################################
> > > > 
> > > > however ... what do I do whith that output ('state: absent' in my test scenario)?
> > > > 
> > > > Maybe I need to put this to work as some sort of condition deciding whether to run a 'service' task disabling the postgresql.service or not?
> > > 
> > > Correct. For example:
> > > 
> > > when: ansible\_facts.services.postgresql.state == 'running'
> > 
> > ok, thx. I'll play with that
> 
> I am still stuck with this:
> 
> theses 3 TASKS should take care of checking the status of postgresql.server and (if running) stop it  
> ####################################################################################################  
> &nbsp;&nbsp;&nbsp;- name: gather service facts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service\_facts:
> 
> &nbsp;&nbsp;&nbsp;- name: return postgres running or not  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ ansible\_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
> 
> &nbsp;&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_facts.services.postgresql.state == 'running'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes  
> ####################################################################################################

"when" is a task level parameter and not a module parameter, so please shift it to the left on the same level as "become".

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 15, 2021, 5:05pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/11 "2021-10-15T17:05:41Z")

</div>

A simple solution is to 'alter' the failure condition:

&nbsp;&nbsp;&nbsp;- name: stop postgresql.service from systemd if running  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql.service  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: psql\_result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed\_when: psql\_result is failed and 'Could not find the  
requested service postgresql.service' not in psql\_result['msg']

Now it will only fail for reasons that are not 'the service is missing'

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 17, 2021, 3:27pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/12 "2021-10-17T15:27:23Z")

</div>

it took a bit of fiddeling with the indentation (not being transalted nicely to the website view of the mailing list) but finally this combination works (in terms of not producing any error on running the playbook).

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 17, 2021, 5:34pm UTC](https://forum.ansible.com/t/tell-service-module-to-continue-with-playbook-if-service-isnt-found/35878/13 "2021-10-17T17:34:21Z")

</div>

Testing service existence should be simpler, I think

&nbsp;&nbsp;&nbsp;&nbsp;- service\_facts:  
&nbsp;&nbsp;&nbsp;&nbsp;- service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: postgresql.service  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: stopped  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: psql\_result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "'postgresql.service' in services"
