一、服务器准备
Master 10.0.82.55 Node1 10.0.82.56 Node2 10.0.82.37
二、配置Master SSH免密码登录节点服务器
具体方法,搜索本博客中的文章
三、Master安装Ansible服务
1、yum安装
yum -y install ansible
2、配置文件 vim /etc/ansible/hosts 添加node的ip
10.0.82.56 10.0.82.37 # 可以分组 如: [web] 10.0.82.56 [db] 10.0.82.37
四、测试
1、在远程服务器根目录创建hello目录
ansible all -a "mkdir /hello" 运行结果 10.0.82.37 | SUCCESS | rc=0 >> 10.0.82.56 | SUCCESS | rc=0 >>
2、把Master本地文件拷贝到远程服务器
ansible all -m copy -a "src=/data/www/test.txt dest=/data/www" 报错了 10.0.82.56 | FAILED! => { "changed": false, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "failed": true, "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!" 解决方案(如果受控机开启SELinux,则必须在受控机上安装libselinux-python) ansible all -k -m yum -a "name=libselinux-python state=installed" 再次运行 ansible all -m copy -a "src=/data/www/test.txt dest=/data/www" 运行结果 10.0.82.37 | SUCCESS => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/data/www/test.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1476215949.6-127742282482280/source", "state": "file", "uid": 0 } 10.0.82.56 | SUCCESS => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/data/www/test.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:default_t:s0", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1476215949.96-237849415762054/source", "state": "file", "uid": 0 }
3、查看节点服务器的某个进程
ansible all -m shell -a "ps -ef | grep nginx" 运行结果 172.17.0.4 | SUCCESS | rc=0 >> root 68 1 0 09:55 ? 00:00:00 nginx: master process /data/soft/nginx/nginx -c /data/soft/nginx/nginx.conf nobody 69 68 0 09:55 ? 00:00:00 nginx: worker process root 298 293 0 09:58 pts/0 00:00:00 /bin/sh -c ps -ef | grep nginx root 300 298 0 09:58 pts/0 00:00:00 grep nginx 172.17.0.3 | SUCCESS | rc=0 >> root 71 1 0 08:18 ? 00:00:00 nginx: master process /data/soft/nginx/nginx -c /data/soft/nginx/nginx.conf nobody 72 71 0 08:18 ? 00:00:00 nginx: worker process root 1816 1811 0 09:58 pts/0 00:00:00 /bin/sh -c ps -ef | grep nginx root 1818 1816 0 09:58 pts/0 00:00:00 grep nginx
4、更改节点服务器文件权限
ansible all -m file -a "dest=/data/www/gong.txt mode=777" 运行结果 172.17.0.4 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/data/www/gong.txt", "size": 6, "state": "file", "uid": 0 } 172.17.0.3 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/data/www/gong.txt", "size": 6, "state": "file", "uid": 0 }
4、在节点服务器上拉取一个git库
ansible all -m shell -a "cd /data/www; git clone git@git.coding.net:gongfuxiang/hello.git" 运行结果 172.17.0.4 | SUCCESS | rc=0 >> Cloning into 'hello'... 172.17.0.3 | SUCCESS | rc=0 >> Cloning into 'hello'...
Ansible更多使用方法 Ansible中文使用权威指南
发表评论: