1、准备CentOS环境
yum update && yum upgrade
2、控制服务器与被管理服务器要求
Master:Python 2.6+ Slave:Python 2.4+,最好也是2.6+(CentOS 6.7自带)
3、配置SSH自动登录
配置Master向每一台Slave的SSH无密码登录(在Master上使用ssh-keygen生成密钥对,并将id_rsa.pub加入到每一个Slave的authorized_keys中)
4、使用yum安装ansible
yum install ansible
如果提示没有此安装包,可以加入EPEL扩展包:
yum install epel-release yum install ansible
5、配置host
默认在/etc/ansible/hosts,加入Slave的ip地址,例如
[Web] 192.168.1.101 192.168.1.102 192.168.1.103
6、测试
ansible all -m ping
成功ping同后显示
192.168.1.101 | success >> { "changed": false, "ping": "pong" } 192.168.1.102 | success >> { "changed": false, "ping": "pong" } 192.168.1.103 | success >> { "changed": false, "ping": "pong" }
基本查看命令:
ansible all -a 'uptime' ansible all -a 'free -m'
7、使用模块(-m)
command 模块不支持 shell 变量,也不支持管道等,如果需要使用这些功能,可以借助模块
ansible web -m shell -a 'echo $TERM'
拷贝文件模块(-m copy)
ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"
文件属性模块
ansible web -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
服务模块
ansible web -m service -a "name=httpd state=started"
具体用法可以参考:
http://docs.ansible.com/ansible/
http://www.ansible.com.cn/docs/
来源:https://www.cnblogs.com/imzye/p/5143708.html