当前位置:网站首页>【无标题】
【无标题】
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"
]
}
边栏推荐
猜你喜欢

功耗控制之DVFS介绍

The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?

Xiaohei leetcode surfing: 94. Inorder traversal of binary tree

一点点读懂Thremal(二)

Pytest学习-Fixture

Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account

truffle

xss总结

Day118.尚医通:订单列表、详情、支付
![[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)](/img/96/946bbef52bd017ac6142c6b7485a86.png)
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
随机推荐
现在学习次世代3D游戏建模还能找到高薪好工作吗
Pytest learning - fixtures
Implementing class target method exception using proxy object execution
uniapp horizontal tab (horizontal scrolling navigation bar) effect demo (organization)
【云原生 · Kubernetes】Kubernetes运维
从“草原牛”到“数字牛”:蒙牛的数字化转型之道
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
The Go Programming Language (Introduction)
Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors
安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
一点点读懂thermal(一)
【字符串函数内功修炼】strncpy + strncat + strncmp(二)
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
Shell编程之循环语句与函数的使用
MySQL基础篇【子查询】
MySQL的JSON 数据类型1
truffle
为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
temp7777