• ansible角色部署lamp架构


    使用ansible角色部署lamp架构


    一、部署Apache


    1、配置主机并创建角色

    [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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、编写task任务

    [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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    3、编写脚本

    [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&
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
  • 相关阅读:
    这款开源神器,让聚类算法从此变得简单易用
    八、数据类型转换
    修正版 | 面对千万级、亿级流量怎么处理?
    关于yolo7和gpu
    看懂这篇文章,你就懂了Mybatis的二级缓存
    Android之OKHttp源码解析
    DFS 桥与割点 tarjan 算法
    【通意千问】大模型GitHub开源工程学习笔记(1)--依赖库
    SQL Server 数据库之生成与执行 SQL 脚本
    【代码随想录】算法训练计划21、22
  • 原文地址:https://blog.csdn.net/qq_65441164/article/details/127733945