playbook源码部署apache
playbook源码部署apache
环境准备
主机 | IP |
---|---|
ansible | 192.168.47.128 |
apache | 192.168.47.129 |
实验步骤
创建项目文件
[root@ansible ansible]# tree
.
├── 10 [defaults]
├── ansible.cfg
├── hosts
├── inventory
├── playbook
│ ├── firewalld.yml
│ ├── host_vars
│ │ └── apache.yml
│ ├── install.yml
│ └── packages
│ ├── apr-1.7.0.tar.gz
│ ├── apr-util-1.6.1.tar.gz
│ └── httpd-2.4.48.tar.gz
└── roles
域名解析
[root@ansible ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.47.129 httpd
给Apache主机下载源码包
apr源码包
https://mirrors.tuna.tsinghua.edu.cn/apache/apr/
httpd源码包:
https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
--2021-07-22 20:36:13-- https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1093896 (1.0M) [application/octet-stream]
正在保存至: “apr-1.7.0.tar.gz”
apr-1.7.0.tar.gz 100%[===================================================>] 1.04M 2.76MB/s 用时 0.4s
2021-07-22 20:36:13 (2.76 MB/s) - 已保存 “apr-1.7.0.tar.gz” [1093896/1093896])
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
--2021-07-22 20:36:34-- https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:554301 (541K) [application/octet-stream]
正在保存至: “apr-util-1.6.1.tar.gz”
apr-util-1.6.1.tar.gz 100%[===================================================>] 541.31K 1.15MB/s 用时 0.5s
2021-07-22 20:36:35 (1.15 MB/s) - 已保存 “apr-util-1.6.1.tar.gz” [554301/554301])
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
--2021-07-22 20:37:01-- https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9418226 (9.0M) [application/octet-stream]
正在保存至: “httpd-2.4.48.tar.gz”
httpd-2.4.48.tar.gz 100%[===================================================>] 8.98M 2.54MB/s 用时 3.5s
2021-07-22 20:37:04 (2.54 MB/s) - 已保存 “httpd-2.4.48.tar.gz” [9418226/9418226])
创建packages目录放置源码包
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# mkdir playbook
[root@ansible ansible]# cd playbook/
[root@ansible playbook]# mkdir packages
[root@ansible ~]# cp apr-1.7.0.tar.gz /etc/ansible/playbook/packages/
[root@ansible ~]# cp apr-util-1.6.1.tar.gz /etc/ansible/playbook/packages/
[root@ansible ~]# cp httpd-2.4.48.tar.gz /etc/ansible/playbook/packages/
通过host_vars定义变量
[root@ansible playbook]# mkdir host_vars
[root@ansible host_vars]# cat apache.yml
---
# 编译工具
dell: gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre
# 编译安装apr
apr_install: " cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install "
# 编译安装apr-util
apr_util_install: "cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install"
# 编译安装httpd
httpd_install: " cd /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util && make && make install "
关闭防火墙和selinux
[root@ansible ansible]# cat playbook/firewalld.yml
---
- name: apache
hosts: apache
tasks:
- name: stop and disabled firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: disabled selinux
lineinfile:
path: /etc/selinux/config
regexp: "^SELINUX="
line: "SELINUX=disabled"
state: present
[root@ansible ansible]# ansible-playbook playbook/firewalld.yml
PLAY [apache] *************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.47.129]
TASK [stop and disabled firewalld] ****************************************************************************************
ok: [192.168.47.129]
TASK [disabled selinux] ***************************************************************************************************
changed: [192.168.47.129]
PLAY RECAP ****************************************************************************************************************
192.168.47.129 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
编写源码安装apache文件
[root@ansible ansible]# cat playbook/install.yml
---
- hosts: apache
vars_files:
- /etc/ansible/playbook/host_vars/apache.yml
tasks:
- name: install tools
yum:
name: "{{ dell }}"
state: present
- name: cp apr
unarchive:
src: /etc/ansible/playbook/packages/apr-1.7.0.tar.gz
dest: /root/
copy: yes
- name: cp apr-util
unarchive:
src: /etc/ansible/playbook/packages/apr-util-1.6.1.tar.gz
dest: /root/
copy: yes
- name: cp httpd
unarchive:
src: /etc/ansible/playbook/packages/httpd-2.4.48.tar.gz
dest: /root/
copy: yes
- name: create user
user:
name: apache
system: yes
state: present
- name: install apr
shell: " {{ apr_install }}"
- name: intall apr-util
shell: " {{ apr_util_install }}"
- name: install httpd
shell: " {{ httpd_install }} "
- name: start httpd service
shell: " /usr/local/httpd/bin/apachectl start "
[root@ansible ansible]# ansible-playbook playbook/install.yml
PLAY [apache] *************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.47.129]
TASK [install tools] ******************************************************************************************************
ok: [192.168.47.129]
TASK [cp apr] *************************************************************************************************************
ok: [192.168.47.129]
TASK [cp apr-util] ********************************************************************************************************
ok: [192.168.47.129]
TASK [cp httpd] ***********************************************************************************************************
ok: [192.168.47.129]
TASK [create user] ********************************************************************************************************
ok: [192.168.47.129]
TASK [install apr] ********************************************************************************************************
changed: [192.168.47.129]
TASK [intall apr-util] ****************************************************************************************************
changed: [192.168.47.129]
TASK [install httpd] ******************************************************************************************************
changed: [192.168.47.129]
TASK [start httpd service] ************************************************************************************************
changed: [192.168.47.129]
PLAY RECAP ****************************************************************************************************************
192.168.47.129 : ok=10 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
修改配置文件
[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
192 #
193 ServerName localhost:80
网页站点配置
[root@apache ~]# cat /usr/local/httpd/htdocs/index.html