前言:
ansible能做什么,最常用的就是批量部署,批量下发配置,批量执行程序等等。最常见的的模式就是通过SSH去控制待更新的机器。
(1)安装方法很多种
简单的pip安装:pip install ansible
(2)配置SSH公钥秘钥无密码登陆
参考文章:ssh公钥密钥无密码登陆
(3)配置ansible hosts文件/etc/ansible/hosts(此乃默认路径,也可以使用-i指定其他路径),32323是我客户机的SSH端口,108是我客户机的IP
[test-ping]
192.168.1.108:32323
[test-copy]
192.168.1.108:32323
[test-command]
192.168.1.108:32323
[test-shell]
192.168.1.108:32323
(4)查看ansible手册
ansible --help
(5)列出ansible模块
ansible-doc -l
(6)查看模块用法,比如查看ping模块用法
ansible-doc ping
(7)常用的模块,比如ping、copy、command、shell等等
(8)例子,先ping客户机,然后准备一个t.sh文件,然后将t.sh拷贝至客户机,更改权限后运行,并观察结果
t.sh
#! /bin/sh
echo "hello world"
ansible test-ping -m ping
ansible test-copy -m copy -a "src=~/t.sh dest=~/t.sh"
ansible test-command -m command -a "chmod +x ~/t.sh"
ansible test-shell -m shell -a "~/t.sh"
ansible功能非常强大,这里不深入展开,更多资料请参考官网:https://www.ansible.com/
原文出自:http://blog.csdn.net/daiyudong2020/article/details/70234316
End;