Ansible 部署Lamt的playbook

Ansible 部署Lamt的playbook

说明: 准备一台控制主机,安装好ansible控制节点,一台受控主机

环境说明:

系统版本IP系统应用
centos8(control)192.168.136.140(控制节点)ansible+python36
centos8(compute)192.168.136.139(被控制节点)python36+apache+mysql+tomcat

apache部署

# 先检查两台主机是否可以ping通
[root@control ~]# ansible all -m ping
compute | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong" (成功)
}
[root@control ~]# 

# 查看结构

# 查看文件内容
[root@control init]# cat main.yml 
- import_tasks: yum.yml
- import_tasks: software.yml
- import_tasks: firewalld.yml
- import_tasks: selinux.yml

# 查看防火墙
[root@control init]# cat firewalld.yml 
- name: close firewalld
  service: 
    name: firewalld
    state: stopped
    enabled: no
 # 查看selnix
[root@control init]# cat selinux.yml 
- name: close selinux temporarily
  shell: setenforce 0
- name: permanent close selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX'
    line: 'SELINUX=disabled'
  # 查看安装的命令
[root@control init]# cat software.yml 
- name: install common software
  yum:
    name:
      - wget
      - tree
      - make 
      - vim
    state: present
  # 查看yum
[root@control init]# cat yum.yml 
- name: ensure yum sources exist
  shell: ls /etc/yum.repos.d/* &> /dev/null
- name: remove /etc/yum.repos.d/*
  shell: rm -rf /etc/yum.repos.d/*
- name: copy yum source 
  copy:
    src: ../files/CentOS-Base.repo
    dest: /etc/yum.repos.d/
- name: clean cache
  shell: "yum clean all && yum makecache"

# 先把要用的软件包下载好
[root@control files]# ll
total 21732
-rwxr-xr-x 1 root root     1236 Oct 23 03:48 apache.sh
-rw-r--r-- 1 root root 10736621 Aug 23 18:54 apache-tomcat-8.5.93.tar.gz
-rw-r--r-- 1 root root  1122147 Apr 16  2023 apr-1.7.4.tar.gz
-rw-r--r-- 1 root root   556623 Feb  1  2023 apr-util-1.6.3.tar.gz
-rw-r--r-- 1 root root     1653 Oct 22 06:06 CentOS-Base.repo
-rw-r--r-- 1 root root  9825177 Oct 19 05:09 httpd-2.4.58.tar.gz



# 查看安装依赖包的变量文件
[root@control vars]# cat package.yml 
pkgs:
  - pcre-devel
  - openssl-devel
  - expat-devel
  - libtool
  - java-1.8.0-openjdk
  - java-1.8.0-openjdk-devel
[root@control vars]# 
# 查看结构
[root@control opt]# tree
.
├── lamt
│   ├── ansible.cfg
│   ├── inventory
│   └── lamt.yml
└── moudule
    ├── application
    │   └── php
    ├── databases
    │   └── mysql
    ├── files
    │   ├── apache.sh
    │   ├── apache-tomcat-8.5.93.tar.gz
    │   ├── apr-1.7.4.tar.gz
    │   ├── apr-util-1.6.3.tar.gz
    │   ├── CentOS-Base.repo
    │   ├── httpd-2.4.58.tar.gz
    │   └── tomcat.sh
    ├── init
    │   ├── firewalld.yml
    │   ├── main.yml
    │   ├── selinux.yml
    │   ├── software.yml
    │   └── yum.yml
    ├── template
    │   └── httpd.service
    ├── vars
    │   └── package.yml
    └── webs
        └── nginx
            └── main.yml

12 directories, 18 files
[root@control opt]# 

# 编写apache的脚本
[root@control files]# vim apache.sh 
[root@control files]# pwd
/opt/moudule/files
[root@control files]# cat apache.sh 
#!/bin/bash
cd /usr/src
if [ -d /usr/src/apr-1.7.4 ];then
	echo "已经解压"
else 
    tar xf apr-1.7.4.tar.gz
    echo "正在解压中"
fi
if [ -d /usr/src/apr-util-1.6.3 ];then
        echo "已经解压"
else
    tar xf apr-util-1.6.3.tar.gz
    echo "正在解压中"
fi
if [ -d /usr/src/ httpd-2.4.58 ];then
        echo "已经解压"
else
    tar xf httpd-2.4.58.tar.gz
    echo "正在解压中"
fi

cd apr-1.7.4
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr && \
	make && make install && \
cd ../apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
	make && make install && \

cd ../httpd-2.4.58

./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
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
ln -s /usr/local/apache/include/ /usr/include/httpd
echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
[root@control files]# chmod +x apache.sh 

# 设置apache开机自启
[root@control template]# cat httpd.service 
[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
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

tomcat配置

# 编写tomcat脚本
[root@control files]# cat tomcat.sh 
#!/bin/bash
cd /usr/src/
tar -xf apache-tomcat-8.5.93.tar.gz -C /usr/local
cd 
cd /usr/local
ln -s /usr/local/apache-tomcat-8.5.93/ /usr/local/tomcat
/usr/local/tomcat/bin/catalina.sh start

[root@control files]# 

playbook部署

[root@control lamt]# cat lamt.yml 
- name: set up lamt
  hosts: compute
  vars_files:
    ../moudule/vars/package.yml
  tasks:
    - name: init system config
      import_tasks: ../moudule/init/main.yml
    - name: create system user
      user:
        name: apache
        system: yes
        create_home: no
        shell: /sbin/nologin
        state: present
    - name: download compilation tools
      shell: yum -y install gcc gcc-c++ --allowerasing
    - name: install dependent tools
      yum:
        name: "{{ pkgs }}"
        state: present
    - name: copy apache need packages
      copy:
        src: ../moudule/files/{{ item }}
        dest: /usr/src/
      loop: 
        - apr-1.7.4.tar.gz
        - apr-util-1.6.3.tar.gz
        - httpd-2.4.58.tar.gz
        - apache-tomcat-8.5.93.tar.gz
    - name: configure install apache
      script: ../moudule/files/apache.sh
    - name: set apache system enable
      template:
        src: ../moudule/template/httpd.service
        dest: /usr/lib/systemd/system/
    - name: start apache
      service:
        name: httpd
        state: started
        enabled: yes
    - name: install mysql
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - mariadb
        - mariadb-server
    - name: start mysql
      service:
        name: mariadb
        state: started
        enabled: yes
    - name: set mysql passwd
      ignore_errors: yes
      shell: mysql -e 'set password = password("123456")'
    - name: configure tomcat
      script: ../moudule/files/tomcat.sh
[root@localhost bin]# ss -antl
State   Recv-Q  Send-Q        Local Address:Port   Peer Address:Port Process 
LISTEN  0       128                 0.0.0.0:22          0.0.0.0:*            
LISTEN  0       100                       *:8080              *:*            
LISTEN  0       511                       *:80                *:*            
LISTEN  0       128                    [::]:22             [::]:*            
LISTEN  0       1        [::ffff:127.0.0.1]:8005              *:*            
LISTEN  0       80                        *:3306              *:*            
[root@localhost bin]# 

http效果

在这里插入图片描述

tomcat效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值