一、command模块
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) “执行什么动作”
command – Executes a command on a remote node
在一个远程主机上执行一个命令
简单用法
[root@ m01 ~]# ansible 172.168.3.41 -m command -a "hostname"
172.168.3.41 | CHANGED | rc=0 >>
backup
扩展应用:
1)、chdir change into this directory before runing the command.
[root@ m01 ~]# ansible 172.168.3.41 -m command -a "chdir=/tmp touch kitty.txt"
172.168.3.41 | CHANGED | rc=0 >>
切换查看
[root@ backup tmp]# ll
total 0
-rw-r--r--. 1 root root 0 May 21 15:16 kitty.txt
2)、creates If it already exists, this step won’t be run.
[root@ backup tmp]# ansible 172.168.3.41 -m command -a "creates=/tmp/hosts touch kitty.txt"
如果远端在tmp目录下存在hosts文件,则不执行touch的命令。
3)、removes If it already exists, this step will be run.
[root@ backup tmp]# ansible 172.168.3.41 -m command -a "removes=/tmp/hosts chdir=/tmp touch kitty.txt"
- 、free_form(required)
The command module takes a free form command to run.
There is no parameter actually named ‘free form’. See the examples!
使用command模块的时候,-a参数后面必须写上一个合法linux命令信息
ps: 有些符号信息无法识别: <", “>”, “|”, “;” and "&"
二、 shell (万能模块)
shell – Execute commands in nodes
在节点上执行操作
简单用法:
[root@ m01 ~]# ansible 172.168.3.41 -m shell -a "hostname"
172.168.3.41 | CHANGED | rc=0 >>
backup
和command一样的效果。
command有的,shell也有,属于万能模块,不必担心符号问题,想干嘛干嘛。
比如:
[root@ m01 ~]# ansible 172.168.3.41 -m shell -a "echo 123456 > /tmp/kitty.txt"
172.168.3.41 | CHANGED | rc=0 >>
切换查看
[root@ backup tmp]# cat kitty.txt
123456
利用shell执行脚本。
编写脚本
[root@ m01 ~]# vim yum.sh
#!/bin/bash
yum install -y htop
将脚本发送到远程主机
[root@ m01 scripts]# scp -rp yun.sh 172.168.3.41:/server/scripts
yun.sh 100% 33 24.4KB/s 00:00
切换查看
[root@ backup scripts]# ll
total 4
-rw-r--r--. 1 root root 33 May 21 15:52 yun.sh
[root@ m01 scripts]# ansible 172.168.3.41 -m shell -a "/server/scripts/yun.sh"
切换查看
[root@ backup scripts]# ps -ef|grep yun.sh
root 3510 3505 0 16:17 pts/1 00:00:00 /bin/bash /server/scripts/yun.sh
root 3531 2015 0 16:17 pts/0 00:00:00 grep --color=auto yun.sh
说明正在安装
最终安装完毕
[root@ m01 scripts]# ansible 172.168.3.41 -m shell -a "/server/scripts/yun.sh"
172.168.3.41 | CHANGED | rc=0 >>
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package htop.x86_64 0:2.2.0-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
htop x86_64 2.2.0-3.el7 epel 103 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 103 k
Installed size: 218 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : htop-2.2.0-3.el7.x86_64 1/1
Verifying : htop-2.2.0-3.el7.x86_64 1/1
Installed:
htop.x86_64 0:2.2.0-3.el7
Complete!
三、 script (万能模块)
第一个步骤: 编写一个脚本
第二个步骤: 运行ansible命令执行脚本
PS: scripts模块参数功能和command模块类似
四、copy模块
copy – Copies files to remote locations
将数据信息进行批量分发
基本用法:
[root@ m01 ~]# ansible 172.168.3.41 -m copy -a "src=/etc/hosts dest=/etc/hosts"
172.168.3.41 | CHANGED => { --- 对哪台主机进行操作
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true, --- 是否对主机信息进行改变
"checksum": "367c00ba8b02188b0c220d103fa1a982757821c6", --- 生成一个文件校验码==MD5数值
"dest": "/etc/hosts", --- 显示目标路径信息
"gid": 0, --- 显示复制后文件gid信息
"group": "root", --- 显示复制后文件属组信息
"md5sum": "137036c0fb18f19c8ea910b6bc26303a", --- 生成一个文件校验码==MD5数值
"mode": "0644", --- 显示复制后文件权限信息
"owner": "root", --- 显示复制后文件属主信息
"secontext": "system_u:object_r:net_conf_t:s0",
"size": 324, --- 显示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1621585546.43-2623-105719499142090/source",
"state": "file", --- 显示文件的类型信息
"uid": 0 --- 显示复制后文件uid信息
}
之前的主机hosts文件已经删除
切换查看验证
[root@ backup ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.168.3.5 lb01
172.168.3.6 lb02
172.168.3.7 web01
172.168.3.8 web02
172.168.3.9 web03
172.168.3.51 db01
172.168.3.31 nfs01
172.168.3.41 backup
172.168.3.61 m01
扩展用法
01. 在传输文件时修改文件的属主和属组信息
[root@ m01 ~]# ansible 172.168.3.41 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=rsync group=rsync"
02. 在传输文件时修改文件的权限信息
[root@ m01 ~]# ansible 172.168.3.41 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在传输数据文件信息时对远程主机源文件进行备份
[root@ m01 ~]# ansible 172.168.3.41 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 创建一个文件并直接编辑文件的信息
[root@ m01 ~]# ansible 172.168.3.41 -m copy -a "content='oldboy123' dest=/etc/rsync.password"
PS:
ansible软件copy模块复制目录信息
ansible 172.168.3.41 -m copy -a “src=/rsync dest=/rsync”
src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
ansible 172.168.3.41 -m copy -a “src=/rsync/ dest=/rsync”
src后面目录有/: 只将目录下面的内容都进行远程传输复制
五、file模块
file – Sets attributes of files
设置文件属性信息
基本用法:
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "dest=/etc/hosts owner=kitty group=kitty mode=666"
扩展用法:
1)、 可以利用模块创建数据信息 (文件 目录 链接文件)
state 参数
=absent — 缺席/删除数据信息
=directory — 创建一个目录信息
=file — 检查创建的数据信息是否存在 绿色存在 红色不存在
=hard — 创建一个硬链接文件
=link — 创建一个软链接文件
=touch — 创建一个文件信息
创建目录信息:
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "dest=/kitty/ state=directory"
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "dest=/kitty/kitty01/kitty02/ state=directory"
创建文件信息:
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "dest=/kitty/kitty.txt state=touch"
创建链接文件信息:
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "src=/kitty/kitty.txt dest=/kitty/kitty_hard.txt state=hard"
[root@ m01 ~]# ansible 172.168.3.41 -m file -a "src=/kitty/kitty.txt dest=/kitty/kitty_link.txt state=link"
2)、可以利用模块删除数据信息
[root@ m01 ~]#ansible 172.168.3.41 -m file -a "dest=/kitty/kitty.txt state=absent"
[root@ m01 ~]#ansible 172.168.3.41 -m file -a "dest=/kitty/ state=absent"