一 点睛
Ansible提供了非常丰富的功能模块,包括Cloud(云计算)、 Commands(命令行)、Database(数据库)、Files(文件管理)、 Internal(内置功能)、Inventory(资产管理)、Messaging(消息队列)、Monitoring(监控管理)、Net Infrastructure(网络基础服务)、Network(网络管理)、Notification(通知管理)、Packaging(包管 理)、Source Control(版本控制)、System(系统服务)、Utilities(公 共服务)、Web Infrastructure(Web基础服务),等等。 模块默认存储目录为/usr/share/ansible/,存储结构以模块分类名作为目 录名,模块文件按分类存放在不同类别目录中。
命令行调用模块格式: ansible
获取远程webservers组主机的uptime信息格式的命令如下:
[root@localhost ansible]# ansible webservers -m command -a uptime
192.168.0.101 | SUCCESS | rc=0 >>
21:06:03 up 1:32, 4 users, load average: 0.00, 0.01, 0.05
192.168.0.102 | SUCCESS | rc=0 >>
21:06:05 up 1:26, 4 users, load average: 0.16, 0.05, 0.06
以上命令等价于:
[root@localhost ansible]# ansible webservers -a uptime
获得模块的帮助说明信息格式:ansible-doc<模块名>
举例:得到ping的帮助信息:
[root@localhost ansible]# ansible-doc ping
二 远程命令模块
1 功能
模块包括command、script、shell,都可以实现远程shell命令运 行。
command作为Ansible的默认模块,可以运行远程权限范围所有的 shell命令;
script功能是在远程主机执行主控端存储的shell脚本文件,相当于scp+shell组合;
shell功能是执行远程主机的shell脚本文件。
2 举例
[root@localhost ansible]# ansible webservers -m command -a "free -m"
192.168.0.101 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 993 328 129 7 534 501
Swap: 4095 0 4095
192.168.0.102 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 993 303 444 7 244 547
Swap: 4095 0 4095
ansible webservers -m script -a "/home/test.sh 12 34" # 脚本在主控端
ansible webservers -m shell -a "/home/test.sh" # 脚本在远程机上
三 copy模块
1 功能
实现主控端向目标主机拷贝文件,类似于scp的功能。
2 举例
以下示例实现拷贝/home/test.txt文件至webserver组目标主机/tmp/目 录下,并更新文件属主及权限(可以单独使用file模块实现权限的修改,格式为:path=/etc/foo.conf owner=foo group=foo mode=0644)。
[root@localhost home]# ansible webservers -m copy -a "src=/home/test.txt dest=/tmp/ owner=root group=root mode=0755"
192.168.0.101 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0755",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1551532651.3-225374543977495/source",
"state": "file",
"uid": 0
}
192.168.0.102 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0755",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1551532651.32-177194548932187/source",
"state": "file",
"uid": 0
}
四 stat模块
1 功能
获取远程文件状态信息,包括atime、ctime、mtime、md5、uid、 gid等信息。
2 举例
[root@localhost home]# ansible webservers -m stat -a "path=/tmp/test.txt"
192.168.0.102 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1551532653.465721,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 0,
"charset": "binary",
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"ctime": 1551532653.4667208,
"dev": 2051,
"device_type": 0,
"executable": true,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 67617399,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"mimetype": "inode/x-empty",
"mode": "0755",
"mtime": 1551532652.8067176,
"nlink": 1,
"path": "/tmp/test.txt",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"version": "1363455500",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
}
192.168.0.101 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1551532652.6626875,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 0,
"charset": "binary",
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"ctime": 1551532652.6636877,
"dev": 2051,
"device_type": 0,
"executable": true,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 3262611,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"mimetype": "inode/x-empty",
"mode": "0755",
"mtime": 1551532651.9646854,
"nlink": 1,
"path": "/tmp/test.txt",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"version": "308466043",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
}