第十六周

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

1.1Ansible安装

1.1.1.安装epel源

[root@ansible ~]# yum -y install epel-release

1.1.2.安装ansible

[root@ansible ~]# yum install ansible -y

1.1.3.修改默认模块

[root@ansible ~]# sed -i 's@# module_name = command@module_name = shell@' /etc/ansible/ansible.cfg

1.1.4.添加主机

[root@ansible ~]# vim /etc/ansible/hosts
#添加
[test]
192.168.214.21
192.168.214.22
192.168.214.23

1.1.5.建立信任关系

[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id 192.168.214.21
[root@ansible ~]# ssh-copy-id 192.168.214.22
[root@ansible ~]# ssh-copy-id 192.168.214.23

1.1.6.测试

[root@ansible ~]# ansible test -a 'echo $HOSTNAME'
192.168.214.21 | CHANGED | rc=0 >>
localhost.localdomain
192.168.214.22 | CHANGED | rc=0 >>
localhost.localdomain
192.168.214.23 | CHANGED | rc=0 >>
localhost.localdomain

1.2.批量安装httpd服务

1.2.1.编写yml文件

[root@ansible ~]# vi httpd_install.yml
#添加
---
  - hosts: test
    remote_user: root
    tasks:
    - name: install httpd
      yum: name=httpd
    - name: change port
      replace: path=/etc/httpd/conf/httpd.conf regexp='Listen 80' replace='Listen 8080'
      notify: restart httpd
      tags: conf
    - name: service start
      service: name=httpd state=started enabled=yes
    handlers:
      - name: restart httpd
        service: name=httpd state=restarted

1.2.2.运行

[root@ansible ~]# ansible-playbook httpd_install.yml

PLAY [test] ****************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [192.168.214.21]
ok: [192.168.214.22]
ok: [192.168.214.23]

TASK [install httpd] *******************************************************************************************************************************************************
changed: [192.168.214.21]
changed: [192.168.214.22]
changed: [192.168.214.23]

TASK [change port] *********************************************************************************************************************************************************
changed: [192.168.214.21]
changed: [192.168.214.22]
changed: [192.168.214.23]

TASK [service start] *******************************************************************************************************************************************************
changed: [192.168.214.21]
changed: [192.168.214.23]
changed: [192.168.214.22]

RUNNING HANDLER [restart httpd] ********************************************************************************************************************************************
changed: [192.168.214.21]
changed: [192.168.214.22]
changed: [192.168.214.23]

PLAY RECAP *****************************************************************************************************************************************************************
192.168.214.21             : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.214.22             : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.214.23             : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2、建立httpd服务器,要求提供两个基于名称的虚拟主机:

(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access

2.1.安装httpd服务

[root@httpd ~]# yum -y isntall httpd

2.2.创建目录和网页

[root@httpd ~]# mkdir /web/vhosts/{x,y} -pv
[root@httpd ~]# echo www.X.com > /web/vhosts/x/index.html
[root@httpd ~]# echo www.Y.com > /web/vhosts/y/index.html

2.3.修改配置文件

[root@httpd ~]# vim /etc/httpd/conf.d/vm.conf
#添加
<virtualhost *:80>
documentroot /web/vhosts/x
CustomLog "/var/log/httpd/x.access" combined
ErrorLog "/var/log/httpd/x.err"
servername www.X.com
<Directory "/web/vhosts/x">
    Require all granted
</Directory>
</virtualhost>

(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access

添加配置文件

[root@httpd ~]# vim /etc/httpd/conf.d/vm.conf
#添加
<virtualhost *:80>
documentroot /web/vhosts/y
CustomLog "/var/log/httpd/y.access" combined
ErrorLog "/var/log/httpd/www2.err"
servername www.Y.com
<Directory "/web/vhosts/y">
    Require all granted
</Directory>
</virtualhost>

2.4.语法测试

[root@httpd ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::e0cd:97aa:bb57:d65f. Set the 'ServerName' directive globally to suppress this message
Syntax OK

2.5.修改主机文件

[root@httpd ~]# echo "127.0.0.1 www.X.com www.Y.com" >> /etc/hosts

2.6.启动服务测试

[root@httpd ~]# systemctl restart httpd
[root@httpd ~]# curl www.X.com
www.X.com
[root@httpd ~]# curl www.Y.com
www.Y.com

2.7.查看日志文件

[root@httpd ~]# tree /var/log/httpd/
/var/log/httpd/
├── access_log
├── error_log
├── www2.err
├── x.access
├── x.err
└── y.access

0 directories, 6 files
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值