ansible-playbook练习2

在两台受控主机上安装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

执行过程:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值