ansible之文件的批量分发

 

python语言是运维人员必会的语言!
ansible是一个基于Python开发的自动化运维工具
ansible的功能实现基于SSH远程连接服务
ansible可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能

特点
1)不需要单独安装客户端,基于系统自带的sshd服务,sshd就相当于ansible的客户端
2)不需要服务端
3)需要依靠大量的模块实现批量管理
4)配置文件/etc/ansible/ansible.cfg

开始安装ansible

yum -y install ansible

所有被管理端需要安装也可以不安装

yum -y install libselinux-python  

ansible基础配置

ansible的配置文件:

[root@m01 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg     #ansible配置文件
├── hosts           #被ansible管理的主机名单(分组)
└── roles           

1 directory, 2 files

编辑ansible的主机配置文件hosts,添加主机组chensiqi

[root@m01 ~]# cp /etc/ansible/hosts{,.bak}  #改前备份可是个好习惯
[root@m01 ~]# tail -6 /etc/ansible/hosts
[chensiqi]
172.16.1.31
172.16.1.41
172.16.1.61
172.16.1.7
172.16.1.8

如果设置了ssh密钥连接的话,hosts文件到这里就算配置完毕了。但是我们还没有设置,因此还需要对ansible的主机映射文件/etc/ansible/hosts继续加工

[root@m01 ~]# tail -6 /etc/ansible/hosts
[chensiqi]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.61 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=登录密码

命令说明:
ansible_ssh_user:ssh连接的用户名
ansible_ssh_pass:ssh连接的密码

注意:
如果没有做密钥认证,hosts又没有如上方式配置的话,ansible进行远程连接是会失败的。

利用ansible远程批量执行命令

语法:

ansible chensiqi -m command -a 'uptime'
ansible 主机组 -m ansible内置功能模块名 -a 命令

ansible命令测试

示例1:获取172.16.1.8的主机的w信息

[root@m01 ~]# ansible 172.16.1.8 -m command -a "w"
172.16.1.8 | SUCCESS | rc=0 >>
 13:44:07 up  5:32,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Sat09   14:16   0.07s  0.07s -bash
root     pts/0    172.16.1.1       Mon23   13:29m  0.08s  0.00s -bash
root     pts/1    m01              13:44    0.00s  0.14s  0.00s /bin/sh -c /usr

[root@m01 ~]# 

示例2:获取整个chensiqi主机组的对应的“w”信息

[root@m01 ~]# ansible chensiqi -m command -a "w"
172.16.1.8 | SUCCESS | rc=0 >>
 13:45:12 up  5:33,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Sat09   15:21   0.07s  0.07s -bash
root     pts/0    172.16.1.1       Mon23   13:30m  0.08s  0.00s -bash
root     pts/1    m01              13:45    0.00s  0.12s  0.00s /bin/sh -c /usr

172.16.1.61 | SUCCESS | rc=0 >>
 21:05:58 up  6:34,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                14:41    4:22m  0.07s  0.07s -bash
root     pts/0    172.16.1.1       16:44    2.00s  1.11s  0.67s /usr/bin/python
root     pts/8    m01              21:05    1.00s  0.10s  0.00s /bin/sh -c /usr

172.16.1.7 | SUCCESS | rc=0 >>
 12:05:07 up  1:05,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                11:00   26:21   0.09s  0.09s -bash
root     pts/0    m01              12:05    0.00s  0.30s  0.00s /bin/sh -c /usr

172.16.1.41 | SUCCESS | rc=0 >>
 22:36:51 up 18:39,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                03:58   58:54   0.11s  0.11s -bash
root     pts/0    m01              22:36    0.00s  0.32s  0.00s /bin/sh -c /usr

172.16.1.31 | SUCCESS | rc=0 >>
 13:45:13 up  6:49,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Mon20   13:42   0.15s  0.15s -bash
root     pts/0    m01              13:45    0.00s  0.30s  0.00s /bin/sh -c /usr

[root@m01 ~]# 

示例3:调用ansible内置的copy模块

[root@m01 ~]# ansible 172.16.1.8 -m copy -a "src=/etc/hosts dest=/tmp"                  #输入命令
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "checksum": "dba0126bf49ea8d4cdc476828f9edb37085c6afe", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root",    #文件复制过去以后的属组
    "md5sum": "09bad48d0c62411850fd04b68f836335", 
    "mode": "0644",     #文件复制过去以后的权限
    "owner": "root",    #文件复制过去以后的属主
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 294, 
    "src": "/root/.ansible/tmp/ansible-tmp-1489411262.1-267125154401179/source", 
    "state": "file", 
    "uid": 0
}
[root@m01 ~]# ansible 172.16.1.8 -m command -a "ls /tmp" #查看一下对方主机目录下有啥
172.16.1.8 | SUCCESS | rc=0 >>      
ansible_0qlGau
hosts               #在这里呢,拷贝成功
pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log

