一、使用ansible的playbook实现自动化安装httpd。
1、使用yum方式安装ansible,需要提前配置好epel源。
[root@centos88 ~]#yum install ansible
[root@centos88 ~]#ansible --version
ansible 2.9.16
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules‘, ‘/usr/share/ansible/plugins/modules‘]
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
2、配置ansible主控端能基于密钥认证的方式联系各个被管理节点。
[root@centos88 ~]#ssh-keygen
[root@centos88 ~]#ssh-copy-id 10.0.0.8
[root@centos88 ~]#ssh-copy-id 10.0.0.68
3、编辑被管理节点的主机清单文件。
[root@centos88 ~]#vim /etc/ansible/hosts
[websrv]
10.0.0.8
10.0.0.68
4、检测被管理节点是否可正常连接。
[root@centos88 ~]#ansible websrv -m ping
10.0.0.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
10.0.0.68 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
5、编辑安装httpd的yml文件,此文件对缩进有严格要求。
[root@centos88 ~]#vim httpd.yml
---
- hosts: websrv
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: start service
service: name=httpd state=started enabled=yes
6、查看一下,被管理节点还未安装httpd。
[root@centos08 ~]#systemctl status httpd
Unit httpd.service could not be found.
[root@centos68 ~]#systemctl status httpd
Unit httpd.service could not be found.
6、检测并执行playbook,执行前可先加上--check检测一下。
[root@centos88 ~]#ansible-playbook httpd.yml --check
[root@centos88 ~]#ansible-playbook httpd.yml
PLAY [websrv] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [10.0.0.8]
ok: [10.0.0.68]
TASK [install httpd] *****************************************************************************************
changed: [10.0.0.68]
changed: [10.0.0.8]
TASK [start service] *****************************************************************************************
changed: [10.0.0.68]
changed: [10.0.0.8]
PLAY RECAP
10.0.0.68 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.8 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7、验证被管理端安装是否成功。
[root@centos08 ~]#systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-07 20:46:23 CST; 1min 50s ago
Docs: man:httpd.service(8)
Main PID: 4955 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 12337)
Memory: 35.7M
CGroup: /system.slice/httpd.service
├─4955 /usr/sbin/httpd -DFOREGROUND
├─4956 /usr/sbin/httpd -DFOREGROUND
├─4957 /usr/sbin/httpd -DFOREGROUND
├─4958 /usr/sbin/httpd -DFOREGROUND
└─4959 /usr/sbin/httpd -DFOREGROUND
Jan 07 20:46:22 centos08 systemd[1]: Starting The Apache HTTP Server...
Jan 07 20:46:23 centos08 httpd[4955]: AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using fe80::50ad:66ce:2b87:6bd7. Set the ‘ServerName‘>
Jan 07 20:46:23 centos08 systemd[1]: Started The Apache HTTP Server.
Jan 07 20:46:23 centos08 httpd[4955]: Server configured, listening on: port 80
[root@centos83 ~]#systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-07 20:46:23 CST; 3min 10s ago
Docs: man:httpd.service(8)
Main PID: 5332 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11219)
Memory: 34.0M
CGroup: /system.slice/httpd.service
├─5332 /usr/sbin/httpd -DFOREGROUND
├─5333 /usr/sbin/httpd -DFOREGROUND
├─5334 /usr/sbin/httpd -DFOREGROUND
├─5335 /usr/sbin/httpd -DFOREGROUND
└─5336 /usr/sbin/httpd -DFOREGROUND
Jan 07 20:46:23 centos83 systemd[1]: Starting The Apache HTTP Server...
Jan 07 20:46:23 centos83 httpd[5332]: AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using fe80::20c:29ff:feb6:4d1c. Set the ‘ServerName‘ >
Jan 07 20:46:23 centos83 systemd[1]: Started The Apache HTTP Server.
Jan 07 20:46:23 centos83 httpd[5332]: Server configured, listening on: port 80
8、测试成果。
[root@centos08 ~]#echo "<h1>这是一个ansible自动安装的apache服务器</h1>" > /var/www/html/index.html
[root@centos08 ~]#curl 127.0.0.1
<h1>这是一个ansible自动安装的apache服务器</h1>
二、建立httpd服务器,要求提供两个基于名称的虚拟主机:
1、yum方式安装httpd软件包。
[root@centos68 ~]#yum -y install httpd
2、创建两个虚拟需要的目录。
[root@centos68 ~]#mkdir -p /web/vhosts/{x,y}
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为
/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
? 编辑www.x.com的配置文件。
<VirtualHost *:80>
ServerName www.x.com
DocumentRoot "/web/vhosts/x"
<Directory "/web/vhosts/x">
Options None
AllowOverride None
Require all granted
</Directory>
CustomLog "/var/log/httpd/x.access" combined
ErrorLog "/var/log/httpd/x.err"
</VirtualHost>
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
? 编辑www.y.com的配置文件。
<VirtualHost *:80>
ServerName www.y.com
DocumentRoot "/web/vhosts/y"
<Directory "/web/vhosts/y">
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/y.access" combined
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名。
[root@centos68 ~]#echo www.x.com>/web/vhosts/x/index.html
[root@centos68 ~]#echo www.y.com>/web/vhosts/y/index.html
3、重新读取配置文件。
[root@centos68 ~]#systemctl reload httpd
4、修改/etc/hosts文件,模拟dns解析域名。
[root@centos68 ~]#vim /etc/hosts
10.0.0.68 www.x.com
10.0.0.68 www.y.com
5、测试成果。
[root@centos68 /web/vhosts/x]#curl www.x.com
www.x.com
[root@centos68 /web/vhosts/x]#curl www.y.com
www.x.com