Linux之ansible模块command,shell,raw,script,file,copy,fetch,synchronize超详解

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" 

在客户端上创建文件(展示的是最后效果图,什么方法都就可以用)

[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 

 shell模块执行命令 ls /root | grep cfg

[root@rhcsa ~]# ansible rhel -m shell -a "ls /root | grep cfg"
rhel | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg

   raw模块执行pwd命令

[root@rhcsa ~]# ansible rhel -m raw -a "pwd"
rhel | CHANGED | rc=0 >>
/home/student
Shared connection to rhel closed.

script模块执行 script.sh文件,文件的内容为 echo "I am script module"

指定可执行脚本输出内容

[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"
    ]
}

2.file模块:
   创建文件,并指定用户,用户组为student, 且权限为600

[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
}

   创建目录,并指定用户,用户组为student,   且权限为755

[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"
}

3.copy
   复制文件

[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
}

4.fetch
   从被控制主机上取文件

[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
}

5.synchronize
   pull: 从被控制主机上拉取目录

[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='<<CHANGED>>%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"
    ]
}

   push:往被控制主机上推送目录

[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='<<CHANGED>>%i %n%L' /root/test6_dir student@rhel:/home/student",
    "msg": "cd+++++++++ test6_dir/\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++ test6_dir/"
    ]
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gur.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值