一、模块使用帮助
1.查看所有模块
[root@rsync-A ~]# ansible-doc -l
2.查看模块的参数
[root@rsync-A ~]# ansible-doc -s shell(shell为模块,通过上面命令可以查到)
二、常用模块
1.ping模块(查看所有节点是否连通)
[root@rsync-A ansible]# ansible all -m ping
10.0.0.131 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}
2.setup (收集远程主机的基本信息)
[root@rsync-A ~]# ansible web -m setup
10.0.0.130 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [###IPV4
"10.0.0.130"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:febe:b636"
],
"ansible_apparmor": {
"status": "disabled"
},
...
..
.
3.file (设置文件的属性)
选项:
force: 需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下
另一种是目录软链接已存在,需要先取消之前的链接,有两个选项yes|no
group: 定义文件/目录的属组
mode: 定义文件/目录的权限
owner: 定义文件/目录的属主
path; 必选项,定义文件/目录的路径
recurse:递归的设置文件属性,只对目录有效
src: 源文件的路径,只应用于state=link的情况
dest: 被链接到目的路径,只应用于setate=link的情况
state:
directory:如果目录不存在,创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard: 创建硬链接
touch:如果文件不存在的话,则会创建一个新的文件,如果文件或目录已存在,则更新时间戳
absent:删除目录、文件或者取消链接文件
示例:
3.1、创建链接文件
[root@rsync-A ~]# ansible web -m file -a "src=/etc/hosts dest=/tmp/hosts state=link"
10.0.0.130 | SUCCESS => {
"changed": true,
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 10,
"src": "/etc/hosts",
"state": "link",
"uid": 0
}
[root@rsync-A ~]# ansible web -a "ls /tmp"
10.0.0.130 | SUCCESS | rc=0 >>
ansible_qGJ8jb
c
d
hosts
rsync_fail_log.sh/
3.2、删除链接文件
[root@rsync-A ~]# ansible web -m file -a "path=/tmp/hosts state=absent"
10.0.0.130 | SUCCESS => {
"changed": true,
"path": "/tmp/hosts",
"state": "absent"
}
[root@rsync-A ~]# ansible web -a "ls /tmp"
10.0.0.130 | SUCCESS | rc=0 >>
ansible_RkXhi5
c
d
rsync_fail_log.sh/
3.3、创建文件testfile
[root@rsync-A ~]# ansible web -m file -a "path=/tmp/testfile state=touch"
10.0.0.130 | SUCCESS => {
"changed": true,
"dest": "/tmp/testfile",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@rsync-A ~]# ansible web -a "ls /tmp"
10.0.0.130 | SUCCESS | rc=0 >>
ansible_ZtoCal
c
d
rsync_fail_log.sh
testfile
3.4、创建目录testfir,属主root,属组root,权限755
[root@rsync-A ~]# ansible web -m file -a "path=/tmp/testdir state=directory owner=root group=root mode=755"
10.0.0.130 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/testdir",
"size": 4096,
"state": "directory",
"uid": 0
}
[root@rsync-A ~]# ansible web -a "ls -lh /tmp"
10.0.0.130 | SUCCESS | rc=0 >>
total 8.0K
drwx------ 2 root root 4.0K Jul 23 20:05 ansible_zU9byu
-rw-r--r-- 1 500 500 0 Jul 22 22:47 c
-rw-r--r-- 1 500 500 0 Jul 22 22:47 d
-rwxrwxrwx 1 root root 0 Jul 23 19:32 rsync_fail_log.sh
drwxr-xr-x 2 root root 4.0K Jul 23 20:04 testdir
-rw-r--r-- 1 root root 0 Jul 23 20:01 testfile/
4.copy (复制文件到远程主机)
选项:
backup:在覆盖前将原文件备份,备份文件包含时间信息。yes|no
content:用于替代“src”,可以直接设定文件的值
dest:必须选,要将源文件复制到远程主机的绝对路径
directory_mode:递归的设定目录权限,默认为系统默认权限
force:如果目标主机包含文件,但内容不同,如果选择yes,则强制覆盖,如果为no,则
只有当目标主机不存在该文件时,才复制。默认为yes
other:所有的file模块里的选项都可以在这里使用
src:要复制到远程主机的文件在本机的位置
示例:
4.1将本地的/etc/hosts发送的远程主机
[root@rsync-A ~]# ansible web -m copy -a "src=/etc/hosts dest=/opt/ mode=755 owner=root group=root"
10.0.0.130 | SUCCESS => {
"changed": true,
"checksum": "71620751afb9eda59339dd26852f9f11c75b7b72",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "e9c570af1bc8838311854522292fa20e",
"mode": "0755",
"owner": "root",
"size": 169,
"src": "/root/.ansible/tmp/ansible-tmp-1532390132.78-98197735349077/source",
"state": "file",
"uid": 0
}
[root@rsync-A ~]# ansible web -a "ls -l /opt"
10.0.0.130 | SUCCESS | rc=0 >>
total 4
-rwxr-xr-x 1 root root 169 Jul 23 20:20 hosts
4.2覆盖式拷贝
[root@rsync-A ~]# cat /root/a.txt
1 a
2 b
3 c
[root@rsync-A ~]# ansible web -m copy -a "src=/root/a.txt dest=/opt/hosts backup=yes"
10.0.0.130 | SUCCESS => {
"backup_file": "/opt/hosts.11746.2018-07-23@20:26:01~",
"changed": true,
"checksum": "5a85c9faf3d20368b2fdc03416c83c042551bb9f",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "be260596c29faa0bbe13679cbac194ae",
"mode": "0755",
"owner": "root",
"size": 27,
"src": "/root/.ansible/tmp/ansible-tmp-1532390456.19-186277784418304/source",
"state": "file",
"uid": 0
}
[root@rsync-A ~]# ansible web -a "ls -l /opt"
10.0.0.130 | SUCCESS | rc=0 >>
total 8
-rwxr-xr-x 1 root root 27 Jul 23 20:26 hosts
-rwxr-xr-x 1 root root 169 Jul 23 20:20 hosts.11746.2018-07-23@20:26:01~
[root@rsync-A ~]# ansible web -a "cat /opt/hosts"
10.0.0.130 | SUCCESS | rc=0 >>
1 a
2 b
3 c
[root@rsync-A ~]# ansible web -a "cat /opt/hosts.11746.2018-07-23@20:26:01~"
10.0.0.130 | SUCCESS | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 nfs-server
5.command (在远程主机上执行命令)
选项:
creates:一个文件名,先切换到该指定的目录
free_from:要执行的Linux指令
chdir:在执行指令前,先切换到该指定目录
remove:一个文件名,当该文件不存在,则该选项不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径
示例:
5.1切换目录执行命令
[root@rsync-A ~]# ansible web -a "chdir=/opt ls -l "
10.0.0.130 | SUCCESS | rc=0 >>
total 8
-rwxr-xr-x 1 root root 27 Jul 23 20:26 hosts
-rwxr-xr-x 1 root root 169 Jul 23 20:20 hosts.11746.2018-07-23@20:26:01~
6.shell
选项跟command的一样,用法也一样。
不同之处,shell支持管道。
示例:
[root@rsync-A ~]# ansible web -m shell -a "ps -ef|grep crond"
10.0.0.130 | SUCCESS | rc=0 >>
root 9575 1 0 12:18 ? 00:00:00 crond
root 12185 12184 0 20:40 pts/2 00:00:00 /bin/sh -c ps -ef|grep crond
root 12187 12185 0 20:40 pts/2 00:00:00 grep crond
7.service (用于管理服务)
选项:
arguments:给命令提供一些选项
enabled:是否开机启动,yes|no
name:必选项,服务名称
pattern:定义一个模式,如果通过指令来查看服务的状态时,没有响应,就会
通过ps指定在进程中根据该模式进行查找,如果匹配到了,则认为该服务在运行
runlevel:运行级别
sleep:如果执行restarted,在则stop和start之间沉睡几秒钟
state:对当前服务执行启动,停止,重启,重新加载等操作(started,stoped,restarted,reloaded)
示例:
7.1开启httpd服务,并设置开机自动启动。
[root@rsync-A ~]# ansible web -m service -a "name=httpd state=started enabled=yes"
10.0.0.130 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started"
}
8.cron (用于管理计划)
选项:
backup:对远程主机上的原任务计划内容修改之前做备份
cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户任务计划
day:日(1-31)
hour:小时(0-23)
minute:分钟(0-59)
weekday:周(0-6)
job:要执行的任务。依赖于state=present
name:该任务描述
special_time:指定什么时候执行
state:确认该任务计划是创建还是删除
user:以哪个用户的身份执行
示例:
8.1创建定时任务
[root@rsync-A ~]# ansible web -m cron -a "name='reboot system' hour=2 job='/sbin/reboot'"
10.0.0.130 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"reboot system"
]
}
[root@rsync-A ~]# ansible web -a "crontab -l"
10.0.0.130 | SUCCESS | rc=0 >>
\#Ansible: reboot system
* 2 * * * /sbin/reboot
8.2删除定时任务
[root@rsync-A ~]# ansible web -m cron -a "name='reboot system' hour=2 job='/sbin/reboot' state=absent"
10.0.0.130 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[root@rsync-A ~]# ansible web -a "crontab -l"
10.0.0.130 | SUCCESS | rc=0 >>
9.filesystem (在块设备上创建文件系统)
选项:
dev:目标块设备
force:在一个已有文件系统的设备上强制创建
fstype:文件系统的类型
opts:传递给mkfs命令的选项
10.yum (使用yum包管理器来管理软件包)
选项:
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启动某个源
enablerepo:启动某个源
list:
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包路径
state:状态(present,absent,latest)
示例:
1.
[root@rsync-A ansible]# ansible web -m yum -a "state=installed name=httpd"
10.0.0.130 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results":
...
..
.
11.user (管理用户)
home:
createhome:
group:
password:
name:
system:
remove:
state:
shell:
示例:
1.创建用户
[root@rsync-A ~]# ansible web -m user -a "createhome=yes home=/home/user01 password=000000 name=user01 state=present shell=/bin/bash"
10.0.0.130 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": true,
"group": 500,
"home": "/home/user01",
"name": "user01",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 500
}
[root@rsync-A ~]# ansible web -a "id user01"
10.0.0.130 | SUCCESS | rc=0 >>
uid=500(user01) gid=500(user01) groups=500(user01)
2.删除用户
[root@rsync-A ~]# ansible web -m user -a "name=user01 remove=yes state=absent"
10.0.0.130 | SUCCESS => {
"changed": true,
"force": false,
"name": "user01",
"remove": true,
"state": "absent"
}
12.group(管理组)
gid:
name:
state:
system:
示例
1.创建组
[root@rsync-A ~]# ansible web -m group -a "name=test01 gid=888 state=present"
10.0.0.130 | SUCCESS => {
"changed": true,
"gid": 888,
"name": "test01",
"state": "present",
"system": false
}
2.删除组
[root@rsync-A ~]# ansible web -m group -a "name=test01 state=absent"
10.0.0.130 | SUCCESS => {
"changed": true,
"name": "test01",
"state": "absent"
}