Ansible源码安装apache

文章目录

  • Ansible源码安装apache
    • ansible源码安装apache
    • 准备两台主机
    • 添加域名解析
    • 配合SSH免密登陆
    • 创建项目文件夹
    • 下载源码包
    • 编写主机变量文件 gaofan.yml文件
    • 编写源码安装apache文件httpd_size.yml文件
    • 执行playbook文件
    • 关闭防火墙和selinux
    • 用IP访问,查看httpd安装情况

Ansible源码安装apache

ansible源码安装apache

准备两台主机

hostname ip
master 192.168.91.128
node2 192.168.91.130

添加域名解析

[root@master ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.91.130 node2

配合SSH免密登陆

[root@master ~]# ssh-keygen -t rsa  #生成密钥,指定加密方式 ,下面默认一路回车即可
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:m9UgEbkFLID4MnlRrsmn76K0ARRhb6NEScTM1etAfEM root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|*O=+=E .++       |
|+==+ +. o..      |
| =.=o o..o.      |
|*.=+..  .. o     |
|.=+ +   S . .    |
|.  o .   +       |
| o.     o        |
|. +.             |
|.o oo            |
+----[SHA256]-----+

[root@master ~]# ssh-copy-id root@node2   #传递公钥
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'httpd (192.168.91.130)' can't be established.
ECDSA key fingerprint is SHA256:+wH81RHiBmLpbkuk2OWGZxVRziiaNwJ9KAVjGtEM8zs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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@httpd's password: 

Number of key(s) added: 1

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

创建项目文件夹

[root@master project]# tree /opt/project/
/opt/project/
├── ansible.cfg
├── group_vars
├── host_vars
│   ├── 192.168.91.129
│   └── gaofan.yml
├── inventories
├── packages
│   ├── apr-1.7.0.tar.gz
│   ├── apr-util-1.6.1.tar.gz
│   └── httpd-2.4.48.tar.gz
└── playbook
    ├── files
    │   └── vsftpd.conf
    ├── firewalld.yml
    ├── httpd-install.yml
    ├── httpd_size.yml
    ├── test.yml
    ├── user.yml
    ├── vars
    │   ├── run
    │   ├── runtime.yml
    │   └── run.yml
    └── vsftpd.yml

下载源码包

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@master ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
 
[root@master ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz

[root@master ~] wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

[root@master ~] mv  apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz /opt/project/packages/

编写主机变量文件 gaofan.yml文件

[root@master ~]# vim /opt/project/host_vars/gaofan.yml
---
   # 编译工具
   tools:  gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre
   # 编译安装apr          
   apr_install: " cd /root/apr-1.7.0/  &&  ./configure --prefix=/usr/local/apr && make && make install "
   # 编译安装apr-util
   apr_util_intall: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "
   # 编译安装httpd
   httpd_install: " 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 "  

编写源码安装apache文件httpd_size.yml文件

[root@master ~]# vim /opt/project/playbook/httpd_size.yml
---
   - hosts: httpd
     vars_files:
       - /opt/project/host_vars/gaofan.yml #指定变量文件
    
     tasks: 
            
       - name: install  server tools   #安装编译工具
         yum: 
           name: "{{ tools }}"
           state: present
              
       - name: copy package apr    #将本地压缩包解压后,传到远程主机上
         unarchive:
           src: /opt/project/packages/apr-1.7.0.tar.gz
           dest: /root/
           copy: yes 

       - name: copy package apr-util
         unarchive:
           src: /opt/project/packages/apr-util-1.6.1.tar.gz
           dest: /root/ 
           copy: yes

       - name: copy package httpd
         unarchive:
           src: /opt/project/packages/httpd-2.4.48.tar.gz
           dest: /root/
           copy: yes

       - name: create group apache    # 创建apache使用的用户和组
         group:
           name: apache
           system: yes
           state: present
         
       - name: create user apache
         user: 
           name: apache
           system: yes
           state: present  
                 
       - name: install apr
         shell: " {{ apr_install }}"  #运行变量

       - name: intall apr-util
         shell: " {{ apr_util_intall }}"   

       - name: install httpd
         shell: " {{ httpd_install }}  "

       - name: start httpd service  #开启服务
         shell: " /usr/local/httpd/bin/apachectl start "

执行playbook文件

[root@master project]# ansible-playbook  playbook/httpd_size.yml

关闭防火墙和selinux

[root@master project]# ansible all -m shell -a "systemctl stop firewalld"
node2 | CHANGED | rc=0 >>

[root@master project]# ansible all -m shell -a "systemctl disable firewalld"
node2 | CHANGED | rc=0 >>
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master project]# ansible all -m shell -a "setenforce 0"
node2 | CHANGED | rc=0 >>

用IP访问,查看httpd安装情况

Ansible源码安装apache_第1张图片

你可能感兴趣的:(Linux,Ansible自动化,linux)