当前位置:网站首页>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:往被控制主机上推送目录


边栏推荐
猜你喜欢

Feature preprocessing

Ali's local life's single-quarter revenue is 10.6 billion, Da Wenyu's revenue is 7.2 billion, and Cainiao's revenue is 12.1 billion

从企业的视角来看,数据中台到底意味着什么?

How to solve complex distribution and ledger problems?

mutillidae下载及安装

商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险

App rapid development and construction experience: the importance of small programs + custom plug-ins
![[BJDCTF2020]EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020]EasySearch
![[MRCTF2020] PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020] PYWebsite

iMedicalLIS监听程序(2)
随机推荐
[BSidesCF 2019]Kookie
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
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
Cron(Crontab)--使用/教程/实例
GC Gaode coordinate and Baidu coordinate conversion
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
bytebuffer 内部结构
工业级远距离无线传输装置的功能有哪些?
使用IDEA连接TDengine服务器
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
UE4 通过互动(键盘按键)开门
[MRCTF2020] Ezpop (detailed)
Feature preprocessing
[CISCN2019 South China Division]Web11
Some conventional routines of program development (1)
iMedicalLIS监听程序(2)
[BJDCTF2020]EasySearch
大学物理---质点运动学
什么是ASEMI光伏二极管,光伏二极管的作用
Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"