马哥N49第十七周作业

1、使用ansible的playbook实现自动化安装httpd
1.1 修改/etc/ansible/hosts文件添加webservers集群
[root@localhost ansible]# cat hosts 
[webservers]
10.0.0.57

1.2 创建主配置文件role_httpd.yml
[root@localhost ansible]# cat role_httpd.yml 
---
# httpd role
- hosts: webservers
  remote_user: root
  
  roles:
    - httpd
[root@localhost ansible]# 
1.3创建相关目录和文件
[root@localhost ansible]# tree roles
roles
└── httpd
    ├── files
    │   ├── apr-1.7.0.tar.bz2
    │   ├── apr-util-1.6.1.tar.bz2
    │   ├── httpd-2.4.46.tar.gz
    │   ├── httpd.conf
    │   └── httpd.service
    ├── handlers
    │   └── main.yml
    └── tasks
        ├── config.yml
        ├── group.yml
        ├── install.yml
        ├── main.yml
        ├── service.yml
        ├── unarch_httpd.yml
        └── user.yml

4 directories, 13 files
[root@localhost ansible]# 
1.4 编写playbook
cat roles/httpd/tasks/group.yml 
- name: create apache group
  group: name=apache system=yes gid=80
cat roles/httpd/tasks/user.yml 
- name: create apache user
  user: name=apache system=yes shell=/sbin/nologin home=/var/www/ uid=80 group=apache
cat roles/httpd/tasks/unarch_httpd.yml 
- name: unarchive httpd
  unarchive: src=httpd-2.4.46.tar.gz dest=/usr/local/src/ copy=yes
- name: unarchive apr 
  unarchive: src=apr-1.7.0.tar.bz2 dest=/usr/local/src/ copy=yes
- name: unarchive apr-util
  unarchive: src=apr-util-1.6.1.tar.bz2 dest=/usr/local/src/ copy=yes
cat roles/httpd/tasks/install.yml 
- name: install dependency package
  yum: 
    name: [gcc,pcre-devel,openssl-devel,expat-devel] 
    state: present

- name: install apr 
  shell: mv /usr/local/src/apr-1.7.0 /usr/local/src/httpd-2.4.46/srclib/apr
  
- name: install apr-util
  shell: mv /usr/local/src/apr-util-1.6.1 /usr/local/src/httpd-2.4.46/srclib/apr-util 

- name: install httpd
  shell: cd /usr/local/src/httpd-2.4.46 && ./configure --prefix=/apps/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre  \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=event \
&& make && make install
cat roles/httpd/tasks/config.yml 
- name: httpd conf file
  copy: src=httpd.conf dest=/apps/httpd24/conf/
  notify: restart
cat roles/httpd/tasks/service.yml 
- name: copy httpd.conf
  copy: src=httpd.service dest=/usr/lib/systemd/system/ 

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

cat roles/httpd/tasks/main.yml 
- include: group.yml
- include: user.yml
- include: unarch_httpd.yml
- include: install.yml
- include: config.yml
- include: service.yml
cat roles/httpd/handlers/main.yml 
- name: restart
  service: name=httpd state=restarted
cat /etc/ansible/role_httpd.yml
---
# httpd role
- hosts: webservers
  remote_user: root
  
  roles:
    - httpd
1.5 检查playbook语法
ansible-playbook role_httpd.yml -C
1.6 执行playbook
ansible-playbook role_httpd.yml 
2、建立httpd服务器,要求提供两个基于名称的虚拟主机:
2.1 www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
2.1.2 在主配置文件追加vhosts配置文件的存放目录
echo "include conf/vhosts/*/*.conf" >> /apps/httpd24/conf/httpd.conf
mkdir /apps/httpd24/conf/vhosts/{X,Y} -pv
mkdir /var/log/httpd
mkdir /web/vhosts/{x,y} -pv
2.1.3 创建虚拟主机X.conf

ServerName www.X.com
DocumentRoot "/web/vhosts/x"
ErrorLog "/var/log/httpd/x.err"
CustomLog "/var/log/httpd/x.access" combined

Options None
AllowOverride None
Require all granted


2.2 www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
2.2.1 创建虚拟主机Y.conf

ServerName www.Y.com
DocumentRoot "/web/vhosts/y"
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/y.access" combined

Options None
AllowOverride None
Require all granted


2.3 为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
2.3.1 编辑/web/vhosts/x/index.html
echo www.X.com > /web/vhosts/x/index.html
2.3.2 编辑/web/vhosts/y/index.html
echo www.Y.com > /web/vhosts/y/index.html
2.3.3 重启httpd
systemctl restart httpd
2.3.4 修改hosts文件,添加域名解析
cat /etc/hosts
10.0.0.57 www.X.com www.Y.com
2.3.5 访问测试
[root@localhost ~]# curl www.X.com
www.X.com
[root@localhost ~]# 
[root@localhost ~]# curl www.Y.com
www.Y.com

你可能感兴趣的:(马哥N49第十七周作业)