实战环境(需求描述:无法使用公网在线安装)
  服务器已做好了镜像的离线yum源,可以离线安装vim等工具,无法离线安装ansible

环境检查

服务器无法访问外网,处于网内环境中。确认可以连接公网的安装主机与即将在内网环境部署的主机是否是一个版本的linux系统。
   uname -a

需求分析

  1. 首先离线ansible需要安装的rpm包。
  2. 编写自动构建离线ansible的yum源脚本
  3. 使用脚本安装ansible工具

开始执行

1、离线下载ansible需要安装的rpm包

语句格式: yum install -y 软件名 --downloadonly --downloaddir=保存文件路径
需要下载ansible、createrepo相关rpm包

import  os

# shell命令
# yum install -y ansible --downloadonly --downloaddir=ansible

## 打印当前路径
print os.getcwd() #获取当前工作目录路径

savedir = os.getcwd() + '/software'
print '下载保存路径=',savedir

# 定义ansible需要yum离线缓存的list表
softwares = ['ansible','createrepo']
print softwares
for software in softwares:
   print '当前下载 :', software
   print os.system("date") ## 使用os模块执行shell命令
   print '执行下载:', os.system("yum install -y %s --downloadonly --downloaddir=%s" % (software,savedir)) ## 使用%s拼接字符串 

print '============== 下载完毕 ===================='

2、编写自动构建ansible的离线yum源脚本

构建yum源:简单的rpm安装的话,会提示需要安装很多python的工具依赖

2.1自动构建离线yum源

构建离线yum源脚本:create_repo.sh

#!/bin/bash
basedir=$(cd `dirname $0`;pwd)
softwaredir=$basedir/software
repoDir=/etc/yum.repos.d

## function 通过shell脚本创建文件
function create_ansible_local_repo(){
echo "
[ansible-local]
name=ansible-local
baseurl=file://${softwaredir}/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7" > $repoDir/ansible-local.repo

createrepo -d $softwaredir
yum repolist
yum makecache
}
create_ansible_local_repo

2.1.1 脚本内容知识点解析

函数function的作用:

  1. 函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程。
  2. 它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部分,定义函数只对当前的会话窗口有效,如果再打开一个窗口再定义另外一个函数,就对另一个窗口有效,两者互不影响。
函数调用create_ansible_local_repo
     echo:生成yum源文件

2.2 ansible安装rpm包脚本

编写Step2_install_software.py脚本如下:

import  os

# shell命令 - 安装createrepo
# rpm -ivh deltarpm-3.6-3.el7.x86_64.rpm 
# rpm -ivh python-deltarpm-3.6-3.el7.x86_64.rpm 
# rpm -ivh libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm 
# rpm -ivh createrepo-0.9.9-28.el7.noarch.rpm 

# 打印当前路径
print os.getcwd() #获取当前工作目录路径

# 设置前面下载rpm的文件路径
softwaredir = os.getcwd() + '/software'

# rpm方式安装createrepo
def install_createrepo():
    os.system("rpm -ivh %s/deltarpm-3.6-3.el7.x86_64.rpm" % (softwaredir))
    os.system("rpm -ivh %s/python-deltarpm-3.6-3.el7.x86_64.rpm" % (softwaredir))
    os.system("rpm -ivh %s/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm" % (softwaredir))
    os.system("rpm -ivh %s/createrepo-0.9.9-28.el7.noarch.rpm" % (softwaredir))
    print('安装createrepo:')

install_createrepo()

# 构建离线yum源
def create_yum_repo():
    os.system("sh create_repo.sh")
    print '创建yum离线源:'

create_yum_repo()

## 使用离线yum源安装
def install_ansible():
    os.system("yum install -y ansible")

print '使用本地yum源安装'
install_ansible()

2.3 最后附赠卸载的脚本

通过yum erase -y ansible 卸载

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import  os
# 卸载
def erase_ansible():
    os.system("yum erase -y ansible")
    print '卸载ansible'

erase_ansible()

好了,对于centos7的步骤可以说是到此为止了。只要将脚本拷贝到内网服务器执行即可。
但是有一个前置条件,就是内网的服务器已经做好了系统镜像的离线yum源。

2.4 下面 Red Hat 7.1版本测试

Red Hat Enterprise Linux Server release 7.1