第十六周作业

1、使用ansible的playbook实现自动化安装httpd

主机:两台,一台ansible主控端 192.168.17.7/24,一台被控端 192.168.17.17/24

[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

[root@ansible ~]# yum install -y ansible  #安装Ansible

配置主机清单,并配置基于key验证的ssh连接

[root@ansible ~]# vim /etc/ansible/hosts

#添加以下内容

[websrvs]

192.168.17.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.17.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.17.17 (192.168.17.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.17.17's password:

Number of key(s) added: 1

Now try logging into the machine, with:  "ssh 'root@192.168.17.17'"

and check to make sure that only the key(s) you wanted were added.

建立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: root

  tasks:

    - name: Install httpd

      yum: name=httpd state=present

    - name: Install configure file

      copy: src=files/httpd.conf dest=/etc/httpd/conf/

    - name: start service

      service: name=httpd state=started enabled=yes

使用playbook文件进行httpd服务的安装,并测试

[root@ansible ~]# ansible-playbook httpd.yml

PLAY [websrvs] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************

ok: [192.168.17.17]

TASK [Install httpd] ****************************************************************************************************************************

changed: [192.168.17.17]

TASK [Install configure file] *******************************************************************************************************************

ok: [192.168.17.17]

TASK [start service] ****************************************************************************************************************************

changed: [192.168.17.17]

PLAY RECAP **************************************************************************************************************************************

192.168.17.17              : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@centos7 ~]# ss -ntl|grep 80    #被控端查看80端口已经打开

LISTEN    0      128        :::80                      :::*   

[root@centos7 ~]# ps -aux |grep httpd    #服务也已启动

root      7900  0.0  0.2 230408  5148 ?        Ss  19:48  0:00 /usr/sbin/httpd -DFOREGROUND

apache    7902  0.0  0.1 232492  3152 ?        S    19:48  0:00 /usr/sbin/httpd -DFOREGROUND

apache    7903  0.0  0.1 232492  3152 ?        S    19:48  0:00 /usr/sbin/httpd -DFOREGROUND

apache    7904  0.0  0.1 232492  3152 ?        S    19:48  0:00 /usr/sbin/httpd -DFOREGROUND

apache    7905  0.0  0.1 232492  3152 ?        S    19:48  0:00 /usr/sbin/httpd -DFOREGROUND

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,内容分别为其对应的主机名

#建立主机网页目录

[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

<VirtualHost *:80>

        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 None

                AllowOverride None

                Require all granted

        </Directory>

</VirtualHost>

<VirtualHost *:80>

        ServerName www.y.com

        DocumentRoot "/web/vhosts/y"

        ErrorLog "/var/log/httpd/www2.err"    #错误日志

        CustomLog "/var/log/httpd/y.access" combined    #访问日志

        <Directory "/web/vhosts/y">

                Options None

                AllowOverride None

                Require all granted

        </Directory>

</VirtualHost>

#重启httpd服务

[root@centos7 ~]# systemctl restart httpd

#创建本地解析

[root@centos7 ~]# vim /etc/hosts

127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4

::1        localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.17.17  www.x.com    #添加此行

192.168.17.17  www.y.com    #添加此行

#本地访问测试

[root@centos7 ~]# curl www.x.com

www.x.com

[root@centos7 ~]# curl www.y.com

www.y.com

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容