Ansible常用模块
1.command 默认模块
没啥卵用
[root@kk ~]# ansible test -m command -a "hostname"
web01-192.168.1.3 | CHANGED | rc=0 >>
hwf
2.shell万能模块
shell模块,在剧本中会反复执行,只有添加creates=xxx,指定文件存在则并不会执行,这种方式,才不会反复执行
chdir 在执行命令之前对目录进行切换
[root@kk ~]# ansible test -m shell -a "chdir=/ pwd" web01-192.168.1.3 | CHANGED | rc=0 >> / [root@kk ~]# ansible test -m shell -a "chdir=/tmp pwd" web01-192.168.1.3 | CHANGED | rc=0 >> /tmp
creates 如果文件存在不执行命令操作
[root@kk ~]# ansible test -m shell -a "creates=/tmp pwd"
web01-192.168.1.3 | SUCCESS | rc=0 >>
skipped, since /tmp exists
removes 如果文件存在将执行命令操作
[root@kk ~]# ansible test -m shell -a "removes=/tmp chdir=/tmp pwd"
web01-192.168.1.3 | CHANGED | rc=0 >>
/tmp
3.script 远程运行脚本模块
使用这个模块,运行脚本的时候不需要添加x权限,而使用shell模块的时候执行脚本需要添加x权限
模块是将本地脚本先拷贝一份到目标主机上去执行,执行完成后再将目标主机上的拷贝删除。
[root@kk ~]# ansible test -m script -a "xx.sh"
web01-192.168.1.3 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.1.3 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.1.3 closed."
],
"stdout": "123\r\n",
"stdout_lines": [
"123"
]
}
4.copy 文件分发模块
将数据文件批量分发,可以创建目录,文件,但是无法递归创建太多目录,创目录的时候不要创文件
src 管理主机哪个文件发过去
这里要强调
src=/文件/ 代表把此目录内的所有文件发送
src=/文件 代表把目录本身和目录内的文件一起发送
dest 发送到哪个目录下
[root@kk ~]# ansible test -m copy -a "src=xx.sh dest=~/"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d45377114808328ac16dce46c5e8cc2dd8f2fdcd",
"dest": "/root/xx.sh",
"gid": 0,
"group": "root",
"md5sum": "eff4223f17d6e1da0f0582df9af6e818",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 28,
"src": "/root/.ansible/tmp/ansible-tmp-1590934824.31-46417-193309249526457/source",
"state": "file",
"uid": 0
}
owner 更换文件属主信息
group 更改文件属组信息
[root@kk ~]# ansible test -m copy -a "src=xx.sh owner=kk group=kk dest=~/"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d45377114808328ac16dce46c5e8cc2dd8f2fdcd",
"dest": "/root/xx.sh",
"gid": 1000,
"group": "kk",
"mode": "0644",
"owner": "kk",
"path": "/root/xx.sh",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 28,
"state": "file",
"uid": 1000
}
[root@hwf ~]# ll total 12 -rw-------. 1 root root 1993 May 17 19:44 anaconda-ks.cfg -rw-r--r--. 1 root root 2041 May 17 19:46 initial-setup-ks.cfg -rw-r--r--. 1 kk kk 28 May 31 22:28 xx.sh
mode 修改文件权限
backup 备份原文件数据
backup=yes 备份生效,默认为no
[root@kk ~]# ansible test -m copy -a "src=xx.sh mode=777 dest=~/ backup=yes"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d45377114808328ac16dce46c5e8cc2dd8f2fdcd",
"dest": "/root/xx.sh",
"gid": 1000,
"group": "kk",
"mode": "0777",
"owner": "kk",
"path": "/root/xx.sh",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 28,
"state": "file",
"uid": 1000
}
remote_src 就是在被管理端寻找文件
[root@kk ~]# ansible test -m copy -a "src=~/xx.sh dest=/tmp remote_src=yes"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d45377114808328ac16dce46c5e8cc2dd8f2fdcd",
"dest": "/tmp/xx.sh",
"gid": 0,
"group": "root",
"md5sum": "eff4223f17d6e1da0f0582df9af6e818",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 28,
"src": "/root/xx.sh",
"state": "file",
"uid": 0
}
5.file 文件操作模块
path 指定文件
group 修改属组信息
owner 修改属主信息
mode 修改文件权限
[root@kk ~]# ansible test -m file -a "path=~/xx.sh mode=000 group=kk owner=kk"
web01-192.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 1000,
"group": "kk",
"mode": "0000",
"owner": "kk",
"path": "/root/xx.sh",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 28,
"state": "file",
"uid": 1000
}
state 状态信息
- touch 创建空文件
- hard 创建硬链接
- link 创建软连接
- directroy 创建目录
- absent 删除
src 指定文件
dest 链接文件
在管理端操作被管理端建立硬链接(软链接方法一致)
[root@kk ~]# ansible test -m file -a "src=~/xx.sh dest=~/a state=hard"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root/a",
"gid": 1000,
"group": "kk",
"mode": "0000",
"owner": "kk",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 28,
"src": "/root/xx.sh",
"state": "hard",
"uid": 1000
}
在管理端操作删除被管理端的文件
[root@kk ~]# ansible test -m file -a "path=~/a state=absent"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/root/a",
"state": "absent"
}
创建目录
[root@kk ~]# ansible test -m file -a "path=kk state=directory"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "kk",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
recurse=yes 递归
递归修改目录权限,和递归创建目录
[root@kk ~]# ansible test -m file -a "path=kk/ww state=directory recurse=yes owner=kk group=kk mode=777"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"group": "kk",
"mode": "0777",
"owner": "kk",
"path": "kk/ww",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 1000
}
6.fetch 批量拉去模块
copy是推文件,fetch是拉文件
src 要拉取的文件
dest 放到管理端的哪里
这里拉去过来的文件,会自动生成一个hosts里别名的目录,就在那里面
[root@kk ~]# ansible test -m fetch -a "src=~/xx.conf dest=./"
web01-192.168.1.3 | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/web01-192.168.1.3/root/xx.conf",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"remote_md5sum": null
}
7.yum 下载模块
name 软件名
state 状态信息
- =installed 下载
- =absented 卸载软件包
- =latest 下载最新版本
[root@kk ~]# ansible test -m yum -a "name=vim state=installed"
web01-192.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"2:vim-enhanced-7.4.160-2.el7.x86_64 providing vim is already installed"
]
}
8.service /systemd启动模块
name 服务名称
state 状态信息
- started
- restarted
- stopped
enabled 开机自启选项
- yes
- no
[root@kk ~]# ansible test -m service -a "name=httpd state=stopped"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Mon 2020-06-01 00:48:42 CST",
"ActiveEnterTimestampMonotonic": "72383547755",
"ActiveExitTimestampMonotonic": "0",
systemd
- name: 服务管理
systemd:
name=etcd
state=restarted
enabled=yes
daemon_reload=yes
9.cron 定时任务模块
批量设置多个主机定时任务
minute 分
- =0-59,*,*/1
hour 小时
- =0-23,*,*/1
day 天
- =1-31,*,*/1
month 月
- =1-12,*,*/1
weekday
- 0-6,*,*/1
name 注释必须写
job 定时任务要执行的命令
[root@kk ~]# ansible test -m cron -a "name='123' minute=00 hour=3 day=5 month=3-5 weekday=* job='/usr/bin/bash ~/xx.sh &>/dev/null'"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"123"
]
}
[root@hwf ~]# crontab -l
#Ansible: 123
00 3 5 3-5 * /usr/bin/bash ~/xx.sh &>/dev/null
state 状态信息
- absent 删除定时任务
[root@kk ~]# ansible test -m cron -a "name='123' state=absent"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
[root@hwf ~]# crontab -l [root@hwf ~]#
disabled #批量注释定时任务
- =yes
[root@kk ~]# ansible test -m cron -a "name='123' job='/usr/bin/bash ~/xx.sh &>/dev/null' disabled=yes"
web01-192.168.1.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"123"
]
}
[root@hwf ~]# crontab -l
#Ansible: 123
#* * * * * /usr/bin/bash ~/xx.sh &>/dev/null
10.mount批量挂载模块
src 你要挂载的目录
path 你要挂到哪里
fstype 指定挂在类型
opts 权限 default
state 状态信息
- =mounted 永久挂载
- =absent 卸载
ansible nfs_server -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted"
卸载
ansible nfs_server -m mount -a " path=/mnt fstype=nfs state=absent"
- debug:
msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
- name: 显示主机已知的所有变量
debug:
var: hostvars[inventory_hostname]
verbosity: 4

评论