环境很重要、环境很重要、环境很重要

# CentOS 7
$ setenforce 0  # 可以设置配置文件永久关闭
$ systemctl stop iptables.service
$ systemctl stop firewalld.service

一. 准备 Python3 和 Python 虚拟环境

1.1 安装依赖包

$ yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git

1.2 编译安装

$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
$ tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1
$ ./configure && make && make install

1.3 建立 Python 虚拟环境

因为 CentOS 6/7 自带的是 Python2,而 Yum 等工具依赖原来的 Python,为了不扰乱原来的环境我们来使用 Python 虚拟环境

$ cd /opt
$ python3 -m venv py3
$ source /opt/py3/bin/activate

# 看到下面的提示符代表成功,以后运行 Jumpserver 都要先运行以上 source 命令,以下所有命令均在该虚拟环境中运行
(py3) [root@localhost py3]

二、Fabric 安装和使用

  fabric版本:(十分重要)
  pip install fabric3 or pip3 install fabric3

  常用命令:

  fab -u root -p '123456.' -H ‘10.0.0.1’ -- 'free -m'

  fab -u root -p 123456 -H "192.168.1.108" -- 'uname -a'

  fab -u root -p '123456' -H '10.0.0.1' -- 'hostname anthony-aliyun'

  fab -u root -p '123456' -H '192.168.1.108' -- 'hostname anthony-local'

  常用脚本格式:

  

 1 #conding:utf-8
 2 from fabric.api import *
 3 
 4 env.user = "root"
 5 env.hosts = ["192.168.1.104","192.168.1.108"]
 6 env.passwords = {
 7         "root@192.168.1.104:22":'123456789',
 8         "root@192.168.1.108:22":'123456'
 9 }
10 
11 env.roledefs = {
12         "webserver":["192.168.1.104"],
13         "dbserver":["192.168.1.108"]
14 }
15 
16 @task
17 def show():
18         run("free -m")
19 
20 @task
21 @roles("webserver")
22 def install_httpd():
23         print("install webserver")
24 
25 @task
26 @roles("dbserver")
27 def install_mysql():
28         print("install mysql")
View Code

相关文章: