第十六周作业

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

  1. 安装 ansible
yum --enablerepo=aliyun_epel install ansible -y
  1. 管理哪些主机
    在这里插入图片描述
  2. 免密登陆
ssh-keygen -P "" -t rsa -f /root/.ssh/id_rsa &> /dev/null
ssh-copy-id 192.168.43.29
ssh-copy-id 192.168.43.39
ssh-copy-id 192.168.43.49
  1. 创建目录结构
mkdir -pv /project/playbook/roles/httpd/{files,handlers,templates,vars,tasks}

在这里插入图片描述

  1. 文件内容

1) 网站的默认页面 /project/playbook/roles/httpd/files/index.html

<h1>www.xuepeng.com</h1>

2) /project/playbook/roles/httpd/handlers/main.yml 触发动作

- name: restart
  service: name=httpd state=restarted
- name: check
  shell: killall -0 httpd > /tmp/httpd.log

3 ) 执行的任务
install.yml

- name: install httpd
  yum: name=httpd state=present

config7.yml

- name: config7
  template: src=httpd7.conf.j2 dest=/etc/httpd/conf/httpd.conf
  when: ansible_distribution_major_version=="7"
  notify:
  - restart
  - check

index.yml

- name: default page
  copy: src=index.html dest=/var/www/html/

service.yml

- name: service
  service: name=httpd state=started

main.yml

- include: install.yml
- include: config7.yml
- include: index.yml
- include: service.yml

4 ) 模板文件 /project/playbook/roles/httpd/templates/httpd7.conf.j2
将/etc/httpd/conf/httpd.conf 文件拷贝一份为 httpd7.conf.j2
Listen {{httpd7_port}}

5 ) 变量 /project/playbook/roles/httpd/vars/main.yml

httpd7_port: 8080

5 ) roles 文件
/project/playbook/httpd.role.yml

- hosts: httpdsrvs
  remote_user: root

  roles:
   - httpd
  1. 运行 playbook
    在这里插入图片描述
    在这里插入图片描述

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

(1)www.xuepeng.com,页面文件目录为/web/vhosts/xuepeng;错误日志为
/var/log/httpd/xuepeng.err,访问日志为/var/log/httpd/xuepeng.access
(2)www.pengpeng.com,页面文件目录为/web/vhosts/pengpeng;错误日志为 /var/log/httpd/pengpeng.err,访问日志为/var/log/httpd/pengpeng.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

yum install httpd -y
vim /etc/httpd/conf.d/www.xuepeng.com.conf
<VirtualHost *:80>
        ServerName www.xuepeng.com
        DocumentRoot "/web/vhosts/xuepeng"
        ErrorLog "/var/log/httpd/xuepeng.err"
        CustomLog "/var/log/httpd/xuepeng.access" combined
        <Directory "/web/vhosts/xuepeng">
            AllowOverride None
            # Allow open access:
            Require all granted
        </Directory>
</VirtualHost>

mkdir /web/vhosts/xuepeng/ -pv
echo `hostname` > /web/vhosts/xuepeng/index.html
httpd -t
systemctl start httpd
cp -p www.xuepeng.com.conf www.pengpeng.com.conf
vim www.pengpeng.com.conf
	:%s@xuepeng@pengpeng@g
mkdir /web/vhosts/pengpeng
httpd -t
systemctl restart httpd
echo `hostname` > /web/vhosts/pengpeng/index.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值