实现目的:使用 Ansibe playbook 检查 web 服务器状态,模拟 httpd 没有启动,启动 httpd,使得 web 服务器恢复正常。
文件说明:
1、pre.yml:安装 httpd 服务,配置 httpd 服务
2、fix_web.yml:检测出 httpd 服务出现问题,并启动 httpd 服务,使得 web 服务器恢复正常。
系统:el7
主机名:web
IP:192.168.149.6
Ansible控制节点和被管节点均为web服务器
[root@web ~]# uname -a
Linux web 3.10.0-1160.36.2.el7.x86_64 #1 SMP x86_64 x86_64 x86_64 GNU/Linux
[root@web ~]# hostname
web
[root@web ~]# ip a|grep inet
inet 192.168.149.6/24 brd 192.168.149.255 scope global ens33
[root@servera ~]# ansible web -m ping -o
192.168.149.6 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@web ~]# ansible-playbook pre.yml
[root@web ~]# curl web
web server is running by @iamjacker
[root@web ~]# #模拟 httpd 没有启动
[root@web ~]# systemctl stop httpd
[root@web ~]# ansible-playbook fix_web.yml
PLAY [web] **********************************************************************************************************************
TASK [Check that a page returns a status 200 and fix if the word jack is not in the page contents] ****************************** fatal: [192.168.149.6]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "content": "", "elapsed": 0, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://192.168.149.6"} ...ignoring
TASK [debug] ******************************************************************************************************************** ok: [192.168.149.6] => { "res": { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "content": "", "elapsed": 0, "failed": true, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://192.168.149.6" } }
TASK [debug] ******************************************************************************************************************** ok: [192.168.149.6] => { "msg": "Status code was -1 and not [200]" }
TASK [start web service if web service is not started] ************************************************************************** changed: [192.168.149.6]
PLAY RECAP ********************************************************************************************************************** 192.168.149.6 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@web ~]# curl web
web server is running by @iamjacker
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。