1、使用ansible的playbook实现自动化安装httpd
(1)配置SSH认证连接
[root@centos01 ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:WlyQgX3QFX+6FwZBcTPQcUGw8ShZiIPJowuedquYSZw root@centos01
The key's randomart image is:
+---[RSA 2048]----+
| .o*=..=X*B+|
| .=o+o.o.B.+|
| . ..oo +...|
| . . . . . .o |
| . o . S .o |
|. .+ o o ...|
| E. . o . .|
|. + . . |
| + .. |
+----[SHA256]-----+
[root@centos01 ~]#ssh-copy-id 192.168.7.72
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.7.72's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.7.72'"
and check to make sure that only the key(s) you wanted were added.
[root@centos01 ~]#ssh 192.168.7.72
Last login: Tue Jun 16 11:43:43 2020 from 192.168.0.1
(2)yaml文件编写
- hosts: webservers #需先在配置文件中加入72,73两台机器
remote_user: root
tasks:
- name: Install httpd
yum: name=httpd state=present
- name: Install configure file
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/
notify: restart httpd
- name: ensure apache is running
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd
service: name=httpd state=restarted
(3)运行测试
[root@centos01 ~]#ansible-playbook httpd.yml -C
PLAY [webservers] *************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************
ok: [192.168.7.72]
ok: [192.168.7.73]
TASK [Install httpd] **********************************************************************************************************************************************************************
changed: [192.168.7.72]
changed: [192.168.7.73]
TASK [Install configure file] *************************************************************************************************************************************************************
changed: [192.168.7.73]
changed: [192.168.7.72]
TASK [ensure apache is running] ***********************************************************************************************************************************************************
changed: [192.168.7.73]
changed: [192.168.7.72]
RUNNING HANDLER [restart httpd] ***********************************************************************************************************************************************************
changed: [192.168.7.73]
changed: [192.168.7.72]
PLAY RECAP ********************************************************************************************************************************************************************************
192.168.7.72 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.7.73 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(4)测试通过后再进行安装并验证
[root@centos01 ~]#ansible-playbook httpd.yml
PLAY [webservers] *************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************
ok: [192.168.7.73]
ok: [192.168.7.72]
TASK [Install httpd] **********************************************************************************************************************************************************************
changed: [192.168.7.73]
changed: [192.168.7.72]
TASK [Install configure file] *************************************************************************************************************************************************************
ok: [192.168.7.72]
ok: [192.168.7.73]
TASK [ensure apache is running] ***********************************************************************************************************************************************************
changed: [192.168.7.73]
changed: [192.168.7.72]
PLAY RECAP ********************************************************************************************************************************************************************************
192.168.7.72 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.7.73 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证,以72为例
[root@centos02 ~]#ss -ntl #查看端口
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@centos02 ~]#systemctl status httpd.service #查看运行状态
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-06-16 15:50:09 CST; 1min 11s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 4372 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─4372 /usr/sbin/httpd -DFOREGROUND
├─4377 /usr/sbin/httpd -DFOREGROUND
├─4378 /usr/sbin/httpd -DFOREGROUND
├─4379 /usr/sbin/httpd -DFOREGROUND
├─4380 /usr/sbin/httpd -DFOREGROUND
└─4381 /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/y.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
1、建立主界面目录
[root@centos01 ~]#mkdir /web/vhosts/{x,y} -p
[root@centos01 ~]#ll /web/vhosts/
total 0
drwxr-xr-x 2 root root 6 Jun 16 19:00 x
drwxr-xr-x 2 root root 6 Jun 16 19:00 y
2、配置网页显示
[root@centos01 ~]#vim /web/vhosts/x/index.html
www.X.com
[root@centos01 ~]#vim /web/vhosts/y/index.html
www.Y.com
3、添加主机对应域名
[root@centos01 ~]#vim /etc/hosts
192.168.7.71 www.X.com www.Y.com
4、配置文件
[root@centos01 ~]#vim /etc/httpd/conf.d/test1.conf
<virtualhost *:80>
documentroot /web/vhosts/x
servername www.X.com
ErrorLog "/var/log/httpd/x.err"
CustomLog "/var/log/httpd/x.access" combined
<Directory "/web/vhosts/x">
Require all granted
</Directory>
</virtualhost>
<virtualhost *:80>
documentroot /web/vhosts/y
servername www.Y.com
ErrorLog "/var/log/httpd/x.err"
CustomLog "/var/log/httpd/x.access" combined
<Directory "/web/vhosts/y">
Require all granted
</Directory>
</virtualhost>
5、重新启动httpd并验证
[root@centos01 ~]#systemctl restart httpd
[root@centos01 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
查看访问结果
[root@centos01 ~]#curl www.X.com
www.X.com
[root@centos01 ~]#curl www.Y.com
www.Y.com
查看日志目录
[root@centos01 ~]#ll /var/log/httpd/
total 12
-rw-r--r-- 1 root root 3474 Jun 16 18:52 access_log
-rw-r--r-- 1 root root 3192 Jun 17 17:28 error_log
-rw-r--r-- 1 root root 176 Jun 17 17:24 x.access
-rw-r--r-- 1 root root 0 Jun 17 17:24 x.err
-rw-r--r-- 1 root root 0 Jun 17 17:28 y.access
-rw-r--r-- 1 root root 0 Jun 17 17:28 y.err