马哥教育N48-第十六周作业

1、使用ansible的playbook实现自动化安装httpd
一、本实验准备2台主机
192.168.37.7 ansible控端
192.168.37.17 http服务器

二、安装ansible程序到控端
直接使用epel源的yum安装方式
yum install ansible -y

三、准备ssh的key认证登录
生成key
ssh-keygen

传送key到远程主机
ssh-copy-id 192.168.37.17

[root@centos7 ~]#ssh 192.168.37.17
Last login: Sat Dec 26 07:27:17 2020 from 192.168.37.7
书中自有黄金屋,书中自有颜如玉

四、准备yum方式安装的yml文件和主机文件

cat installhttpd.yml

  • hosts: webservers
    remote_user: root
    gather_facts: no
    tasks:
    • name: yum install httpd
      yum: name=httpd

    • name: start httpd service
      shell: /usr/bin/systemctl start httpd
      tags: start

    • name: enable httpd service
      shell: /usr/bin/systemctl enable httpd

    • name: create test webfiles
      shell: /usr/bin/echo “this is test web file…” > /var/www/html/index.html

cat /etc/ansible/hosts
[webservers]
192.168.37.17

五、执行ansible-play指令

执行结果:
ansible-playbook installhttpd.yml

PLAY [webservers] *****************************************************

TASK [yum install httpd] ***********************************************
changed: [192.168.37.17 ]

TASK [start httpd service] **********************************************
changed: [192.168.37.17 ]

TASK [enable httpd service] *********************************************
changed: [192.168.37.17 ]

TASK [create test webfiles] *********************************************
changed: [192.168.37.17 ]

PLAY RECAP **************************************************************
192.168.37.17 : ok=4 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

六、测试效果
[root@centos7 ~]#cat /var/www/html/index.html
this is test web file…
2、建立httpd服务器,要求提供两个基于名称的虚拟主机:

(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为
/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

cat /etc/httpd/conf.d/x.conf
<VirtualHost 192.168.37.17:80>
DirectoryIndex index.html
ServerName www.X.com
DocumentRoot “/web/vhosts/x”
ErrorLog “/var/log/httpd/x.err”
CustomLog /var/log/httpd/x.access combined
<Directory “/web/vhosts/x”>
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted

[root@ansible-client ~]# cat /etc/httpd/conf.d/y.conf
<VirtualHost 192.168.37.17:80>
DirectoryIndex index.html
ServerName www.Y.com
DocumentRoot “/web/vhosts/y”
ErrorLog “/var/log/httpd/y.err”
CustomLog /var/log/httpd/y.access combined
<Directory “/web/vhosts/y”>
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted

[root@ansible-client ~]# mkdir -p /web/vhosts/{x,y}
[root@ansible-client ~]# echo ‘www.X.com’ > /web/vhosts/x/index.html
[root@ansible-client ~]# echo ‘www.Y.com’ > /web/vhosts/y/index.html

[root@ansible-client]# httpd -t
Syntax OK

[root@ansible-client ~]# systemctl restart httpd

[root@ansible-client ~]# curl www.X.com
www.X.com

[root@ansible-client ~]# curl www.Y.com
www.Y.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值