一、准备CentOS环境web
yum update && yum upgrade
二、控制服务器与被管理服务器要求shell
Master:Python 2.6+ Slave:Python 2.4+,最好也是2.6+(CentOS 6.7自带)
三、配置SSH自动登陆服务器
配置Master向每一台Slave的SSH无密码登陆(在Master上使用ssh-keygen生成密钥对,并将id_rsa.pub加入到每个Slave的authorized_keys中)ssh
四、使用yum安装ansible测试
yum install ansible
若是提示没有此安装包,能够加入EPEL扩展包:spa
yum install epel-release yum install ansible
五、配置hostcode
默认在/etc/ansible/hosts,加入Slave的ip地址,例如blog
[Web] 192.168.1.101 192.168.1.102 192.168.1.103
六、测试ip
ansible all -m ping
成功ping同后显示get
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'
七、使用模块(-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"
具体用法能够参考: