各個節(jié)點如下
ansible | 被管理端 |
---|---|
10.0.0.7 centos7 | 10.0.0.7 centos7 |
10.0.0.18 centos8 | |
10.0.0.28 centos8 |
思路:首先在ansible編譯好httpd后 利用角色傳送好編譯的文件到各個主機(jī) 然后啟動
1.ansible主機(jī)編譯httpd
說明:安裝httpd-2.4亭敢,依賴于apr-1.4+, apr-util-1.4+
編譯安裝的背景:centos8編譯安裝httpd很簡單知牌,因為centos8提供的apr的包版本比較新亮垫,但是在centos7中yum源的apr版本低于httpd2.4的要求了始鱼。所以需要單獨下載apr包和apr-util包進(jìn)行編譯
準(zhǔn)備httpd軟件和apr apr-util
# 準(zhǔn)備編譯工具
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即多進(jìn)程I/O模型:并行啟動多個進(jìn)程,每個進(jìn)程響應(yīng)一個連接請求半火。centos7默認(rèn)模型
#一個主進(jìn)程:生成和回收n個子進(jìn)程,創(chuàng)建套接字,不響應(yīng)請求
#多個子進(jìn)程:工作 work進(jìn)程方库,每個子進(jìn)程處理一個請求;系統(tǒng)初始時障斋,預(yù)先生成多個空閑進(jìn)程纵潦,等待請求
# 開始編譯 以兩核心交叉編譯
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.準(zhǔn)備角色目錄
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目錄準(zhǔn)備內(nèi)容
#之前編譯好的httpd文件在`/app/httpd24/`路徑,將該目錄打包壓縮到`/etc/ansible/roles/httpd/files`
cd /etc/ansible/roles/httpd/files
tar -cjf httpd.tar.bz2 /app/httpd24/
#準(zhǔn)備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
#準(zhǔn)備httpd的環(huán)境變量文件到roles/httpd/files目錄下
cat > /etc/ansible/roles/httpd/files/httpd.sh <<EOF
PATH=$PATH:/app/httpd24/bin
EOF
#準(zhǔn)備template模版文件到roles/httpd/templates模版目錄下
cd /app/httpd24/conf
cp /app/httpd24/conf/httpd.conf /etc/ansible/roles/httpd/templates/httpd.conf.j2
#修改模版文件的原有配置為變量
sed -ri.bak -e '/^User/s/(User).*/\1 {{ APACHE_USER }}/' -e '/^Group/s/(Group).*/\1 {{ APACHE_GROUP }}/' httpd.conf.j2
sed -ri '/^Listen/s/(Listen).*/Listen {{ LISTEN_PORT }}/' httpd.conf.j2
sed -ri 's/^#(ServerName).*/\1 {{ SERVER_NAME }}/' httpd.conf.j2
#sed -ri '/^DocumentRoot/s/(DocumentRoot).*/\1 "{{ ROOT }}"/' httpd.conf.j2
# 驗證
# sed -rn '/^(User|Group)/p' httpd.conf.j2
# sed -rn '/^Listen/p' httpd.conf.j2
# sed -rn '/^ServerName/p' httpd.conf.j2
4.roles/httpd/tasks目錄準(zhǔn)備內(nèi)容
#創(chuàng)建用戶和組 group.yaml user.yaml
cat > /etc/ansible/roles/httpd/tasks/group.yaml <<EOF
- name: create apache group
group: name=apache state=present system=yes gid=80
EOF
cat > /etc/ansible/roles/httpd/tasks/user.yaml <<EOF
- name: create apache user
user: name=apache state=present system=yes create_home=no home=/var/www shell=/sbin/nologin uid=80 group=apache
EOF
#準(zhǔn)備httpd軟件software.yaml
#之前已經(jīng)將軟件目錄打包并采用bzip2壓縮 放在了files目錄下
cat > /etc/ansible/roles/httpd/tasks/software.yaml <<EOF
- name: unarchive tar.bz2 to remote host
unarchive: src=httpd.tar.bz2 dest=/
EOF
#準(zhǔn)備unit和環(huán)境變量文件unit.yaml
cat >/etc/ansible/roles/httpd/tasks/unit.yaml <<EOF
- name: copy var_config_file to remote host
copy: src=httpd.sh dest=/etc/profile.d/ mode=644 owner=root
- name: copy unit_file to remote host
copy: src=httpd.service dest=/usr/lib/systemd/system/
notify: reload httpd
EOF
#準(zhǔn)備調(diào)用模版文件template.yaml
cat > /etc/ansible/roles/httpd/tasks/template.yaml <<EOF
- name: copy template file to remote host
template: src=httpd.conf.j2 dest=/app/httpd24/conf/httpd.conf
EOF
#準(zhǔn)備服務(wù)文件service.yaml
cat >/etc/ansible/roles/httpd/tasks/service.yaml <<EOF
- name: start httpd.service
service: name=httpd state=started enabled=yes
EOF
#創(chuàng)建handlers文件
cat > /etc/ansible/roles/httpd/handlers/main.yaml <<EOF
- name: reload httpd
service: name=httpd state=reloaded
EOF
5.roles/httpd/vars目錄準(zhǔn)備變量文件
cat > /etc/ansible/roles/httpd/vars/main.yaml <<EOF
APACHE_USER: apache
APACHE_GROUP: apache
LISTEN_PORT: 8080
SERVER_NAME: wangcloud.top
EOF
6.準(zhǔn)備tasks的入口main文件 調(diào)整執(zhí)行順序
cat > /etc/ansible/roles/httpd/tasks/main.yaml <<EOF
- include: group.yaml
- include: user.yaml
- include: software.yaml
- include: unit.yaml
- include: template.yaml
- include: service.yaml
EOF
7.準(zhǔn)備playbook
cat > /etc/ansible/role_httpd.yaml <<EOF
---
- hosts: all
remote_user: root
gather_facts: no
roles:
- role: httpd
EOF
最后:查看目錄 檢測語法 執(zhí)行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
#測試執(zhí)行
ansible-playbook -C /etc/ansible/role_httpd.yaml
#執(zhí)行
ansible-playbook /etc/ansible/role_httpd.yaml
驗證結(jié)果
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
當(dāng)然我這里沒有準(zhǔn)備測試頁文件到files目錄 也沒有寫測試頁的tasks垃环。如果有需要 則再寫邀层。用ansible copy模塊把文件復(fù)制過去就行。