1、使用ansible的playbook實現自動化安裝httpd
ansible管理主控與被管理機器已實現ssh免密登錄
主機清單中新增如下信息
[root@ansible ansible-playbook]# cat /etc/ansible/hosts
[httpd]
192.168.0.222
#查看httpd組內機器連接狀態
[root@ansible ~]# ansible httpd -m ping
192.168.0.222 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
配置ansible-playbook yml文件
[root@ansible ansible-playbook]# cat install-httpd.yml
- hosts: httpd
remote_user: root
tasks:
- name: install the latest version of Apache
yum:
name: httpd
state: latest
安裝httpd
[root@ansible ansible-playbook]# ansible-playbook install-httpd.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,內容分別爲其對應的主機名
[root@ansible-client ~]# cat /etc/httpd/conf.d/x.conf
<VirtualHost 192.168.0.112:80>
DirectoryIndex index.html
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 -Indexes +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
[root@ansible-client ~]# cat /etc/httpd/conf.d/y.conf
<VirtualHost 192.168.0.112:80>
DirectoryIndex index.html
ServerName www.Y.com
DocumentRoot "/web/vhosts/y"
ErrorLog "/var/log/httpd/y.err"
CustomLog /var/log/httpd/y.access combined
<Directory "/web/vhosts/y">
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
[root@ansible-client ~]# mkdir -p /web/vhosts/{x,y}
[root@ansible-client ~]# echo 'www.X.com' > /web/vhosts/x/index.html
[root@ansible-client ~]# echo 'www.Y.com' > /web/vhosts/y/index.html
[root@ansible-client]# httpd -t
Syntax OK
[root@ansible-client ~]# systemctl restart httpd
[root@ansible-client ~]# curl www.X.com
www.X.com
[root@ansible-client ~]# curl www.Y.com
www.Y.com