ansible - Ansible:安装包后无法启动 httpd 服务
问题描述
我正在测试剧本
以下 playbook 可以安装apache
和启动服务:
- 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
但不是下一个,因为它会安装httpd
软件包但不会启动服务并启用它。
- 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
错误输出:
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
我创建的第二本剧本可能有什么问题?
解决方案
由于任务失败,启动服务的处理程序Start HTTPD
没有运行。verify that the web service is running
在每台主机上,处理程序仅在播放中的所有任务完成后运行一次。
在你的剧本中,有三个任务。尽管第一个任务通知了处理程序Install the apache web server
,但 ansible 仅在完成该播放中的所有三个任务后才运行处理程序。
当主机上的任务失败时,先前通知的处理程序将不会在该主机上运行。
第二个任务的失败使处理程序无法在该主机上运行,因此服务没有启动。
如果您希望在任务失败的情况下运行处理程序,您可以force_handlers: True
在您的游戏中设置。
推荐阅读
- java - 如何在 Java RS json 响应中为简单列表类型设置客户名称
- php - 如何修复我的 eval()'d 代码行:1 个问题
- python - 我想使用熊猫保留那些具有空数据输出的行
- cmake - swig 从接口头文件生成的包装器 java 文件为空
- c - 从c中的堆栈获取参数
- android - Android房间一次加载多个数据如何知道所有请求都完成了
- javascript - Xamarin Android - 使用 c# 调用 JavaScript
- spreadsheet - 强制 Google 表格在新行上不使用格式
- apache-spark - Spark数据帧上的多个连接重复记录
- aws-lambda - DynamoDB DocumentClient 将字符串集 (SS) 属性作为对象返回