一、安装支持包

yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel zlib-devel zlib openssl-devel openssl

二、源码编译Python3.5

  下载安装包:

ansible在python3运行 ansible python3_配置文件

tar xf Python-3.5.2.tgz -C /usr/src/
 cd /usr/src/Python-3.5.2/
./configure --prefix=/usr/local/python/
 make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3
which python3
python3 -V (linux下默认2.6版本,基本不用)

ansible在python3运行 ansible python3_python_02

三、使用pip3安装ansible(ansible的批量分发调用模块时,基本都需要Python)

安装ansible最新版本(用pip3的方式安装,下了Python就有这个命令,这是开发装软件的方式,需要用到网络):

/usr/local/python/bin/pip3 install ansible

若在此处报错pip,就按下条命令执行:

/usr/local/python/bin/pip3 install --upgrade pip
  /usr/local/python/bin/pip3 install ansible

安装好ansible后,创建软链接: ln -s /usr/local/python/bin/ansible /usr/local/bin

查看命令: which ansible

查看ansible的版本: ansible --version

ansible在python3运行 ansible python3_python_03

ansible查看帮助:

查看ansible的总帮助: /usr/local/python/bin/ansible-doc -l

查看shell模块的帮助: /usr/local/python/bin/ansible-doc(总帮助) -s(选择模块) shell

           /usr/local/python/bin/ansible-doc -s raw

四、分发密钥

生成密钥对: ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""

 

ansible在python3运行 ansible python3_ansible在python3运行_04

分发密钥: scp .ssh/id_rsa.pub root@192.168.126.151:/root/.ssh

ansible在python3运行 ansible python3_ansible在python3运行_05

测试免密连接: ssh 192.168.126.151 (默认用root连接)

ansible在python3运行 ansible python3_Python_06

五、ansible连接web服务器的配置文件

创建目录: mkdir -p /etc/ansible

创建文件: touch /etc/ansible/hosts\

写入配置文件内容: vim /etc/ansible/hosts (这里的主机名可以不映射,因为后面的IP已经映射过了)

ansible在python3运行 ansible python3_Python_07

六、ansible的使用方法

获取web的平均负载值: ansible web01 -m command -a 'uptime'

ansible -i /etc/ansible/hosts/ 主机或主机组 -m 指定模块 -a 命令

-i (指定配置文件位置,如果不指定,默认到ansible配置文件/etc/ansible/hosts找)

检查服务器是否正常: ansible all -m ping (ping不需要-a指定参数)

 

ansible在python3运行 ansible python3_Python_08

command模块 命令不支持管道符、重定向操作 (连接端需要支持Python)

ansible在python3运行 ansible python3_配置文件_09

shell模块 命令支持管道符、重定向操作。shell模块大部分命令都支持。(连接端需要支持Python)

ansible在python3运行 ansible python3_配置文件_10

raw模块 最原始的方式执行命令(只有极少数情况下使用,可以不依赖Python,仅通过ssh实现。)

ansible在python3运行 ansible python3_Python_11

 copy模块 批量下发文件或文件夹自动备份,更改属主属组。

ansible在python3运行 ansible python3_ansible在python3运行_12

ansible在python3运行 ansible python3_python_13

script模块 能够实现远程服务器批量运行本地的shell脚本/server/scripts/

编译脚本:

ansible在python3运行 ansible python3_Python_14

编写nginx安装脚本: vim auto_nignx.sh

#!/bin/sh
#nginx install shell scriptstest -d /media/cdrom || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom &> /dev/null
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &> /dev/nul
l
test -d /service/scripts || exit 3
cd /service/scripts
tar xf nginx-1.10.2.tar.gz -C /usr/src
cd /usr/src/nignx-1.10.2/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &> /d
ev/null
make &> /dev/null
make install &> /dev/null
exit 0

编写分发脚本:

vim fenfa.sh
#!/bin/sh
Group=$1
ansible $Group -m copy -a "src=/service/scripts dest=/bin/sh /service/scripts/ mode=0755"
ansible $Group -m scripts -a "/service/scripts/auto_nginx.sh"

执行分发脚本:

ansible在python3运行 ansible python3_ansible在python3运行_15

 七、剧本play-book和ansible共同使用的方式

创建软链接: ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/

查看命令: which ansible-playbook

ansible在python3运行 ansible python3_Python_16

设定剧本: vim test_shell.yaml

剧本用.yaml结尾。

ansible在python3运行 ansible python3_配置文件_17

执行剧本: ansible-playbook test_shell.yaml

ansible在python3运行 ansible python3_Python_18

当传输过程中出错误时,更改一下配置文件: vim /etc/ssh/sshd_config

更改配置文件之后需要重启: service sshd reload

ansible在python3运行 ansible python3_Python_19

ansible在python3运行 ansible python3_python_20

nignx配置下发并检测:

 

ansible在python3运行 ansible python3_配置文件_21

在剧本中引入自定义变量:

ansible在python3运行 ansible python3_ansible在python3运行_22

ansible all(或自己的IP地址) -m setup | less

ansible在python3运行 ansible python3_Python_23

 

python3的使用方法:

在命令界面直接输入python3

Python3中取键值的方法:

ansible在python3运行 ansible python3_Python_24

利用template模块下发可变的配置文件:

ansible在python3运行 ansible python3_Python_25

ansible在python3运行 ansible python3_ansible在python3运行_26

ansible在python3运行 ansible python3_ansible在python3运行_27

 

  下发配置文件里面使用判断语法:

ansible在python3运行 ansible python3_配置文件_28

  测试执行(自定义一个端口号):

ansible在python3运行 ansible python3_ansible在python3运行_29

  执行脚本:

ansible在python3运行 ansible python3_ansible在python3运行_30

ansible在python3运行 ansible python3_ansible在python3运行_31

   测试不定义端口号(置空):

ansible在python3运行 ansible python3_配置文件_32

  再次执行脚本:

ansible在python3运行 ansible python3_Python_33

ansible在python3运行 ansible python3_ansible在python3运行_34

playbook的notify通知和下发nginx配置

如果你下发的文件引起它改变了,它通知在执行一遍文件,如果他没有引起改变,则通知不下发就不会触发改变对方服务。

ansible在python3运行 ansible python3_python_35

ansible在python3运行 ansible python3_配置文件_36