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

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

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:

  - service_facts:

  - debug:
      msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

TASK [service_facts] ***************************************************************************************************************************************
ok: [papas]

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

Regards
         Racke

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

  - name: stop postgresql.service from systemd if running
    service:
      name: postgresql
        service_facts:
        debug:
          msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
      state: stopped
      # enabled: false
    become: yes

but that did not work

No, use my tasks verbatim.

Regards
           Racke

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.

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:

    - service_facts:

    - debug:
        msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

TASK [service_facts] ***************************************************************************************************************************************
ok: [papas]

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

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

    - name: stop postgresql.service from systemd if running
      service:
        name: postgresql
          service_facts:
          debug:
            msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
        state: stopped
        # enabled: false
      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.

############################################################
   - name: gather service facts
     service_facts:

   - name: return postgres running or not
     debug:
       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
         Racke

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:

     - service_facts:

     - debug:
         msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

TASK [service_facts] ***************************************************************************************************************************************
ok: [papas]

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

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

     - name: stop postgresql.service from systemd if running
       service:
         name: postgresql
           service_facts:
           debug:
             msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
         state: stopped
         # enabled: false
       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.
   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:
    - service_facts:
    ^ here
######################################################################

Could be an indentation problem.

Regards
        Racke

I am still stuck with this:

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

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:

     - service_facts:

     - debug:
         msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

TASK [service_facts] ***************************************************************************************************************************************
ok: [papas]

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

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

     - name: stop postgresql.service from systemd if running
       service:
         name: postgresql
           service_facts:
           debug:
             msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"
         state: stopped
         # enabled: false
       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.
   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:
    - service_facts:
    ^ here
######################################################################

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

############################################################
    - name: gather service facts
      service_facts:

    - name: return postgres running or not
      debug:
        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
####################################################################################################
   - name: gather service facts
     service_facts:

   - name: return postgres running or not
     debug:
       msg: "{{ ansible_facts.services.postgresql | default({ 'state' : 'absent'}) }}"

   - name: stop postgresql.service from systemd if running
     service:
       name: postgresql
       state: stopped
       when: ansible_facts.services.postgresql.state == 'running'
     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
          Racke

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

   - name: stop postgresql.service from systemd if running
      service:
        name: postgresql.service
        state: stopped
      become: yes
      register: psql_result
      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'

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).

Testing service existence should be simpler, I think

    - service_facts:
    - service:
        name: postgresql.service
        state: stopped
      become: true
      register: psql_result
      when: "'postgresql.service' in services"