使用ansible配置HTTP服務

一、使用ansible的playbook實現自動化安裝httpd

本次實驗須要準備兩個虛擬機,一臺安裝ansible,另外一臺安裝httpdhtml

配置ansible服務器

# 安裝ansible
rpm -q epel-release || yum install -y epel-release
yum install -y ansible

# 生成SSH密鑰而且發送到HTTP服務器
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/null
ssh-copy-id 192.168.0.51
ssh root@192.168.0.51 "ip a"

# 編輯playbook
cat > install_httpd.yml << EOF
# install httpd
---
- hosts: WebServer
  remote_user: root
  gather_facts: no
  tasks:
    - name: install httpd
      yum: name=httpd
    - name: start httpd
      service: name=httpd state=started enabled=yes
    - name: edit homepage
      shell: /usr/bin/echo "Test OK!" > /var/www/html/index.html
EOF

# 編輯/etc/ansible/hosts 
cat > /etc/ansible/hosts << EOF
[WebServer]
192.168.0.51
EOF

# 檢查playbook的語法
ansible-playbook -C install_httpd.yml

# 運行playbook
ansible-playbook install_httpd.yml

# 驗證http服務的運行狀態
curl 192.168.0.51

二、創建httpd服務器,要求提供兩個基於名稱的虛擬主機:

(1)www.X.com,頁面文件目錄爲/web/vhosts/x;錯誤日誌爲 /var/log/httpd/x.err,訪問日誌爲/var/log/httpd/x.accessweb

(2)www.Y.com,頁面文件目錄爲/web/vhosts/y;錯誤日誌爲 /var/log/httpd/www2.err,訪問日誌爲/var/log/httpd/y.accessshell

(3)爲兩個虛擬主機創建各自的主頁文件index.html,內容分別爲其對應的主機名服務器

本次實驗使用的是前一個實驗的兩個虛擬機ssh

# 編輯配置文件
cat > x.conf << EOF
<VirtualHost *:80>
    ServerName www.X.com
    DocumentRoot "/web/vhosts/x"
    CustomLog "/var/log/httpd/x.access" combined
    ErrorLog "/var/log/httpd/x.err"
    <Directory "/web/vhosts/x">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
EOF

cat > y.conf << EOF
<VirtualHost *:80>
    ServerName www.Y.com
    DocumentRoot "/web/vhosts/y"
    CustomLog "/var/log/httpd/y.access" combined
    ErrorLog "/var/log/httpd/www2.err"
    <Directory "/web/vhosts/y">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
EOF

# 編輯主頁文件
echo "www.X.com" > Xindex.html
echo "www.Y.com" > Yindex.html

# 編輯playbook
cat > vhost_conf.yml << EOF
---
- hosts: WebServer
  remote_user: root
  tasks:
    - name: mkdir virtualhost documentroot directory
      shell: mkdir -p /web/vhosts/{x,y}
    - name: copy x.conf to remotehost
      copy: src=/root/x.conf dest=/etc/httpd/conf.d/x.conf
    - name: copy Xindex.html to remotehost
      copy: src=/root/Xindex.html dest=/web/vhosts/x/index.html

    - name: copy y.conf to remotehost
      copy: src=/root/y.conf dest=/etc/httpd/conf.d/y.conf
    - name: copy Yindex.html to remotehost
      copy: src=/root/Yindex.html dest=/web/vhosts/y/index.html
EOF

# 檢查playbook的語法
ansible-playbook -C -vvv vhost_conf.yml

# 執行playbook
ansible-playbook vhost_conf.yml

# 檢查配置文件是否存在問題
ansible WebServer -m shell -a 'httpd -t'

# 重啓httpd服務
ansible WebServer -m shell -a 'systemctl restart httpd'

# 編輯hosts文件,便於使用域名訪問gttp服務器
echo '192.168.0.51    www.X.com www.Y.com' >> /etc/hosts
curl www.X.com
curl www.Y.com

# 查看配置文件
ansible WebServer -m shell -a 'ls -l /var/log/httpd'

# 查看訪問日誌
ansible WebServer -m shell -a 'cat /var/log/httpd/x.access'
ansible WebServer -m shell -a 'cat /var/log/httpd/y.access'
相關文章
相關標籤/搜索