当前位置:网站首页>【无标题】
【无标题】
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"
]
}
边栏推荐
猜你喜欢
随机推荐
npm基本操作及命令详解
Service Mesh落地路径
Laravel 实现redis分布式锁
Uniapp dynamic sliding navigation effect demo (finishing)
The Controller layer code is written like this, concise and elegant!
Nuclei(二)进阶——深入理解workflows、Matchers和Extractors
2022年华数杯数学建模
容联云发送短信验证码
typeScript-promise
407. 接雨水 II
Will we still need browsers in the future?(feat. Maple words Maple language)
文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
MySQL的JSON 数据类型1
web3.js
temp7777
七牛云图片上传
typeScript-闭包函数的使用
一点点读懂regulator(二)
Go 编程语言(简介)
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)