在两台受控主机上安装httpd软件,复制首页文件,并且启动httpd软件:
第一步:在/etc/ansible/hosts文件末尾添加以下三行:
[webservers]
192.168.200.100 ansible_user=root
192.168.200.200 ansible_user=root
第二部:在当前目录下准备配置文件和网页首页文件:httpd.conf.j2和index.html
httpd.conf.j2:从httpd服务器的/etc/httpd/conf/httpd.conf复制,并且更改listen端口80为8080
index.html的内容如下:
[root@proxy playbook]# cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Page</title>
</head>
<body>
<div><h1>Hello Ansible-playbook </h1></div>
</body>
</html>
第三步:编写以下httpd.yaml文件,安装apache软件,并且配置这个网页的index.html,然后启动这个软件,开启防火墙并且放行tcp 8080端口。
[root@proxy playbook]# cat httpd.yaml
- hosts: webservers
remote_user: root
tasks:
- name: Installed Http Server
yum: name=httpd state=present
- name: Cofigure Http Server
copy: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
tags: confhttp
notify: Restart Http Server
- name: Configure Httpd Server WebSite
copy: src=./index.html dest=/var/www/html/index.html
- name: Service Httpd Server
service: name=httpd state=started enabled=true
- name: Service Firewalld Server
service: name=firewalld state=started
- name: Configure Firewalld Server
firewalld: zone=public port=8080/tcp permanent=yes immediate=yes state=enabled
handlers:
- name: Restart Http Server
service: name=httpd state=restarted
执行:ansible-playbook httpd.yaml后
用同一子网内浏览器去访问:192.168.200.100:8080和192.168.200.200的结果如下:
关闭并且卸载受控主机上的Apache服务器软件:
[root@proxy httpd]# cat uninstall_httpd.yaml
- hosts: webservers
remote_user: root
tasks:
- name: Stop Httpd Server
service: name=httpd state=stopped
- name: Uninstall Httpd Server
yum: name=httpd state=absent
执行:ansible-playbook uninstall_httpd.yaml
执行过程: