ansible使用简单说明

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\nLoading mirror speeds from cached hostfile\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package curl.x86_64 0:7.19.7-37.el6_4 will be updated\n---> Package curl.x86_64 0:7.19.7-53.el6_9 will be an update\n--> Processing Dependency: libcurl = 7.19.7-53.el6_9 for package: curl-7.19.7-53.el6_9.x86_64\n--> Running transaction check\n---> Package libcurl.x86_64 0:7.19.7-37.el6_4 will be updated\n---> Package libcurl.x86_64 0:7.19.7-53.el6_9 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch            Version                   Repository     Size\n================================================================================\nUpdating:\n curl             x86_64          7.19.7-53.el6_9           base          197 k\nUpdating for dependencies:\n libcurl          x86_64          7.19.7-53.el6_9           base          169 k\n\nTransaction Summary\n================================================================================\nUpgrade       2 Package(s)\n\nTotal download size: 367 k\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           576 kB/s | 367 kB     00:00     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Updating   : libcurl-7.19.7-53.el6_9.x86_64                               1/4 \n\r  Updating   : curl-7.19.7-53.el6_9.x86_64                                  2/4 \n\r  Cleanup    : curl-7.19.7-37.el6_4.x86_64                                  3/4 \n\r  Cleanup    : libcurl-7.19.7-37.el6_4.x86_64                               4/4 \n\r  Verifying  : libcurl-7.19.7-53.el6_9.x86_64                               1/4 \n\r  Verifying  : curl-7.19.7-53.el6_9.x86_64                                  2/4 \n\r  Verifying  : libcurl-7.19.7-37.el6_4.x86_64                               3/4 \n\r  Verifying  : curl-7.19.7-37.el6_4.x86_64                                  4/4 \n\nUpdated:\n  curl.x86_64 0:7.19.7-53.el6_9                                                 \n\nDependency Updated:\n  libcurl.x86_64 0:7.19.7-53.el6_9                                              \n\nComplete!\n"
    ]
}
192.168.100.128 | SUCCESS => {
    "changed": true, 
    "msg": "warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY\nImporting GPG key 0xC105B9DE:\n Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>\n Package: centos-release-6-5.el6.centos.11.1.i686 (@anaconda-CentOS-201311271240.i386/6.5)\n From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\n", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror, refresh-packagekit, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.163.com\n * extras: mirrors.163.com\n * updates: mirrors.163.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package curl.i686 0:7.19.7-37.el6_4 will be updated\n---> Package curl.i686 0:7.19.7-53.el6_9 will be an update\n--> Processing Dependency: libcurl = 7.19.7-53.el6_9 for package: curl-7.19.7-53.el6_9.i686\n--> Running transaction check\n---> Package libcurl.i686 0:7.19.7-37.el6_4 will be updated\n---> Package libcurl.i686 0:7.19.7-53.el6_9 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch          Version                     Repository     Size\n================================================================================\nUpdating:\n curl             i686          7.19.7-53.el6_9             base          197 k\nUpdating for dependencies:\n libcurl          i686          7.19.7-53.el6_9             base          176 k\n\nTransaction Summary\n================================================================================\nUpgrade       2 Package(s)\n\nTotal download size: 374 k\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           514 kB/s | 374 kB     00:00     \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Updating   : libcurl-7.19.7-53.el6_9.i686                                 1/4 \n\r  Updating   : curl-7.19.7-53.el6_9.i686                                    2/4 \n\r  Cleanup    : curl-7.19.7-37.el6_4.i686                                    3/4 \n\r  Cleanup    : libcurl-7.19.7-37.el6_4.i686                                 4/4 \n\r  Verifying  : curl-7.19.7-53.el6_9.i686                                    1/4 \n\r  Verifying  : libcurl-7.19.7-53.el6_9.i686                                 2/4 \n\r  Verifying  : curl-7.19.7-37.el6_4.i686                                    3/4 \n\r  Verifying  : libcurl-7.19.7-37.el6_4.i686                                 4/4 \n\nUpdated:\n  curl.i686 0:7.19.7-53.el6_9                                                   \n\nDependency Updated:\n  libcurl.i686 0:7.19.7-53.el6_9                                                \n\nComplete!\n"
    ]
}

 

posted @ 2019-07-12 17:45  熊熊闯深林  阅读(2141)  评论(0编辑  收藏  举报