1、编辑文件,添加管理分组/etc/ansible/hosts
# Ex 1: Ungrouped hosts, specify before any group headers. ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com [webservers] 192.168.100.66 192.168.100.128
2、分组建立好以后,可以使用ping的方式来确认服务器连接是否正常
oot@zxw63 ansible]# ansible webservers -m ping 192.168.100.66 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.100.128 | SUCCESS => { "changed": false, "ping": "pong" } [root@zxw63 ansible]#
我这里由于已经配置了服务器之间免密码登陆,所以没加key来输入密码。实际情况,可能需要输入密码,最后加参数-k,强制输入密码
ansible webservers -m ping -k
3、使用分组执行命令
ot@zxw63 ~]# ansible webservers -m command -a 'free -m' 192.168.100.66 | SUCCESS | rc=0 >> total used free shared buffers cached Mem: 988 902 86 0 27 544 -/+ buffers/cache: 329 658 Swap: 1983 0 1983 192.168.100.128 | SUCCESS | rc=0 >> total used free shared buffers cached Mem: 1006 531 474 0 24 324 -/+ buffers/cache: 182 823 Swap: 1023 0 1023
4、使用分组复制上传文件
[root@zxw63 ~]# ansible webservers -m copy -a "src=/root/test.sh dest=/tmp/test.sh owner=root group=root mode=0755" 192.168.100.66 | SUCCESS => { "changed": true, "checksum": "9f55cd2a07f99f817ec13d7be09618578c38da4a", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "fc56aca29ab679337991a6714f0262bf", "mode": "0755", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 33, "src": "/root/.ansible/tmp/ansible-tmp-1562922897.07-223155038519772/source", "state": "file", "uid": 0 } 192.168.100.128 | SUCCESS => { "changed": true, "checksum": "9f55cd2a07f99f817ec13d7be09618578c38da4a", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "fc56aca29ab679337991a6714f0262bf", "mode": "0755", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 33, "src": "/root/.ansible/tmp/ansible-tmp-1562922897.08-10823376014870/source", "state": "file", "uid": 0 }
5、使用分组执行脚本
[root@zxw63 ~]# ansible webservers -m command -a "sh /tmp/test.sh" 192.168.100.66 | SUCCESS | rc=0 >> this is test shell-script 192.168.100.128 | SUCCESS | rc=0 >> this is test shell-script [root@zxw63 ~]# ansible webservers -m script -a "/tmp/test.sh" 192.168.100.66 | FAILED! => { "changed": false, "msg": "Could not find or access '/tmp/test.sh'" } 192.168.100.128 | FAILED! => { "changed": false, "msg": "Could not find or access '/tmp/test.sh'" } [root@zxw63 ~]# ansible webservers -m shell -a "/tmp/test.sh" 192.168.100.66 | SUCCESS | rc=0 >> this is test shell-script 192.168.100.128 | SUCCESS | rc=0 >> this is test shell-script
6、查看文件属性
[root@zxw63 ~]# ansible webservers -m stat -a "path=/tmp/test.sh" 192.168.100.66 | SUCCESS => { "changed": false, "stat": { "atime": 1562923035.5270104, "attr_flags": "e", "attributes": [ "extents" ], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "9f55cd2a07f99f817ec13d7be09618578c38da4a", "ctime": 1562922861.7570105, "dev": 2050, "device_type": 0, "executable": true, "exists": true, "gid": 0, "gr_name": "root", "inode": 135448, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "fc56aca29ab679337991a6714f0262bf", "mimetype": "text/plain", "mode": "0755", "mtime": 1562922861.4270105, "nlink": 1, "path": "/tmp/test.sh", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 33, "uid": 0, "version": "1217872402", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": true, "xoth": true, "xusr": true } } 192.168.100.128 | SUCCESS => { "changed": false, "stat": { "atime": 1562923052.5340095, "attr_flags": "e", "attributes": [ "extents" ], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "9f55cd2a07f99f817ec13d7be09618578c38da4a", "ctime": 1562922878.7960095, "dev": 2050, "device_type": 0, "executable": true, "exists": true, "gid": 0, "gr_name": "root", "inode": 16964, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "fc56aca29ab679337991a6714f0262bf", "mimetype": "text/plain", "mode": "0755", "mtime": 1562922878.4440095, "nlink": 1, "path": "/tmp/test.sh", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 33, "uid": 0, "version": "2428583384", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": true, "xoth": true, "xusr": true } }
7、安装应用包
ansible webservers -m yum -a "name=curl state=latest" 192.168.100.66 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package curl.x86_64 0:7.19.7-37.el6_4 will be updated ---> Package curl.x86_64 0:7.19.7-53.el6_9 will be an update --> Processing Dependency: libcurl = 7.19.7-53.el6_9 for package: curl-7.19.7-53.el6_9.x86_64 --> Running transaction check ---> Package libcurl.x86_64 0:7.19.7-37.el6_4 will be updated ---> Package libcurl.x86_64 0:7.19.7-53.el6_9 will be an update --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Updating: curl x86_64 7.19.7-53.el6_9 base 197 k Updating for dependencies: libcurl x86_64 7.19.7-53.el6_9 base 169 k Transaction Summary ================================================================================ Upgrade 2 Package(s) Total download size: 367 k Downloading Packages: -------------------------------------------------------------------------------- Total 576 kB/s | 367 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Updating : libcurl-7.19.7-53.el6_9.x86_64 1/4 Updating : curl-7.19.7-53.el6_9.x86_64 2/4 Cleanup : curl-7.19.7-37.el6_4.x86_64 3/4 Cleanup : libcurl-7.19.7-37.el6_4.x86_64 4/4 Verifying : libcurl-7.19.7-53.el6_9.x86_64 1/4 Verifying : curl-7.19.7-53.el6_9.x86_64 2/4 Verifying : libcurl-7.19.7-37.el6_4.x86_64 3/4 Verifying : curl-7.19.7-37.el6_4.x86_64 4/4 Updated: curl.x86_64 0:7.19.7-53.el6_9 Dependency Updated: libcurl.x86_64 0:7.19.7-53.el6_9 Complete! " ] } 192.168.100.128 | SUCCESS => { "changed": true, "msg": "warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Importing GPG key 0xC105B9DE: Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org> Package: centos-release-6-5.el6.centos.11.1.i686 (@anaconda-CentOS-201311271240.i386/6.5) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 ", "rc": 0, "results": [ "Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package curl.i686 0:7.19.7-37.el6_4 will be updated ---> Package curl.i686 0:7.19.7-53.el6_9 will be an update --> Processing Dependency: libcurl = 7.19.7-53.el6_9 for package: curl-7.19.7-53.el6_9.i686 --> Running transaction check ---> Package libcurl.i686 0:7.19.7-37.el6_4 will be updated ---> Package libcurl.i686 0:7.19.7-53.el6_9 will be an update --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Updating: curl i686 7.19.7-53.el6_9 base 197 k Updating for dependencies: libcurl i686 7.19.7-53.el6_9 base 176 k Transaction Summary ================================================================================ Upgrade 2 Package(s) Total download size: 374 k Downloading Packages: -------------------------------------------------------------------------------- Total 514 kB/s | 374 kB 00:00 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Updating : libcurl-7.19.7-53.el6_9.i686 1/4 Updating : curl-7.19.7-53.el6_9.i686 2/4 Cleanup : curl-7.19.7-37.el6_4.i686 3/4 Cleanup : libcurl-7.19.7-37.el6_4.i686 4/4 Verifying : curl-7.19.7-53.el6_9.i686 1/4 Verifying : libcurl-7.19.7-53.el6_9.i686 2/4 Verifying : curl-7.19.7-37.el6_4.i686 3/4 Verifying : libcurl-7.19.7-37.el6_4.i686 4/4 Updated: curl.i686 0:7.19.7-53.el6_9 Dependency Updated: libcurl.i686 0:7.19.7-53.el6_9 Complete! " ] }