Ansible 使用playbook编译安装apache

Ansible 使用playbook编译安装apache

1.关闭防火墙

//关闭防火墙
---
- hosts: apache
  tasks: 
    - name: stop firewalld
      service: 
        name: firewalld
        state: stopped

    - name: 
      lineinfile: 
        path: /etc/selinux/config
        regexp: "^SELINUX="
        line: "SELINUX=disabled"

    - name: 
      shell: 
        setenforce 0

//执行playbook
[root@localhost opt]# ansible-playbook lamp/firewalld.yml 

PLAY [all] *********************************************************************
skipping: no hosts matched

PLAY RECAP ******************************************************************

2.拉取apache软件包

//拉去软件包
[root@localhost opt]# vim apache.yml
---
- hosts: apache
  tasks:
    - name:
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz       
        dest: /root/
        
    - name:
      get_url:
       url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
        dest: /root/
        
    - name:
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
        dest: /root/

[root@localhost opt]# ansible-playbook  apache.yml 
PLAY [apache] ******************************************************************

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

TASK [get_url] *****************************************************************
changed: [192.168.200.145]

TASK [get_url] *****************************************************************
changed: [192.168.200.145]

TASK [get_url] *****************************************************************
changed: [192.168.200.145]

PLAY RECAP *********************************************************************
192.168.200.145            : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

//受控主机
[root@localhost ~]# ls
anaconda-ks.cfg   apr-util-1.6.1.tar.gz
apr-1.7.0.tar.gz  httpd-2.4.48.tar.gz

3.安装依赖环境编写变量

//编写环境变量
[root@localhost opt]# tree
.
├── ansible.cfg
├── apache.yml
├── group_vars
│   ├── apache
│   └── mysql
├── host_vars
│   ├── 192.168.200.145
│   └── 192.168.200.147
├── inventory
├── lamp
│   ├── firewalld.yml
│   ├── httpd.yml
│   ├── mysql.yml
│   └── php.yml
├── playbook
│   ├── auto.yml
│   ├── test.yml
│   └── user.yml
└── vars_file
    ├── httpd.yml
    └── line.yml
//编写变量
[root@localhost opt]# vim vars_file/httpd.yml 
user: apache

packages:
  - openssl-devel
  - pcre-devel
  - expat-devel
  - libtool
  - gcc
  - gcc-c++
  - make
  - pcre
  - perl-devel
  - perl
  - '@Development Tools'
        
apr: " cd /root/apr-1.7.0/  &&  ./configure --prefix=/usr/local/apr && make && make install "

apr_util: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "

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

4.编写playbook

---
- hosts: apache
  vars_files:
    - vars_file/httpd.yml
  tasks:
    - name: create user
      user:
        name: "{{ user }}"
        shell: sbin/nologin
        state: present

    - name: Installation dependency environment
      yum:
        name: "{{ packages }}"
        state: present

    - name: uncompress app
      unarchive:
        src: /root/apr-1.7.0.tar.gz
        dest: /root/
        copy: no

    - name: uncompress app
      unarchive:
        src: /root/apr-util-1.6.1.tar.gz4
        dest: /root/
        copy: no

    - name: uncompress app
      unarchive:
        src: /root/httpd-2.4.48.tar.gz
        dest: /root/
        copy: no

    - name: config
      lineinfile:
        path: /root/apr-1.7.0/configure
        regexp: '^$RM "$cfgfile"'
        line: '# $RM "$cfgfile"'

    - name: make
      shell: "{{ apr}}"

    - name: make2
      shell: "{{ apr_util }}"

    - name: make3
      shell: "{{ httpd }}"
//运行
[root@localhost opt]# ansible-playbook  apache.yml 

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

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

TASK [create user] *************************************************************
changed: [192.168.200.145]

TASK [Installation dependency environment] *************************************
changed: [192.168.200.145]

TASK [uncompress app] **********************************************************
changed: [192.168.200.145]

TASK [uncompress app] **********************************************************
changed: [192.168.200.145]

TASK [uncompress app] **********************************************************
changed: [192.168.200.145]

TASK [config] ******************************************************************
changed: [192.168.200.145]

TASK [make] ********************************************************************
changed: [192.168.200.145]

TASK [make2] *******************************************************************
changed: [192.168.200.145]

TASK [make3] *******************************************************************
changed: [192.168.200.145]

PLAY RECAP *********************************************************************
192.168.200.145            : ok=10   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


//启动apache
[root@localhost opt]# ansible apache -m shell -a ' /usr/local/apache/bin/apachectl start'
192.168.200.145 | CHANGED | rc=0 >>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

5.访问

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值