vjwilson
vjwilson

Reputation: 833

Ansible: Unable to start the httpd service after installing its package

I am testing playbooks

The following playbook could install apache and start the service:

- hosts: apacheweb
  user: ansibleuser
  become: yes
  gather_facts: no
  tasks:
  - name: Install the apache web server
    yum: pkg=httpd state=latest
    notify: Start HTTPD
  handlers:
  - name: Start HTTPD
    systemd: name=httpd state=started enabled=yes

But not the following one, as it does install the httpd package but doesn't start the service and enable it.

- hosts: apacheweb
  user: ansibleuser
  become: yes
  gather_facts: no
  tasks:
  - name: Install the apache web server
    yum: pkg=httpd state=latest
    notify: Start HTTPD
  - name: verify that the web service is running
    command: systemctl status httpd
    register: result
  - debug: var=result
  handlers:
  - name: Start HTTPD
    systemd: name=httpd state=started enabled=yes

Error output:

PLAY [apacheweb] ******************************************************************************************************************

TASK [Install the apache web server] **********************************************************************************************
changed: [host.learn.com]

TASK [verify that the web service is running] *************************************************************************************
fatal: [host.learn.com]: FAILED! => {"changed": true, "cmd": ["systemctl", "status", "httpd"], "delta": "0:00:00.032030", "end": "2020-04-14 17:59:18.295428", "msg": "non-zero return code", "rc": 3, "start": "2020-04-14 17:59:18.263398", "stderr": "", "stderr_lines": [], "stdout": "● httpd.service - The Apache HTTP Server\n   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)\n   Active: inactive (dead)\n     Docs: man:httpd(8)\n           man:apachectl(8)\n\nApr 14 14:50:31 host.learn.com systemd[1]: Starting The Apache HTTP Server...\nApr 14 14:50:31 host.learn.com systemd[1]: Started The Apache HTTP Server.\nApr 14 14:53:45 host.learn.com systemd[1]: Stopping The Apache HTTP Server...\nApr 14 14:53:46 host.learn.com systemd[1]: Stopped The Apache HTTP Server.\nApr 14 17:56:51 host.learn.com systemd[1]: Starting The Apache HTTP Server...\nApr 14 17:56:51 host.learn.com systemd[1]: Started The Apache HTTP Server.\nApr 14 17:57:32 host.learn.com systemd[1]: Stopping The Apache HTTP Server...\nApr 14 17:57:33 host.learn.com systemd[1]: Stopped The Apache HTTP Server.", "stdout_lines": ["● httpd.service - The Apache HTTP Server", "   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)", "   Active: inactive (dead)", "     Docs: man:httpd(8)", "           man:apachectl(8)", "", "Apr 14 14:50:31 host.learn.com systemd[1]: Starting The Apache HTTP Server...", "Apr 14 14:50:31 host.learn.com systemd[1]: Started The Apache HTTP Server.", "Apr 14 14:53:45 host.learn.com systemd[1]: Stopping The Apache HTTP Server...", "Apr 14 14:53:46 host.learn.com systemd[1]: Stopped The Apache HTTP Server.", "Apr 14 17:56:51 host.learn.com systemd[1]: Starting The Apache HTTP Server...", "Apr 14 17:56:51 host.learn.com systemd[1]: Started The Apache HTTP Server.", "Apr 14 17:57:32 host.learn.com systemd[1]: Stopping The Apache HTTP Server...", "Apr 14 17:57:33 host.learn.com systemd[1]: Stopped The Apache HTTP Server."]}

RUNNING HANDLER [Start HTTPD] *****************************************************************************************************

PLAY RECAP ************************************************************************************************************************
host.learn.com : ok=1    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

What might be wrong in the second playbook I created?.

Upvotes: 2

Views: 1844

Answers (1)

franklinsijo
franklinsijo

Reputation: 18300

The handler Start HTTPD to start the service did not run since the task verify that the web service is running failed.

On each host, Handlers are run only once after all the tasks in a play are complete.

In your playbook, there are three tasks. Eventhough the handler was notified by the first task Install the apache web server, ansible runs the handler only after completing all the three tasks in that play.

When a task fails on a host, handlers which were previously notified will not be run on that host.

And the failure of the second task stopped the handler from running on that host, thus the service was not started.

If you wish to run the handler irrespective of task failure, you can set force_handlers: True in your play.

Read more: Handlers, Handler Failures

Upvotes: 1

Related Questions