Ansible-playbook源码安装apache

本文档详细介绍了如何通过Ansible-playbook在Linux环境中进行Apache的源码安装。首先,准备两台主机,然后创建项目文件,接着下载并解压APR包,之后修改配置文件。进一步,编写Ansible变量和playbook来自动化安装过程,最后检查安装结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备两台主机

主机IP
client192.168.8.129
node1192.168.8.130

创建项目文件

[root@client playbook]# tree
.
├── 1
├── apache.yml
└── vars
    └── vars_file.yml

下载APR包并解压

[root@client ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz

[root@client ~]#tar xf apr-1.7.0.tar.gz

修改配置文件

[root@client ~]# vim apr-1.7.0/configure
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"  #注释此行

编写ansible变量

[root@client playbook]# cat vars/vars_file.yml 
user: apache

dependency_package: openssl-devel,pcre-devel,expat-devel,libtool,gcc,gcc-c++

httpd: wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz

apr: wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz

apr_util: wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

编写playbook

[root@client playbook]# cat apache.yml
---
- name: install apache
  hosts: 192.168.8.130
  vars_files:
    - vars/vars_file.yml
  tasks:
    - name: create user
      user:
        name: apache

    - name: install Package
      dnf:
        name: "{{ dependency_package  }}"
        state: latest
      when: ansible_facts['distribution'] in platform

    - name: tar packages 
      unarchive:
        src: "{{ item }}"
        copy: yes
        dest: /usr/src/
        mode: 0755
      loop:
        - /usr/src/apr-1.7.0.tar.gz
        - /usr/src/apr-util-1.6.1.tar.gz
        - /usr/src/httpd-2.4.48.tar.gz

    #注释 $RM "$cfgfile"
    - name: apr configure
      copy:
        src: /usr/src/apr-1.7.0/configure
        dest: /usr/src/apr-1.7.0/
        
    - name: copy script
      copy:
        src: /etc/ansible/playbook/apache.sh
        dest: /root/
   
    - name: mode
      file:
        path: /root/apache.sh
        mode: 0655

    - name: script
      shell: "./apache.sh"

    - name: restart httpd
      shell: "/usr/local/httpd/bin/apachectl start"

#查看脚本
[root@client playbook] cat apache.sh
#!/bin/bash
cd /usr/src/apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install

cd /usr/src/apr-util-1.6.1 && ./configure  --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

cd /usr/src/httpd-2.4.48 && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util && make && make install

#执行playbook
[root@client playbook]# ansible-playbook apache.yml 
[WARNING]: While constructing a mapping from /etc/ansible/playbook/vars/vars_file.yml, line
1, column 1, found a duplicate dict key (compile_apr_util). Using last defined value only.

PLAY [install apache] ************************************************************************

TASK [Gathering Facts] ***********************************************************************
ok: [192.168.8.130]

TASK [user apache] ***************************************************************************
ok: [192.168.8.130]

TASK [install Package] ***********************************************************************
ok: [192.168.8.130]

TASK [wget httpd] ****************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'wget'.  If you need
to use command because get_url or uri is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.8.130]

TASK [wget apr] ******************************************************************************
changed: [192.168.8.130]

TASK [wget apr-util] *************************************************************************
changed: [192.168.8.130]

TASK [tar package] ***************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need to use
command because unarchive is insufficient you can add 'warn: false' to this command task or
set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.8.130]

TASK [tar package] ***************************************************************************
changed: [192.168.8.130]

TASK [tar package] ***************************************************************************
changed: [192.168.8.130]

TASK [vim configure] *************************************************************************
changed: [192.168.8.130]

TASK [compile apr1] **************************************************************************
changed: [192.168.8.130]
···略

查看测试结果

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值