[root@m01 ~]# ssh root@172.16.1.8 "ls /tmp" #用ssh再看一下
`root@172.16.1.8's password: `
hosts               #在这里呢拷贝成功
pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log
[root@m01 ~]# 



命令说明:
-m:调用ansible内置模块   copy  拷贝模块
-a:接命令。由于调用了copy模块,命令格式发生改变。src=本地文件路径 dest=目的地所在路径

示例4:调用copy模块实现保存文件的属性改变

[root@m01 ~]# ansible 172.16.1.8 -m copy -a "src=/etc/hosts dest=/tmp owner=chensiqi group=chensiqi mode=600"
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "checksum": "dba0126bf49ea8d4cdc476828f9edb37085c6afe", 
    "dest": "/tmp/hosts", 
    "gid": 502, 
    "group": "chensiqi", 
    "mode": "0600", 
    "owner": "chensiqi", 
    "path": "/tmp/hosts", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 294, 
    "state": "file", 
    "uid": 502
}
[root@m01 ~]# ssh root@172.16.1.8 "ls -l /tmp/hosts"
`root@172.16.1.8's password: `
-rw-------. 1 chensiqi chensiqi 294 3月  14 14:00 /tmp/hosts

命令说明:
对于ansible内置模块的使用,大家参考命令执行后的返回信息里看就可以了。写的很清楚。

备注:
copy模块,如果复制的对方主机路径下没有目录,那么会递归创建

特别提示:

ansible的部分模块并不支持
1)管道符 “|”
2)重定向 “> < >> <<”
3)类似top,tail -f这种不能即刻返回明确信息的命令
4)*

 利用ansible远程执行各类脚本

3.3.1 先将脚本分发到各个机器上去

[root@m01 ~]# echo "echo '测试成功!'" >> /server/scripts/test.sh

ansible chensiqi -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=0755 backup=yes"

注意:dest路径的写法,若是不存在的目录,结尾要加斜线(/server/scripts/),否则默认不会创建目标目录

3.3.2 远程批量执行脚本

ansible chensiqi -m shell -a "/server/scripts/yum.sh"

示例:

[root@m01 ~]# ansible chensiqi -m command -a "sh /server/scripts/test.sh"
172.16.1.41 | SUCCESS | rc=0 >>
测试成功!

172.16.1.31 | SUCCESS | rc=0 >>
测试成功!

172.16.1.7 | SUCCESS | rc=0 >>
测试成功!

172.16.1.8 | SUCCESS | rc=0 >>
测试成功!

172.16.1.61 | SUCCESS | rc=0 >>
测试成功!

 常用模块:每个模块就是一个功能

模块名作用
command 执行命令模块(重要)
copy 文件拷贝模块(重要)
shell 执行shell脚本模块(重要)
script 执行shell脚本模块(重要)
file 设定文件属性模块
service 系统服务管理模块
cron 计划任务管理模块
yum yum软件包安装管理模块
synchronize 使用rsync同步文件模块

 查看Ansible的帮助

ansible-doc -l 查看所有的模块
ansible-doc -s service 查看指定模块用法

原文地址:https://www.cnblogs.com/zpzp7878/p/10467108.html