1、使用ansible的playbook實(shí)現(xiàn)自動化安裝httpd
安裝ansible服務(wù):
[root@Ansiable ~]# yum install -y ansible
配置ansible跟其他主機(jī)之間基于key驗(yàn)證
[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主機(jī)清單规揪,將需要管控的主機(jī)地址添加進(jìn)去:
[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
執(zhí)行playbook:
[root@Ansiable playbook]# ansible-playbook http_install.yml
客戶機(jī)驗(yàn)證:
2桥氏、建立httpd服務(wù)器,要求提供兩個基于名稱的虛擬主機(jī):
(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)為兩個虛擬主機(jī)建立各自的主頁文件index.html堕伪,內(nèi)容分別為其對應(yīng)的主機(jī)名
測試機(jī)修改hosts文件,進(jìn)行測試: