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


边栏推荐
- Four-digit display header design
- UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
- UE4 通过与其它Actor互动开门
- 机器学习概述
- 数据库设计的酸(ACID)碱(BASE)原则
- How to discover a valuable GameFi?
- How to solve the three major problems of bank data collection, data supplementary recording and index management?
- [8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
- 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
- bytebuffer put flip compact clear 方法演示
猜你喜欢

The production method of the powered small sailboat is simple, the production method of the electric small sailboat

Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure

The test salary is so high?20K just graduated
![[CISCN2019 South China Division]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)
[CISCN2019 South China Division]Web11

bytebuffer 使用demo

使用IDEA连接TDengine服务器
How to identify false evidence and evidence?

What is the function of industrial-grade remote wireless transmission device?

UE4 为子弹蓝图添加声音和粒子效果

Mini Program_Dynamic setting of tabBar theme skin
随机推荐
狗仔队:表面编辑多视点图像处理
Acid (ACID) Base (BASE) Principles for Database Design
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
Is the NPDP certificate high in gold content?Compared to PMP?
UE4 通过与其它Actor互动开门
关于sklearn库的安装
[BSidesCF 2019]Kookie
阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
虚证、实证如何鉴别?
Summary of common methods of arrays
Cron(Crontab)--use/tutorial/example
UE4 第一人称角色模板 添加蹲伏功能
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
Swing有几种常用的事件处理方式?如何监听事件?
How to solve complex distribution and ledger problems?
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
Bytebuffer put flip compact clear method demonstration
UE4 opens door via interaction (keyboard key)
Spark基础【介绍、入门WordCount案例】
token, jwt, oauth2, session parsing