配置主机
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg hosts roles
[root@ansible ansible]# vim hosts
[dev]
node1
创建角色
[root@ansible ansible]# cd roles/
[root@ansible roles]# ls
[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
通过apache角色配置httpd服务
先编写tasks任务
[root@ansible roles]# vim apache/tasks/main.yml
---
# tasks file for apache
- name: set firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/sysconfig/selinux
regexp: '^SELINUX='
line: 'SELINUX=disabled'
- name: setenforce 0
shell: setenforce 0
- name: yum.sh
script: yum.sh
- name: install pkgs
yum:
name: "{
{ httpdpkgs }}"
state: present
- name: uzip pkgs
unarchive:
src: apr-1.6.5.tar.bz2
dest: /opt/
- name: uzip pkgs1
unarchive:
src: apr-util-1.6.1.tar.bz2
dest: /opt/
- name: uzip pkgs2
unarchive:
src: httpd-2.4.54.tar.bz2
dest: /opt/
- name: create user
user:
name: apache
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: make install
script: httpd.sh
- name: bianliang
script: bianliang.sh
- name: set service
template:
src: httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
- name: daemon-reload
shell: systemctl daemon-reload
- name: restart httpd
service:
name: httpd
state: started
enabled: yes
yum源脚本
[root@ansible apache]# vim files/yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -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 files/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 apache]# vim files/bianliang.sh
export PATH=/usr/local/apache/bin/:$PATH
vars变量
[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 apache]# vim templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop