使用ansible配置HTTP服务

1、使用ansible的playbook实现自动化安装httpd

本次实验需要准备两个虚拟机,一台安装ansible,另一台安装httpd

配置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 [email protected] "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

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,内容分别为其对应的主机名

本次实验使用的是前一个实验的两个虚拟机

# 编辑配置文件
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'

猜你喜欢

转载自blog.51cto.com/14920534/2621894