第十六周作业(ansible安装httpd)

1、使用ansible的playbook实现自动化安装httpd 主机:两台,一台ansible主控端 192.168.27.7/24,一台被控端 192.168.27.17/24
(1) 主控端安装ansible服务,使用阿里云的EPEL源,结合光盘yum源

[root@ansible ~]# cat /etc/yum.repos.d/base.repo [development] name=dvdbase repo baseurl=file:///mnt/cdrom/ enabled=1 gpgcheck=1 gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7[aliyunEpel] name=aliyun epel baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever#安装Ansible [root@ansible ~]# yum install -y ansible

(2) 配置主机清单,并配置基于key验证的ssh连接
[root@ansible ~]# vim /etc/ansible/hosts #添加以下内容 [websrvs] 192.168.27.17#基于Key验证 [root@ansible ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:qehCr9s+C0MGZ4U17a+9FyqIsKJMAoGuRYFJRFxSK4s root@ansible The key's randomart image is: +---[RSA 2048]----+ |=B+=+.| |+.oo...| |+.+. .| |o*o..| |Eo+.S| |++.. .. .| |o=oo...o . .| |* o=+ o o .| |+.o==o ..o| +----[SHA256]-----+ [root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.27.17 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.27.17 (192.168.27.17)' can't be established. ECDSA key fingerprint is SHA256:TX1biesR1B8Gsz8MuCTPDZKPX3i4E051OkhQK/C9kBI. ECDSA key fingerprint is MD5:83:8c:bb:e5:b9:a7:45:99:38:fc:5d:c8:89:cf:fb:5f. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.27.17's password: Number of key(s) added: 1Now try logging into the machine, with:"ssh 'root@192.168.27.17'" and check to make sure that only the key(s) you wanted were added.

(3) 建立playbook文件
[root@ansible ~]# ll files/httpd.conf#准备httpd的配置文件 -rw-r--r-- 1 root root 11753 Feb 25 19:43 files/httpd.conf[root@ansible ~]# vim httpd.yml ---- hosts: websrvs remote_user: roottasks: - name: Install httpd yum: name=httpd state=present - name: Install configure file copy: src=https://www.it610.com/article/files/httpd.conf dest=/etc/httpd/conf/ - name: start service service: name=httpd state=started enabled=yes

(4) 使用playbook文件进行httpd服务的安装,并测试
[root@ansible ~]# ansible-playbook httpd.yml PLAY [websrvs] **********************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************** ok: [192.168.27.17]TASK [Install httpd] **************************************************************************************************************************** changed: [192.168.27.17]TASK [Install configure file] ******************************************************************************************************************* ok: [192.168.27.17]TASK [start service] **************************************************************************************************************************** changed: [192.168.27.17]PLAY RECAP ************************************************************************************************************************************** 192.168.27.17: ok=4changed=2unreachable=0failed=0skipped=0rescued=0ignored=0 [root@centos7 ~]# ss -ntl|grep 80#被控端查看80端口已经打开 LISTEN0128:::80:::* [root@centos7 ~]# ps -aux |grep httpd#服务也已启动 root79000.00.2 2304085148 ?Ss19:480:00 /usr/sbin/httpd -DFOREGROUND apache79020.00.1 2324923152 ?S19:480:00 /usr/sbin/httpd -DFOREGROUND apache79030.00.1 2324923152 ?S19:480:00 /usr/sbin/httpd -DFOREGROUND apache79040.00.1 2324923152 ?S19:480:00 /usr/sbin/httpd -DFOREGROUND apache79050.00.1 2324923152 ?S19:480:00 /usr/sbin/httpd -DFOREGROUND apache79060.00.1 2324923152 ?S19:480:00 /usr/sbin/httpd -DFOREGROUND root79720.00.0 112708976 pts/0S+19:510:00 grep --color=auto httpd

2、建立httpd服务器,要求提供两个基于名称的虚拟主机 【第十六周作业(ansible安装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,内容分别为其对应的主机名
#建立主机网页目录 [root@centos7 ~]# mkdir -p /web/vhosts/{x,y} #授权apache用户访问 [root@centos7 ~]# chown -R root:apache /web#建立各虚拟主机的主页文件index.html [root@centos7 ~]# echo www.x.com > /web/vhosts/x/index.html [root@centos7 ~]# echo www.y.com > /web/vhosts/y/index.html#建立虚拟主机配置 [root@centos7 ~]# vim /etc/httpd/conf.d/vhosts.conf ServerName www.x.com DocumentRoot "/web/vhosts/x" ErrorLog "/var/log/httpd/x.err"#错误日志 CustomLog "/var/log/httpd/x.access" combined#访问日志 Options None AllowOverride None Require all granted ServerName www.y.com DocumentRoot "/web/vhosts/y" ErrorLog "/var/log/httpd/www2.err"#错误日志 CustomLog "/var/log/httpd/y.access" combined#访问日志 Options None AllowOverride None Require all granted #重启httpd服务 [root@centos7 ~]# systemctl restart httpd#创建本地解析 [root@centos7 ~]# vim /etc/hosts 127.0.0.1localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.27.17www.x.com#添加此行 192.168.27.17www.y.com#添加此行#本地访问测试 [root@centos7 ~]# curl www.x.com www.x.com [root@centos7 ~]# curl www.y.com www.y.com

    推荐阅读