Ansible内置了丰富的模块供用户使用,但是经常使用到的模块却不多。本文主要记录了ansible的一些常用模块以及详细参数 、注意事项等 ,供大家学习。
ping
ping模块用来检查目标主机是否在线
示例:
[root@Ansible ~]# ansible defaults -m ping
172.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
命令模块
command
概要
命令模块:适合使用简单的命令,无法支持"<",">","|",";","&"等符号
官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module
参数 | 选项/默认值 | 释义 |
chdir | 在执行命令前,进入到指定目录中 | |
creates | 判断指定文件是否存在,如果存在,不执行后面的操作 | |
removes | 判断指定文件是否存在,如果存在,执行后面的操作 | |
free_form | 必须要输入一个合理的命令 |
备注:无法支持"<",">","|",";","&"等符号
示例:
[root@Ansible ~]# ansible defaults -m command -a "hostname"
172.168.1.3 | CHANGED | rc=0 >>
localhost.localdomain
chdir
[root@Ansible ~]# ansible defaults -m command -a "chdir=/data ls -l"
172.168.1.3 | CHANGED | rc=0 >>
total 0
drwxr-xr-x 2 root root 65 May 26 10:09 scripts
creates
[root@Ansible ~]# ansible defaults -m command -a "touch /data/test.txt creates=/data/test.txt"
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file
is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.
172.168.1.3 | CHANGED | rc=0 >>
removes
[root@Ansible ~]# ansible defaults -m command -a "rm -rf /data/test.txt removes=/data/test.txt"
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.
172.168.1.3 | CHANGED | rc=0 >>
shell模块
概要
类似command模块升级版—万能模块
官方文档:https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module
参数 | 选项/默认值 | 释义 |
chdir | 在执行命令前,进入到指定目录中 | |
creates | 判断指定文件是否存在,如果存在,不执行后面的操作 | |
removes | 判断指定文件是否存在,如果存在,执行后面的操作 | |
free_form | 必须要输入一个合理的命令 |
备注:可以使用"<",">","|",";","&"等符号特殊符号
示例:
[root@Ansible ~]# ansible defaults -m shell -a "uptime"
172.168.1.3 | CHANGED | rc=0 >>
09:28:27 up 33 days, 21:09, 2 users, load average: 0.00, 0.01, 0.05
注:其它参数参考command模块,使用方法一致
文件模块
copy
概要
主要用于将管理主机上的数据信息传送给多台主机
官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
参数 | 选项/默认值 | 释义 |
src | 指定将本地主机数据信息进行远程复制 | |
bakcup | no/yes | 默认数据复制到远程主机,会覆盖原有文件(yes 将源文件进行备份) |
content | 在文件中添加信息 | |
dest(required) | 将数据复制到远程节点的路径信息 | |
group | 设置文件属组用户信息 | |
mode | 设置文件权限 | |
owner | 设置文件属主用户信息 | |
remote_src | no/yes | 如果设置为yes,表示将远程主机上的数据进行移动操作如果设置为no,表示将管理主机上的数据进行分发操作 |
备注:(required)为必须使用的参数
* 为默认参数
copy模块在复制数据时,如果数据为软链接文件,会将链接指定源文件进行复制
修改权限时候,需要加0 例如:chmod 0644 0755
示例:
[root@Ansible ~]# ansible defaults -m copy -a "src=/etc/passwd dest=/tmp"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "23c774c3d7d75a3346358dfcbb8e5a5bcae80267",
"dest": "/tmp/passwd",
"gid": 0,
"group": "root",
"md5sum": "6a099b88677b9be622a2a349f262d0f3",
"mode": "0644",
"owner": "root",
"size": 1117,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1594691237.41-21243-206286804713882/source",
"state": "file",
"uid": 0
}
backup
[root@Ansible ~]# ansible defaults -m copy -a "src=/etc/passwd dest=/tmp bakcup=yes"
172.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"checksum": "23c774c3d7d75a3346358dfcbb8e5a5bcae80267",
"dest": "/tmp/passwd",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/tmp/passwd",
"size": 1117,
"state": "file",
"uid": 0
}
owner、group、mode
[root@Ansible ~]# ansible defaults -m copy -a "src=/etc/passwd dest=/tmp owner=test group=test mode=0644"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "23c774c3d7d75a3346358dfcbb8e5a5bcae80267",
"dest": "/tmp/passwd",
"gid": 1004,
"group": "test",
"mode": "0644",
"owner": "test",
"path": "/tmp/passwd",
"size": 1117,
"state": "file",
"uid": 1004
}
[root@localhost ~]# ll /tmp/
total 4
-rw-r--r-- 1 test test 1117 Jul 14 09:47 passwd
content
[root@Ansible ~]# ansible defaults -m copy -a "content=admin dest=/tmp/passwd"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d033e22ae348aeb5660fc2140aec35850c4da997",
"dest": "/tmp/passwd",
"gid": 1004,
"group": "test",
"md5sum": "21232f297a57a5a743894a0e4a801fc3",
"mode": "0644",
"owner": "test",
"size": 5,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1594691575.17-14988-207105987650487/source",
"state": "file",
"uid": 1004
}
[root@localhost ~]# cat /tmp/passwd
admin[root@localhost ~]#
注:content添加内容不会添加回车符
fetch
概要
抓取文件到管理机上
官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
参数 |
选项/默认值 |
释义 |
src(required) |
要获取的远程系统上的文件,必须是文件,而不是目录 |
|
dest |
用于保存文件的目录 |
示例:
[root@Ansible ~]# ansible defaults -m fetch -a "src=/tmp/passwd dest=/root"
172.168.1.3 | CHANGED => {
"changed": true,
"checksum": "d033e22ae348aeb5660fc2140aec35850c4da997",
"dest": "/root/172.168.1.3/tmp/passwd",
"md5sum": "21232f297a57a5a743894a0e4a801fc3",
"remote_checksum": "d033e22ae348aeb5660fc2140aec35850c4da997",
"remote_md5sum": null
}
[root@Ansible ~]# tree /root
/root
└── 172.168.1.3
└── tmp
└── passwd
2 directories, 1 file
file模块
一、概述
file 模块可以帮助我们完成一些对文件的基本操作。比如,创建文件或目录、删除文件或目录、修改文件权限等。
官方文档:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
二、常用参数
path参数:必须参数,用于指定要操作的文件或目录,在之前版本的ansible中,使用dest参数或者name参数指定要操作的文件或目录,为了兼容之前的版本,使用dest或name也可以。
state参数:此参数非常灵活,其对应的值需要根据情况设定。比如,我们想要在远程主机上创建/testdir/a/b目录,那么则需要设置path=/testdir/a/b,但是,我们无法从"/testdir/a/b"这个路径看出b是一个文件还是一个目录,ansible也同样无法单单从一个字符串就知道你要创建文件还是目录,所以,我们需要通过state参数进行说明。当我们想要创建的/testdir/a/b是一个目录时,需要将state的值设置为directory,"directory"为目录之意,当它与path结合,ansible就能知道我们要操作的目标是一个目录。同理,当我们想要操作的/testdir/a/b是一个文件时,则需要将state的值设置为touch。当我们想要创建软链接文件时,需将state设置为link。想要创建硬链接文件时,需要将state设置为hard。当我们想要删除一个文件时(删除时不用区分目标是文件、目录、还是链接),则需要将state的值设置为absent,"absent"为缺席之意,当我们想让操作的目标"缺席"时,就表示我们想要删除目标。
src参数:当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源。
force参数:当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件。不过强制创建链接文件分为三种情况。情况一:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。情况二:当要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。情况三:当要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件。
owner参数:用于指定被操作文件的属主,属主对应的用户必须在远程主机中存在,否则会报错。
group参数:用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错。
mode参数:用于指定被操作文件的权限,比如,如果想要将文件权限设置为"rw-r-x---",则可以使用mode=650进行设置,或者使用mode=0650,效果也是相同的。如果想要设置特殊权限,比如为二进制文件设置suid,则可以使用mode=4700。
recurse参数:当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性。
三、示例
1、在defaults主机上创建一个名为test.txt的文件,如果test.txt文件已经存在,则会更新文件的时间戳,与touch命令的作用相同。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test.txt state=touch"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
2、在defaults主机上创建一个名为/tmp/test的目录,如果/tmp/test目录已经存在,则不进行任何操作。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test state=directory"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test",
"size": 6,
"state": "directory",
"uid": 0
}
3、在defaults上为test.txt文件创建软链接文件,软链接名为testlink.txt,执行下面命令的时候,test.txt已经存在。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/testlink.txt state=link src=/tmp/test.txt"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/testlink.txt",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 13,
"src": "/tmp/test.txt",
"state": "link",
"uid": 0
}
4、在defaults上为test1文件创建硬链接文件,硬链接名为hardfile,执行下面命令的时候,test1已经存在。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/hardfile state=hard src=/tmp/test1"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/hardfile",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/tmp/test1",
"state": "hard",
"uid": 0
}
5、删除远程机器上的指定文件或目录。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test state=absent"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/test",
"state": "absent"
}
6、在创建文件或目录的时候指定属主,或者修改远程主机上的文件或目录的属主。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test state=touch owner=test"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "test",
"size": 0,
"state": "file",
"uid": 1004
}
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test owner=test"
172.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "test",
"path": "/tmp/test",
"size": 0,
"state": "file",
"uid": 1004
}
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test2 state=directory owner=test"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "test",
"path": "/tmp/test2",
"size": 6,
"state": "directory",
"uid": 1004
}
7、在创建文件或目录的时候指定属组,或者修改远程主机上的文件或目录的属组。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test3 state=touch group=test"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test3",
"gid": 1004,
"group": "test",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test3 group=test"
172.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 1004,
"group": "test",
"mode": "0644",
"owner": "root",
"path": "/tmp/test3",
"size": 0,
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test4 state=directory group=test"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1004,
"group": "test",
"mode": "0755",
"owner": "root",
"path": "/tmp/test4",
"size": 6,
"state": "directory",
"uid": 0
}
8、在创建文件或目录的时候指定权限,或者修改远程主机上的文件或目录的权限。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test.txt state=touch mode=0644"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test.txt mode=0644"
172.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/tmp/test.txt",
"size": 0,
"state": "file",
"uid": 0
}
9、当操作远程主机中的目录时,同时递归的将目录中的文件的属主属组都设置为test。
[root@Ansible ~]# ansible defaults -m file -a "path=/tmp/test state=directory owner=test group=test recurse=yes"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1004,
"group": "test",
"mode": "0755",
"owner": "test",
"path": "/tmp/test",
"size": 22,
"state": "directory",
"uid": 1004
}
安装模块
yum
概要
使用yum软件包管理器安装,升级,降级,删除和列出软件包和组。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html#yum-repository-module
参数 | 选项/默认值 | 释义 |
name(required) | 指定软件名称信息 | |
state | absent/removed | 将软件进行卸载(慎用) |
= | present/installed | 将软件进行安装 |
latest | 安装最新的软件 yum update |
示例:
# 安装httpd-tools软件包
[root@Ansible ~]# ansible defaults -m yum -a "name=httpd-tools state=installed"
# 卸载httpd-tools软件包
[root@Ansible ~]# ansible defaults -m yum -a "name=httpd-tools state=absent"
服务模块
service
概要
用于管理服务运行状态
官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
参数 |
选项/默认值 |
释义 |
enabled |
no/yes |
设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留 |
name (required) |
设置要启动/停止服务名称 |
|
state |
reloaded |
平滑重启 |
state |
restarted |
重启 |
state |
started |
启动 |
state |
stopped |
停止 |
示例:
[root@Ansible ~]# ansible defaults -m service -a "name=crond state=started enabled=yes"
get_url
get_url模块,该模块主要用于从http、ftp、https服务器上下载文件(类似于wget
选项:
url:指定要下载的文件的URL地址
示例:
# 下载Nginx软件包到/root目录
[root@Ansible ~]# ansible defaults -m get_url -a "url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/root"
172.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum_dest": null,
"checksum_src": "47b2c5ccd12e2a7088b03d629ff6b9ab18215180",
"dest": "/root/nginx-1.18.0.tar.gz",
"elapsed": 259,
"gid": 0,
"group": "root",
"md5sum": "b2d33d24d89b8b1f87ff5d251aa27eb8",
"mode": "0644",
"msg": "OK (1039530 bytes)",
"owner": "root",
"size": 1039530,
"src": "/home/admin/.ansible/tmp/ansible-tmp-1594706967.86-19847-208435713718024/tmpYG9vaG",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://nginx.org/download/nginx-1.18.0.tar.gz"
}
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
评论