当前位置:网站首页>ansible各个模块详解
ansible各个模块详解
2022-08-05 04:15:00 【cy11114】
1.command,shell,raw,script模块的作用和区别
1.1command shell raw区别
command、shell模块:
相同点:要求受管主机上安装Python。
不同点:command可以在受管主机上执行shell命令,但是不支持环境变量和操作符(例如 '|', '<', '>', '&')
shell模块调用的/bin/sh指令执行。
raw模块:
不需要受管主机上安装Python,直接使用远程shell运行命令,通常用于无法安装Python的系统(例如网络设备等)。
- command
- shell
- raw
应尽量避免使用这三个模块来执行命令,因为其他模块大部分都是幂等性的,可以自动进行更改跟踪。command、shell、raw不具备幂等性。注: 幂等性:简单来讲,就是输入相同,输出相同,无论多少次比如说,确认接口,如果传入订单号,返回确认OK,如果已经确认过了,再次调用确认接口,返回如果还是确认OK,那么这个接口就是满足幂等性.
2、command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo "I am command module"
[[email protected] ~]#vim command.sh
[[email protected] ~]# more ./.ansible/inventory
node1
[node]
node1
[node1:children]
node
test
[test]
node2
[[email protected] ~]# ansible node -m copy -a "src=/root/command.sh dest=/home/student"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "ce65dc39709980daa42a03e5d2ac1c97210446db",
"dest": "/home/student/command.sh",
"gid": 0,
"group": "root",
"md5sum": "5f4164eeeea65e4697bdb62d078065bb",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 27,
"src": "/home/student/.ansible/tmp/ansible-tmp-1659481931.964826-27384-280168700260751/source",
"state": "file",
"uid": 0
}
[[email protected] ~]# ansible node -m command -a "ls -l /home/student/"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 27 Aug 2 12:33 command.sh
[[email protected] ~]# ansible node -m command -a "sh command.sh chdir=/home/student"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED | rc=0 >>
I am command module
3、shell模块执行命令 ls /root | grep txt
4、 raw模块执行pwd命令
5、script模块执行 script.sh文件,文件的内容为 echo "I am script module"
[[email protected] ~]# vim script.sh
[[email protected] ~]# pwd
/root
[[email protected] ~]# ansible node -m copy -a "src=/root/script.sh dest=/home/student"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "7fe9ae1b68ec686919b7799f9eef6c7eff006c8c",
"dest": "/home/student/script.sh",
"gid": 0,
"group": "root",
"md5sum": "ffb843f4c7a3cc3a444c21a7bdc2b2ac",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 26,
"src": "/home/student/.ansible/tmp/ansible-tmp-1659482546.7581532-27525-45068860098080/source",
"state": "file",
"uid": 0
}
[[email protected] ~]# ansible node -m command -a "ls -l /home/student"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED | rc=0 >>
total 8
-rw-r--r--. 1 root root 27 Aug 2 12:33 command.sh
-rw-r--r--. 1 root root 26 Aug 2 12:43 script.sh
[[email protected] ~]# ansible node -m script -a "script.sh chdir=/home/student"
[WARNING]: Found both group and host with same name: node1
node1 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to node1 closed.\r\n",
"stderr_lines": [
"Shared connection to node1 closed."
],
"stdout": "I am script module\r\n",
"stdout_lines": [
"I am script module"
]
6.file模块:
创建文件,并指定用户,用户组为student, 且权限为600
创建目录,并指定用户,用户组为student, 且权限为755
创建链接文件
删除第一个创建的文件
7.copy
复制文件
复制目录
8.synchronize
pull: 从被控制主机上拉取目录
push:往被控制主机上推送目录
边栏推荐
- 数据库设计的酸(ACID)碱(BASE)原则
- Detailed explanation of Mysql's undo log
- 浅析主流跨端技术方案
- Cron(Crontab)--use/tutorial/example
- Redis key basic commands
- Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"
- 从企业的视角来看,数据中台到底意味着什么?
- 商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
- 事件解析树Drain3使用方法和解释
- 【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
猜你喜欢
UE4 为子弹蓝图添加声音和粒子效果
从企业的视角来看,数据中台到底意味着什么?
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
[MRCTF2020] PYWebsite
日志导致线程Block的这些坑,你不得不防
[SWPU2019]Web1
Defect detection (image processing part)
UE4 通过重叠事件开启门
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
36-Jenkins-Job Migration
随机推荐
七夕节赚徽章拉
[MRCTF2020]PYWebsite
How to wrap markdown - md file
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
token, jwt, oauth2, session parsing
七夕节代码表白
[GYCTF2020]EasyThinking
程序开发的一些常规套路(一)
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
bytebuffer 使用demo
What is the function of industrial-grade remote wireless transmission device?
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
UE4 第一人称角色模板 添加冲刺(加速)功能
Mysql的redo log详解
Mini Program_Dynamic setting of tabBar theme skin
关于sklearn库的安装
UE4 通过重叠事件开启门
JeeSite New Report
JeeSite新建报表