2

I am trying the following playbook. But even after getting the error from handler, the rescue section is not working.

handlers:
 - name: port status
   shell: netstat -nltp | grep {{ app1_port }}
   register: port
   listen: port_status
 - name: display port status
   debug: var=port.stdout_lines
   listen: port_status
tasks:
 - name: Reload service if checks fail
   block:
    - name: Check process status
      shell: ps -aux | grep {{ app1 }} | grep -v grep
      notify: port status
   rescue:
    - name: fetching proc ids
      shell: ps -aux | grep {{ app2 }} | grep -v grep | awk '{print $2}'
      register: result
      ignore_errors: True
    - name: Reloading config
      shell: "kill -HUP {{ item }}"
       with_items:
        - "{{ result.stdout_lines }}" 
      notify: port_status

Bellow is the output I'm getting while running.

TASK [Check service status] ********************************************************************************************************* changed: [localhost]

RUNNING HANDLER [port status] **************************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": true, "cmd": "netstat -nltp | grep 3306", "delta": "0:00:00.017951", "end": "2019-03-13 22:04:41.024950", "msg": "non-zero return code", "rc": 1, "start": "2019-03-13 22:04:41.006999", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

NO MORE HOSTS LEFT ***************************************************************************************************************** to retry, use: --limit @/home/sachin/ansible.retry

PLAY RECAP ***************************************************************************************************************** localhost : ok=1 changed=1 unreachable=0 failed=1

Sachin C
  • 21
  • 3
  • please, could you provide the error? – c4f4t0r Mar 13 '19 at 14:44
  • This looks extraordinarily complicated. What is the purpose of everything in this playbook? It is probably going to need to be scrapped and rewritten, so it is necessary to understand what it is supposed to do – Michael Hampton Mar 13 '19 at 17:07
  • Why bother with these at all? You can just have the `service` set the `state` to `started` and ansible will start the service if it is not running, but do nothing if it is already running. BTW, that handler doesn't work, which is the immediate cause of your error. – Michael Hampton Mar 13 '19 at 17:16
  • @MichaelHampton Actually I wanted to satisfy both conditions. Service module I used for testing, mostly I will be using custom command with the shell module. – Sachin C Mar 13 '19 at 17:19
  • OK, but then could you please edit your question to reflect what you are really trying to do? – Michael Hampton Mar 13 '19 at 17:26
  • @MichaelHampton basically I wanted to run some tasks which can act as functions in the playbook. I thought handlers will do the job. Is there any better way to achieve this? – Sachin C Mar 14 '19 at 09:49
  • You still haven't really explained what you mean to accomplish. – Michael Hampton Mar 14 '19 at 15:53
  • @MichaelHampton I've updated the question – Sachin C Mar 15 '19 at 11:46

0 Answers0