1、使用ansible的playbook实现自动化安装httpd
安装ansible服务:
[root@Ansiable ~]# yum install -y ansible
配置ansible跟其他主机之间基于key验证
[root@Ansiable ~]# 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:bAHlfv4EF2vxanwwjlT77C1E0XqR7JRsygOZtTOhSUU root@Ansiable
The key's randomart image is:
+---[RSA 2048]----+
| ... .*Eo.o|
| o .+o +O.|
| o o*+=o.|
| o . . X=..|
| S + B.o. |
| . + B *. |
| o *.+ |
| + o.. |
| . ...|
+----[SHA256]-----+
[root@Ansiable ~]# ssh-copy-id 192.168.10.4
[root@Ansiable ~]# ssh-copy-id 192.168.10.5
配置ansible主机清单,将需要管控的主机地址添加进去:
[root@Ansiable ~]# vim /etc/ansible/hosts
[webser]
192.168.10.4
192.168.10.5
编写playbook:
[root@Ansiable playbook]# vim http_install.yml
- hosts: all
tasks:
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=started
测试playbook:
[root@Ansiable playbook]# ansible-playbook -C http_install.yml
执行playbook:
[root@Ansiable playbook]# ansible-playbook http_install.yml
客户机验证:
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,内容分别为其对应的主机名
测试机修改hosts文件,进行测试: