command、 shell 模块:
相同点:要求受管主机上安装 Python。
不同点: command 可以在受管主机上执行 shell 命令, 但是不支持环境变量和操
作符(例如 '|', '<', '>', '&')
shell 模块调用的/bin/sh 指令执行。
raw 模块:
不需要受管主机上安装 Python,直接使用远程 shell 运行命令,通常用于无法安装
Python 的系统(例如网络设备等)。
- [root@rhel ~]# cat command.sh
- echo"I am command module"
- [root@rhcsa ~]# ansible rhel -m command -a "sh command.sh chdir=/root"
- rhel | CHANGED | rc=0 >>
- I am command module
- [root@rhcsa ~]# ansible rhel -m shell -a "ls /root | grep cfg"
- rhel | CHANGED | rc=0 >>
- anaconda-ks.cfg
- initial-setup-ks.cfg
- [root@rhcsa ~]# ansible rhel -m raw -a "pwd"
- rhel | CHANGED | rc=0 >>
- /home/student
- Shared connection to rhel closed.
-
[root@rhcsa ~]# echo "echo "I am script module"" > script.sh
- [root@rhcsa ~]# ansible rhel -m script -a "script.sh free_form=/root"
- rhel | CHANGED => {
- "changed": true,
- "rc": 0,
- "stderr": "Shared connection to rhel closed.\r\n",
- "stderr_lines": [
- "Shared connection to rhel closed."
- ],
- "stdout": "I am script module\r\n",
- "stdout_lines": [
- "I am script module"
- ]
- }
- [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt owner=student group=student mode=600 state=touch"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dest": "/home/student/test2.txt",
- "gid": 2002,
- "group": "student",
- "mode": "0600",
- "owner": "student",
- "secontext": "unconfined_u:object_r:user_home_t:s0",
- "size": 0,
- "state": "file",
- "uid": 2002
- }
- [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test_dir owner=student group=student mode=755 state=directory"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "gid": 2002,
- "group": "student",
- "mode": "0755",
- "owner": "student",
- "path": "/home/student/test_dir",
- "secontext": "unconfined_u:object_r:user_home_t:s0",
- "size": 6,
- "state": "directory",
- "uid": 2002
- }
- [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test4.txt src=test2.txt state=link"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "dest": "/home/student/test4.txt",
- "gid": 0,
- "group": "root",
- "mode": "0777",
- "owner": "root",
- "secontext": "unconfined_u:object_r:user_home_t:s0",
- "size": 9,
- "src": "test2.txt",
- "state": "link",
- "uid": 0
- }
- [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt state=absent"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "path": "/home/student/test2.txt",
- "state": "absent"
- }
- [root@rhcsa ~]# ansible rhel -m copy -a "src=/root/test.txt dest=/home/student"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
- "dest": "/home/student/test.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
- "mode": "0644",
- "owner": "root",
- "secontext": "unconfined_u:object_r:user_home_t:s0",
- "size": 0,
- "src": "/home/student/.ansible/tmp/ansible-tmp-1659519729.6693015-3382-265136890969898/source",
- "state": "file",
- "uid": 0
- }
- [root@rhcsa test_dir]# ansible rhel -m copy -a "src=/root/test_dir dest=/home/student"
- rhel | CHANGED => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/libexec/platform-python"
- },
- "changed": true,
- "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
- "dest": "/home/student/test_dir/666.xt",
- "gid": 0,
- "group": "root",
- "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
- "mode": "0644",
- "owner": "root",
- "secontext": "unconfined_u:object_r:user_home_t:s0",
- "size": 0,
- "src": "/home/student/.ansible/tmp/ansible-tmp-1659521079.1086252-4202-160722513414600/source",
- "state": "file",
- "uid": 0
- }
- [root@rhcsa test_dir]# ansible rhel -m fetch -a "dest=/root src=/home/student/test4.txt"
- rhel | CHANGED => {
- "changed": true,
- "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
- "dest": "/root/rhel/home/student/test4.txt",
- "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
- "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
- "remote_md5sum": null
- }
- [root@rhcsa student]# ansible rhel -m synchronize -a "src=/home/student/test2_dir dest=/home/student mode=pull"
- rhel | 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='<
>%i %n%L' student@rhel:/home/student/test2_dir /home/student" , - "msg": "cd+++++++++ test2_dir/\n>f+++++++++ test2_dir/d\n>f+++++++++ test2_dir/e\n",
- "rc": 0,
- "stdout_lines": [
- "cd+++++++++ test2_dir/",
- ">f+++++++++ test2_dir/d",
- ">f+++++++++ test2_dir/e"
- ]
- }
- [root@rhcsa ~]# ansible rhel -m synchronize -a "src=/root/test6_dir dest=/home/student mode=push"
- rhel | 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='<
>%i %n%L' /root/test6_dir student@rhel:/home/student" , - "msg": "cd+++++++++ test6_dir/\n",
- "rc": 0,
- "stdout_lines": [
- "cd+++++++++ test6_dir/"
- ]
- }