1.command,shell,raw,script模块的作用和区别
command 模块:
command模块不是调用的shell的指令,所以没有bash的环境变量,也不能使用shell的一些操作方式,其他和shell没有区别
shell 模块:
shell模块 [执行远程主机的shell/python脚本] shell模块调用的/bin/sh指令执行,
raw 模块:
raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了,支持管道传递
script 模块:
script模块 [在远程主机执行主控端的shell/python脚本 ] (使用相对路径)
command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo “I am command module”
[root@node1 ~]# vim command.sh
echo "i am command module"
[root@node1 ~]# chmod +x command.sh
[root@node1 ~]# ll command.sh
-rwxr-xr-x. 1 root root 27 Aug 7 22:29 command.sh
[root@server ~]# ansible node1.example.com -m command -a "sh /root/command.sh chdir=/root"
node1.example.com | CHANGED | rc=0 >>
i am command module
shell模块执行命令 ls /root | grep txt raw模块执行pwd命令 script模块执行 script.sh文件,文件的内容为 echo “I am script module”
[root@server ~]# ansible node1.example.com -m shell -a "ls /root | grep cfg"
node1.example.com | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
[root@server ~]# ansible node1.example.com -m raw -a "pwd"
node1.example.com | CHANGED | rc=0 >>
/root
Shared connection to node1.example.com closed.
[root@server ~]# vim script.sh
echo "I am script module"
[root@server ~]# chmod +x script.sh
[root@server ~]# ansible node1.example.com -m script -a "script.sh chdir=/root"
node1.example.com | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to node1.example.com closed.\r\n",
"stderr_lines": [
"Shared connection to node1.example.com closed."
],
"stdout": "i am script module\r\n",
"stdout_lines": [
"i am script module"
]
}
2.file模块:
创建文件,并指定用户,用户组为student, 且权限为600
[root@server ~]# ansible node2.example.com -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
[root@server ~]# ansible node2.example.com -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
}
创建链接文件
[root@server ~]# ansible node2.example.com -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
}
删除第一个创建的文件
[root@server ~]# ansible node2.example.com -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
复制文件
[root@server ~]# ansible node2.example.com -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
}
复制目录
[root@server ~]# ansible node2.example.com -m copy -a "src=/root/dir1 dest=/root/"
node2.example.com | SUCCESS => {
"changed": false,
"dest": "/root/",
"src": "/root/dir1"
}
4.fetch
从被控制主机上取文件
[root@server ~]# ansible node2.example.com -m fetch -a "src=/root/file1 dest=/root/ flat=yes"
node2.example.com | CHANGED => {
"changed": true,
"checksum": "e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
"dest": "/root/file1",
"md5sum": "b026324c6904b2a9cb4b88d6d61c81d1",
"remote_checksum": "e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
"remote_md5sum": null
}
5.synchronize
pull: 从被控制主机上拉取目录
[root@server ~]# ansible node2.example.com -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' student@node2.example.com:/root/file3 /root/",
"msg": "cL+++++++++ file3 -> /root/file1\n",
"rc": 0,
"stdout_lines": [
"cL+++++++++ file3 -> /root/file1"
]
}
push:往被控制主机上推送目录
[root@server ~]# ansible node2.example.com -m synchronize -a "src=/root/file2 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/file2 student@node2.example.com:/root/",
"msg": "<f+++++++++ file2 \n",
"rc": 0,
"stdout_lines": [
"<f+++++++++ file2 "
]
}