ansible使用和httpd服务虚拟主机实现

一、使用ansible的playbook实现自动化安装httpd
###配置epel源
~]# vim /etc/yum.repos.d/CentOS-Base.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

###清除缓存
~]# yum clean all

###生成缓存
~]# yum makecache

###安装ansible
~]# yum install ansible -y

###配置ssh密钥登陆
[root@centos ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:UAiOTkugbjilL/AxTL5vBHO4ilJMMqAC27340psqYME root@centos
The key's randomart image is:
+---[RSA 2048]----+
|.   .. ..        |
|+. o  ..         |
|*o*o. .          |
|OE*.o  .         |
|*BO* .  S        |
|+==+o            |
|=o++             |
|=...+.           |
|...++.           |
+----[SHA256]-----+

[root@centos ~]# ssh-copy-id -i 172.16.10.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.10.17 (172.16.10.17)' can't be established.
ECDSA key fingerprint is SHA256:uFEtHZIofSPIIr+e8pnHhx6FnDZTgVdjOn5oWLO5sKU.
ECDSA key fingerprint is MD5:2c:66:b5:84:f1:f8:59:e8:6b:f1:e3:13:5b:f4:b7:b3.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.10.17's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.10.17'"
and check to make sure that only the key(s) you wanted were added.

[root@centos ~]# ssh-copy-id -i 172.16.10.27
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.10.27 (172.16.10.27)' can't be established.
ECDSA key fingerprint is SHA256:uFEtHZIofSPIIr+e8pnHhx6FnDZTgVdjOn5oWLO5sKU.
ECDSA key fingerprint is MD5:2c:66:b5:84:f1:f8:59:e8:6b:f1:e3:13:5b:f4:b7:b3.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.10.27's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.10.27'"
and check to make sure that only the key(s) you wanted were added.







###编写playbook脚本 使用roles来安装
[root@centos playbook]# pwd
/data/playbook
[root@centos playbook]# tree
.
├── httpd.yml
└── roles
    └── httpd
        ├── files
        │   └── httpd.conf
        ├── handlers
        └── tasks
            ├── config.yml
            ├── install.yml
            ├── main.yml
            └── service.yml

5 directories, 6 files


[root@centos playbook]# cat httpd.yml 
- hosts: webserver

  roles:
    - httpd

###准备一个配置文件放在files目录下面
vim roles/httpd/files/httpd.conf

Listen 8080

### 准备安装步骤的yml脚本
[root@centos playbook]# cat roles/httpd/tasks/install.yml 
---

- name: install
  yum: name=httpd

[root@centos playbook]# cat roles/httpd/tasks/config.yml 
---

- name: config
  copy: src=httpd.conf dest=/etc/httpd/conf/

[root@centos playbook]# cat roles/httpd/tasks/service.yml 
---

- name: start httpd service
  service: name=httpd state=started enabled=yes

###按照执行顺序在main.yml里排序
[root@centos playbook]# cat roles/httpd/tasks/main.yml 
- include: install.yml 
- include: config.yml
- include: service.yml

### 执行ansible-playbook命令安装
[root@centos playbook]# ansible-playbook  httpd.yml

PLAY [webserver] ***************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [172.16.10.17]

TASK [httpd : install] *********************************************************************************************************************************************************************************************************************
changed: [172.16.10.17]

TASK [httpd : config] **********************************************************************************************************************************************************************************************************************
changed: [172.16.10.17]

TASK [start httpd service] *****************************************************************************************************************************************************************************************************************
changed: [172.16.10.17]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
172.16.10.17               : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



在这里插入图片描述

在这里插入图片描述

二、建立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/y.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

###yum安装httpd
[root@centos web]# yum install httpd -y

###准备页面文件目录
[root@centos web]# mkdir /web/vhosts/{x,y} -pv
[root@centos ~]# cat /web/vhosts/x/index.html 
<h1>www.X.com</h1>
[root@centos ~]# cat /web/vhosts/y/index.html 
<h1>www.Y.com</h1>

[root@centos /]# tree /web/
/web/
└── vhosts
    ├── x
    │   └── index.html
    └── y
        └── index.html

3 directories, 2 files


###编辑配置文件
[root@centos web]# cat /etc/httpd/conf.d/vhosts.conf 
<virtualhost *:80>
documentroot /web/vhosts/x
servername www.x.com
ErrorLog "/var/log/httpd/x.err"
CustomLog "/var/log/httpd/x.access" combined
<Directory "/web/vhosts/x"> 
	Require all granted
</Directory>
</virtualhost>

<virtualhost *:80>
documentroot /web/vhosts/y
servername www.y.com
ErrorLog "/var/log/httpd/y.err"
CustomLog "/var/log/httpd/y.access" combined
<Directory "/web/vhosts/y">
        Require all granted
</Directory>
</virtualhost>

[root@centos web]# systemctl restart httpd

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值