Ansible变量源码安装apache

源码安装apache

1环境准备

1.1准备两台主机

主机IP
ansible192.168.25.128
apache192.168.25.130

1.2域名解析

[root@ansible ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.25.130 httpd

1.3给apache设置免密登录

[root@ansible ansible]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
[root@ansible ansible]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FfwMfU2ceV3i1lRcd0xlPQdDxjkfwuZbcaw8rS59wFM root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|         ....o*@^|
|          o..*=%#|
|          .+oo+=O|
|         .  oo+.E|
|        S    .o+ |
|             .=  |
|             o o |
|            . o .|
|             . . |
+----[SHA256]-----+
[root@ansible ansible]# ssh-copy-id root@192.168.25.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.25.130's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.25.130'"
and check to make sure that only the key(s) you wanted were added.


2给apache主机下载源码包

apr源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
httpd源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
apr_util源码包地址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

[root@ansible ansible]# cat playdemo/apache.yml 
--- 
- hosts: 192.168.25.130
  tasks:
    - name: httpd    //安装httpd
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
        dest: /root
    - name: apr    //安装apr
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
        dest: /root
    - name: apr-util    //安装apr-util
      get_url:
        url:  https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
        dest: /root

[root@ansible ansible]# ansible-playbook playdemo/apache.yml 

PLAY [192.168.25.130] *******************************************************************

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

TASK [httpd] ****************************************************************************
changed: [192.168.25.130]

TASK [apr] ******************************************************************************
changed: [192.168.25.130]

TASK [apr-util] *************************************************************************
changed: [192.168.25.130]

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

[root@apache ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg   apr-util-1.6.1.tar.gz  initial-setup-ks.cfg
模板  图片  下载  桌面  apr-1.7.0.tar.gz  httpd-2.4.48.tar.gz

3编辑变量文件

[root@ansible ansible]# cat vars_file/httpd.yml 
---
user: http
 
packages:
  - openssl-devel
  - pcre-devel
  - expat-devel
  - libtool
  - gcc
  - gcc-c++
  - make
  - pcre
  - perl-devel
  - perl
  - '@Development Tools'

apr_install: "cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install"
apr_util_install: "cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install"
httpd_install: "cd /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/http --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util  && make && make install"
  
[root@ansible ansible]# tree
.
├── ansible.cfg
├── group_vars
├── hosts
├── host_vars
│   └── 192.168.25.130
├── http.yml
├── inventory
├── playbook
│   ├── apache.yml
│   ├── ll
│   │   ├── firewalld.yml
│   │   ├── httpd
│   │   │   ├── conf.yml
│   │   │   └── httpd.yml
│   │   ├── install
│   │   │   └── install.yml
│   │   └── php
│   │       └── php.yml
│   ├── test.yml
│   └── vars
│       └── test.yml
├── roles
└── vars_file
    └── httpd.yml


4编写playbook

[root@ansible ansible]# cat http.yml 
---
- hosts: 192.168.25.130
  vars_files:
    - vars_file/httpd.yml
  tasks:
    - name: create user
      user:
        name: "{{ user }}"
        state: present
    - name: install http environment
      yum:
        name: "{{ packages }}"
        state: present
    - name: uncompress apr
      unarchive:
        src: /root/apr-1.7.0.tar.gz/
        dest: /root/
        copy: yes
    - name: uncompress apr-util
      unarchive:
        src: /root/apr-util-1.6.1.tar.gz/
        dest: /root/
        copy: yes
    - name: uncompress httpd
      unarchive:
        src: /root/httpd-2.4.48.tar.gz/
        dest: /root/
        copy: yes
    - name: config
      lineinfile:
        path: /root/apr-1.7.0/configure
        regexp: '^$RM "$cfgfile"'
        line: '#RM "$cfgfile"'
    - name: make
      shell: "{{ apr_install }}"
    - name: make2
      shell: "{{ apr_util_install }}"
    - name: make3
      shell: "{{ httpd_install }}"



5执行playbook

[root@ansible ansible]# ansible-playbook  http.yml 

PLAY [192.168.25.130] *******************************************************************

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

TASK [create user] **********************************************************************
ok: [192.168.25.130]

TASK [install http environment] *********************************************************
ok: [192.168.25.130]

TASK [uncompress apr] *******************************************************************
changed: [192.168.25.130]

TASK [uncompress apr-util] **************************************************************
ok: [192.168.25.130]

TASK [uncompress httpd] *****************************************************************
ok: [192.168.25.130]

TASK [config] ***************************************************************************
changed: [192.168.25.130]

TASK [make] *****************************************************************************
changed: [192.168.25.130]

TASK [make2] ****************************************************************************
changed: [192.168.25.130]

TASK [make3] ****************************************************************************
changed: [192.168.25.130]

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

//启动apache
[root@ansible ansible]# ansible 192.168.25.130 -a ' /usr/local/http/bin/apachectl start'
192.168.25.130 | CHANGED | rc=0 >>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20a1:fdd5:36c5:6f01%ens160. Set the 'ServerName' directive globally to suppress this message
[root@ansible ansible]# ansible 192.168.25.130 -m raw -a 'ss -antl'
192.168.25.130 | CHANGED | rc=0 >>
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:111              0.0.0.0:*       
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*       
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*       
LISTEN    0         5                127.0.0.1:631              0.0.0.0:*       
LISTEN    0         128              127.0.0.1:6010             0.0.0.0:*       
LISTEN    0         128                   [::]:111                 [::]:*       
LISTEN    0         128                      *:80                     *:*       
LISTEN    0         128                   [::]:22                  [::]:*       
LISTEN    0         5                    [::1]:631                 [::]:*       
LISTEN    0         128                  [::1]:6010                [::]:*       
Shared connection to 192.168.25.130 closed.


6访问

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值