[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts
node1
[root@ansible ansible]# ls
ansible.cfg hosts roles
[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ls
apache mysql php
[root@ansible apache]# vim tasks/main.yml
---
# tasks file for apache
- name: set yum
script: yum.sh
- name: install packages
yum:
name: "{
{ httpdpkgs }}"
state: present
- name: unzip apr
unarchive:
src: apr-1.6.5.tar.bz2
dest: /usr/src/
- name: unzip apr-util
unarchive:
src: apr-util-1.6.1.tar.bz2
dest: /usr/src/
- name: unzip httpd
unarchive:
src: httpd-2.4.54.tar.bz2
dest: /usr/src/
- name: install httpd
script: httpd.sh
- name: apache.sh
script: apache.sh
- name: create user
user:
name: apache
system: yes
create_home: no
shell: /sbin/nologin
state: present
- name: set httpd service
template:
src: httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
- name: refresh
shell:
cmd: systemctl daemon-reload
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
[root@ansible files]# ls
apache.sh apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.54.tar.bz2 httpd.sh yum.sh
[root@ansible files]# vim yum.sh
#!/bin/bash
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@ansible apache]# vim vars/main.yml
---
# vars file for apache
httpdpkgs:
- bzip2
- make
- wget
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- libxml2-devel
[root@ansible files]# vim httpd.sh
#/bin/bash
cd /opt/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install
cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
cd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make
make install
[root@ansible files]# vim apache.sh
export PATH=/usr/local/apache/bin/:$PATH
[root@ansible apache]# vim templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart&