当前位置:网站首页>【无标题】
【无标题】
2022-08-04 23:20:00 【我是渣渣辉】
1.command,shell,raw,script模块的作用和区别
command、shell模块:
相同点:要求受管主机上安装Python。
不同点:command可以在受管主机上执行shell命令,但是不支持环境变量和操作符(例如 ‘|’, ‘<’, ‘>’, ‘&’)
shell模块调用的/bin/sh指令执行。
raw模块:
不需要受管主机上安装Python,直接使用远程shell运行命令,通常用于无法安装Python的系统(例如网络设备等)。
command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo “I am command module”
[[email protected] ~]# ansible test -m command -a "bash command chdir=/root"
node2.example.com | CHANGED | rc=0 >>
I am command module
shell模块执行命令 ls /root | grep txt
[[email protected] ~]# ansible test -m shell -a "ls /root | grep txt"
node2.example.com | CHANGED | rc=0 >>
file.txt
raw模块执行pwd命令
[[email protected] ~]# ansible test -m shell -a "pwd"
node2.example.com | CHANGED | rc=0 >>
/home/student
script模块执行 script.sh文件,文件的内容为 echo “I am script module”
[[email protected] ~]# ansible test -m script -a "/root/script.sh"
node2.example.com | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to node2.example.com closed.\r\n",
"stderr_lines": [
"Shared connection to node2.example.com closed."
],
"stdout": "I am script module\r\n",
"stdout_lines": [
"I am script module"
]
}
2.file模块:
创建文件,并指定用户,用户组为student, 且权限为600
[[email protected] ~]# ansible test -m file -a "path=/root/file2 state=touch owner=student group=student mode=600 "
node2.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/root/file2",
"gid": 1001,
"group": "student",
"mode": "0600",
"owner": "student",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 0,
"state": "file",
"uid": 1001
}
创建目录,并指定用户,用户组为student, 且权限为755
[[email protected] ~]# ansible test -m file -a "path=/root/dir state=directory owner=student group=student mode=755 "
node2.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1001,
"group": "student",
"mode": "0755",
"owner": "student",
"path": "/root/dir",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 1001
}
创建链接文件
[[email protected] ~]# ansible test -m file -a "path=/root/file3 src=/root/file1 state=link"
node2.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/root/file3",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 11,
"src": "/root/file1",
"state": "link",
"uid": 0
}
删除第一个创建的文件
[[email protected] ~]# ansible test -m file -a "path=/root/file1 state=absent"
node2.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/root/file1",
"state": "absent"
}
3.copy
复制文件
[[email protected] ~]# ansible test -m copy -a "src=/root/file1 dest=/root/"
node2.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/file1",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 0,
"src": "/home/student/.ansible/tmp/ansible-tmp-1659622665.9384096-11300-258171013076166/source",
"state": "file",
"uid": 0
}
复制目录
[[email protected] ~]# ansible test -m copy -a "src=/root/dir1 dest=/root/"
node2.example.com | SUCCESS => {
"changed": false,
"dest": "/root/",
"src": "/root/dir1"
}
4.fetch
从被控制主机上取文件
[[email protected] ~]# ansible test -m fetch -a "src=/root/file.txt dest=/root/ flat=yes"
node2.example.com | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/file.txt",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"remote_md5sum": null
}
5.synchronize
pull: 从被控制主机上拉取目录
[[email protected] ~]# ansible test -m synchronize -a "src=/root/file3 dest=/root/ mode=pull"
node2.example.com | CHANGED => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --rsync-path='sudo -u root rsync' --out-format='<<CHANGED>>%i %n%L' [email protected]:/root/file3 /root/",
"msg": "cL+++++++++ file3 -> /root/file1\n",
"rc": 0,
"stdout_lines": [
"cL+++++++++ file3 -> /root/file1"
]
}
push:往被控制主机上推送目录
[[email protected] ~]# ansible test -m synchronize -a "src=/root/2 dest=/root/ mode=push"
node2.example.com | CHANGED => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --rsync-path='sudo -u root rsync' --out-format='<<CHANGED>>%i %n%L' /root/2 [email protected]:/root/",
"msg": "<f+++++++++ 2\n",
"rc": 0,
"stdout_lines": [
"<f+++++++++ 2"
]
}
边栏推荐
猜你喜欢
随机推荐
Linear DP (bottom)
uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
Service Mesh落地路径
软件测试技术之如何编写测试用例(4)
The Go Programming Language (Introduction)
enumerate()函数
一点点读懂regulator(二)
一点点读懂cpufreq(二)
使用代理对象执行实现类目标方法异常
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
365天深度学习训练营-学习线路
对“为什么一些程序员很傲慢”的解读
ClickHouse 二级索引
uniapp sharing function - share to friends group chat circle of friends effect (sorting)
【七夕快乐篇】Nacos是如何实现服务注册功能的?
话题 | 雾计算和边缘计算有什么区别?
Pytest学习-Fixture
现在学习次世代3D游戏建模还能找到高薪好工作吗
轮播图动态渲染
线性DP(下)



![[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)](/img/b6/5a1c8b675dc7f67f359c25908403e1.png)





