使用ansible批量管理linux服务器

首先我们需要安装Ansible,避免麻烦我们直接更换阿里云yum源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install ansible

上面是服务端需要安装的,客户端也是需要安装一些工具的

yum -y install libselinux-python

下面我们来看看ansible的基本配置,主要是/etc/ansible/下hosts文件添加以下内容

[client]
192.168.128.128
192.168.128.129

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

[client]
192.168.128.128 ansible_ssh_user=root ansible_ssh_pass=admin
192.168.128.129 ansible_ssh_user=root ansible_ssh_pass=admin

上面的ansible_ssh_user后面是客户端的账户,ansible_ssh_pass是客户端的密码

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

以上直接操作ansible会报错

“Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.”

解决方法是:

vi /etc/ansible/ansible.cfg
[defaults]
forks          = 8           #执行时并发数
host_key_checking = False    #不检测host key

下面就介绍利用ansible远程批量执行命令

ansible client -m command -a 'uptime'

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

ansible命令测试

示例1:获取192.168.128.128的主机的w信息
ansible 192.168.128.128 -m command -a "w"
192.168.128.128 | SUCCESS | rc=0 >> 
13:44:07 up  5:32,  3 users,  load average: 0.00, 0.01, 0.05USER     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

示例2:获取整个client(all)主机组的对应的“w”信息

ansible client -m command -a "w"
192.168.128.128 | SUCCESS | rc=0 >> 
13:45:12 up  5:33,  3 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Sat09   15:21   0.07s  0.07s -bash
root     pts/0    192.168.128.128       Mon23   13:30m  0.08s  0.00s -bash
root     pts/1    m01              13:45    0.00s  0.12s  0.00s /bin/sh -c /usr192.168.128.128 | SUCCESS | rc=0 >> 21:05:58 up  6:34,  3 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                14:41    4:22m  0.07s  0.07s -bash
root     pts/0    192.168.128.128       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 /usr192.168.128.128 | SUCCESS | rc=0 >> 12:05:07 up  1:05,  2 users,  load average: 0.00, 0.01, 0.05USER     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 /usr192.168.128.128 | SUCCESS | rc=0 >> 22:36:51 up 18:39,  2 users,  load average: 0.00, 0.01, 0.05USER     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 /usr192.168.128.128 | SUCCESS | rc=0 >> 13:45:13 up  6:49,  2 users,  load average: 0.00, 0.01, 0.05USER     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

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

ansible 192.168.128.128 -m copy -a "src=/etc/hosts dest=/tmp"
    192.168.128.128 | 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}
ansible 192.168.128.128 -m command -a "ls /tmp"
#查看一下对方主机目录下有啥
192.168.128.128 | SUCCESS | rc=0 >>      
ansible_0qlGau
hosts               #在这里呢,拷贝成功pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log

ssh root@192.168.128.128 "ls /tmp"
#用ssh再看一下`root@192.168.128.128's password: `hosts
#在这里呢拷贝成功pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log
# 命令说明:
-m:调用ansible内置模块   copy  拷贝模块
-a:接命令。由于调用了copy模块,命令格式发生改变。src=本地文件路径 dest=目的地所在路径

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

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

命令说明:

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

备注:

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

特别提示:

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

利用ansible远程执行各类脚本

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

echo "echo '测试成功!'" >> /server/scripts/test.sh
ansible client -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=0755 backup=yes"

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

2 远程批量执行脚本

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

示例:

ansible client -m command -a "sh /server/scripts/test.sh"
192.168.128.128 | SUCCESS | rc=0 >>
测试成功!192.168.128.128 | SUCCESS | rc=0 >>
测试成功!192.168.128.129 | SUCCESS | rc=0 >>
......

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

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

第5章 查看Ansible的帮助

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


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/359.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。