各个节点如下
ansible | 被管理端 |
---|---|
10.0.0.7 centos7 | 10.0.0.7 centos7 |
10.0.0.18 centos8 | |
10.0.0.28 centos8 |
思路:首先在ansible编译好httpd后 利用角色传送好编译的文件到各个主机 然后启动
1.ansible主机编译httpd
说明:安装httpd-2.4,依赖于apr-1.4+, apr-util-1.4+
编译安装的背景:centos8编译安装httpd很简单,因为centos8提供的apr的包版本比较新,但是在centos7中yum源的apr版本低于httpd2.4的要求了。所以需要单独下载apr包和apr-util包进行编译
准备httpd软件和apr apr-util
# 准备编译工具
yum -y install gcc make pcre-devel openssl-devel expat-devel
cd /usr/local/src
wget https://downloads.apache.org//apr/apr-1.7.0.tar.bz2
wget https://downloads.apache.org//apr/apr-util-1.6.1.tar.bz2
wget https://downloads.apache.org//httpd/httpd-2.4.43.tar.bz2
tar xvf apr-1.7.0.tar.bz2
tar xvf apr-util-1.6.1.tar.bz2
tar xvf httpd-2.4.43.tar.bz2
将apr和apr-util源码和httpd源码合并
mv apr-1.7.0 httpd-2.4.43/srclib/apr
mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
ls httpd-2.4.43/srclib/
apr apr-util Makefile.in
将三者一并编译并安装
cd httpd-2.4.43/
./configure \
--prefix=/app/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
# 我这里配置--with-mpm=prefork httpd 支持三种MPM工作模式:prefork, worker, event
# prefork即多进程I/O模型:并行启动多个进程,每个进程响应一个连接请求。centos7默认模型
#一个主进程:生成和回收n个子进程,创建套接字,不响应请求
#多个子进程:工作 work进程,每个子进程处理一个请求;系统初始时,预先生成多个空闲进程,等待请求
# 开始编译 以两核心交叉编译
make -j 2 && make install
# 编译完成
root@7 ~]# ll /app/httpd24/
total 44
drwxr-xr-x 2 root root 302 Feb 1 18:36 bin
drwxr-xr-x 2 root root 253 Feb 1 18:36 build
drwxr-xr-x 2 root root 78 Feb 1 18:36 cgi-bin
drwxr-xr-x 4 root root 84 Feb 1 18:36 conf
drwxr-xr-x 3 root root 4096 Feb 1 18:36 error
drwxr-sr-x 2 root root 24 Mar 26 2020 htdocs
drwxr-xr-x 3 root root 8192 Feb 1 18:36 icons
drwxr-xr-x 2 root root 8192 Feb 1 18:36 include
drwxr-xr-x 3 root root 281 Feb 1 18:35 lib
drwxr-xr-x 2 root root 6 Feb 1 18:36 logs
drwxr-xr-x 4 root root 30 Feb 1 18:36 man
drwxr-sr-x 14 root root 8192 Mar 26 2020 manual
drwxr-xr-x 2 root root 4096 Feb 1 18:36 modules
2.准备角色目录
cd /etc/ansible/roles
mkdir httpd/{tasks,templates,files,vars,handlers,meta} -p
root@7 roles]# tree
.
└── httpd
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
7 directories, 0 files
3.roles/httpd/files目录准备内容
#之前编译好的httpd文件在`/app/httpd24/`路径,将该目录打包压缩到`/etc/ansible/roles/httpd/files`
cd /etc/ansible/roles/httpd/files
tar -cjf httpd.tar.bz2 /app/httpd24/
#准备service unit文件到roles/httpd/files目录下(centos7)
cat > /etc/ansible/roles/httpd/files/httpd.service << EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/app/httpd24/bin/apachectl start
#ExecStart=/app/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/app/httpd24/bin/apachectl graceful
#ExecReload=/app/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/app/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#准备httpd的环境变量文件到roles/httpd/files目录下
cat > /etc/ansible/roles/httpd/files/httpd.sh <
4.roles/httpd/tasks目录准备内容
#创建用户和组 group.yaml user.yaml
cat > /etc/ansible/roles/httpd/tasks/group.yaml < /etc/ansible/roles/httpd/tasks/user.yaml < /etc/ansible/roles/httpd/tasks/software.yaml </etc/ansible/roles/httpd/tasks/unit.yaml < /etc/ansible/roles/httpd/tasks/template.yaml </etc/ansible/roles/httpd/tasks/service.yaml < /etc/ansible/roles/httpd/handlers/main.yaml <
5.roles/httpd/vars目录准备变量文件
cat > /etc/ansible/roles/httpd/vars/main.yaml <
6.准备tasks的入口main文件 调整执行顺序
cat > /etc/ansible/roles/httpd/tasks/main.yaml <
7.准备playbook
cat > /etc/ansible/role_httpd.yaml <
最后:查看目录 检测语法 执行playbook
root@7 roles]# tree
.
└── httpd
├── files
│ ├── httpd.service
│ ├── httpd.sh
│ └── httpd.tar.bz2
├── handlers
│ └── main.yaml
├── meta
├── tasks
│ ├── conf.yaml
│ ├── group.yaml
│ ├── main.yaml
│ ├── service.yaml
│ ├── software.yaml
│ ├── template.yaml
│ ├── unit.yaml
│ └── user.yaml
├── templates
│ ├── httpd.conf.bak
│ └── httpd.conf.j2
└── vars
└── main.yaml
#Inventory
[websrvs]
10.0.0.7
[dbsrvs]
10.0.0.7
10.0.0.[1:2]8
#检查语法
ansible-playbook --syntax-check /etc/ansible/role_httpd.yaml
#测试执行
ansible-playbook -C /etc/ansible/role_httpd.yaml
#执行
ansible-playbook /etc/ansible/role_httpd.yaml
验证结果
ansible all -m shell -a 'systemctl is-active httpd'
10.0.0.7 | CHANGED | rc=0 >>
active
10.0.0.28 | CHANGED | rc=0 >>
active
10.0.0.18 | CHANGED | rc=0 >>
active
当然我这里没有准备测试页文件到files目录 也没有写测试页的tasks。如果有需要 则再写。用ansible copy模块把文件复制过去就行。