美文网首页
ansible实现自动化编译安装httpd的角色

ansible实现自动化编译安装httpd的角色

作者: 念念OPS | 来源:发表于2021-02-02 01:54 被阅读0次

    各个节点如下

    ansible 被管理端
    10.0.0.7 centos7 10.0.0.7 centos7
    10.0.0.18 centos8
    10.0.0.28 centos8

    思路:首先在ansible编译好httpd后 利用角色传送好编译的文件到各个主机 然后启动

    1.ansible主机编译httpd

    说明:安装httpd-2.4,依赖于apr-1.4+, apr-util-1.4+
    编译安装的背景:centos8编译安装httpd很简单,因为centos8提供的apr的包版本比较新,但是在centos7中yum源的apr版本低于httpd2.4的要求了。所以需要单独下载apr包和apr-util包进行编译
    准备httpd软件和apr apr-util

    # 准备编译工具
    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即多进程I/O模型:并行启动多个进程,每个进程响应一个连接请求。centos7默认模型
    #一个主进程:生成和回收n个子进程,创建套接字,不响应请求
    #多个子进程:工作 work进程,每个子进程处理一个请求;系统初始时,预先生成多个空闲进程,等待请求
    # 开始编译 以两核心交叉编译
    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.准备角色目录

    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目录准备内容

    #之前编译好的httpd文件在`/app/httpd24/`路径,将该目录打包压缩到`/etc/ansible/roles/httpd/files`
    cd /etc/ansible/roles/httpd/files
    tar -cjf httpd.tar.bz2 /app/httpd24/
    
    #准备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
    
    #准备httpd的环境变量文件到roles/httpd/files目录下
    cat > /etc/ansible/roles/httpd/files/httpd.sh <<EOF
    PATH=$PATH:/app/httpd24/bin
    EOF
    
    #准备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目录准备内容

    #创建用户和组 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
    
    #准备httpd软件software.yaml
    #之前已经将软件目录打包并采用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
    
    #准备unit和环境变量文件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
    
    #准备调用模版文件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
    
    #准备服务文件service.yaml
    cat >/etc/ansible/roles/httpd/tasks/service.yaml <<EOF
    - name: start httpd.service
      service: name=httpd state=started enabled=yes
    EOF
    
    #创建handlers文件
    cat > /etc/ansible/roles/httpd/handlers/main.yaml <<EOF
    - name: reload httpd
      service: name=httpd state=reloaded
    EOF
    

    5.roles/httpd/vars目录准备变量文件

    cat > /etc/ansible/roles/httpd/vars/main.yaml <<EOF
    APACHE_USER: apache
    APACHE_GROUP: apache
    LISTEN_PORT: 8080
    SERVER_NAME: wangcloud.top
    EOF
    

    6.准备tasks的入口main文件 调整执行顺序

    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.准备playbook

    cat > /etc/ansible/role_httpd.yaml <<EOF
    ---
    - hosts: all
      remote_user: root
      gather_facts: no
      roles:
      - role: httpd
    EOF
    

    最后:查看目录 检测语法 执行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
    #测试执行
    ansible-playbook -C /etc/ansible/role_httpd.yaml
    #执行
    ansible-playbook /etc/ansible/role_httpd.yaml
    

    验证结果

    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
    
    image.png

    当然我这里没有准备测试页文件到files目录 也没有写测试页的tasks。如果有需要 则再写。用ansible copy模块把文件复制过去就行。

    相关文章

      网友评论

          本文标题:ansible实现自动化编译安装httpd的角色

          本文链接:https://www.haomeiwen.com/subject/pcsetltx.html