• 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" 

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

    1. [root@rhel ~]# cat command.sh
    2. echo"I am command module" 
    1. [root@rhcsa ~]# ansible rhel -m command -a "sh command.sh chdir=/root"
    2. rhel | CHANGED | rc=0 >>
    3. I am command module 

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

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

       raw模块执行pwd命令

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

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

    指定可执行脚本输出内容

    [root@rhcsa ~]# echo "echo "I am script module"" > script.sh
    1. [root@rhcsa ~]# ansible rhel -m script -a "script.sh free_form=/root"
    2. rhel | CHANGED => {
    3. "changed": true,
    4. "rc": 0,
    5. "stderr": "Shared connection to rhel closed.\r\n",
    6. "stderr_lines": [
    7. "Shared connection to rhel closed."
    8. ],
    9. "stdout": "I am script module\r\n",
    10. "stdout_lines": [
    11. "I am script module"
    12. ]
    13. }

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

    1. [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt owner=student group=student mode=600 state=touch"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "dest": "/home/student/test2.txt",
    8. "gid": 2002,
    9. "group": "student",
    10. "mode": "0600",
    11. "owner": "student",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 0,
    14. "state": "file",
    15. "uid": 2002
    16. }

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

    1. [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test_dir owner=student group=student mode=755 state=directory"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "gid": 2002,
    8. "group": "student",
    9. "mode": "0755",
    10. "owner": "student",
    11. "path": "/home/student/test_dir",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 6,
    14. "state": "directory",
    15. "uid": 2002
    16. }

        创建链接文件

    1. [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test4.txt src=test2.txt state=link"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "dest": "/home/student/test4.txt",
    8. "gid": 0,
    9. "group": "root",
    10. "mode": "0777",
    11. "owner": "root",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 9,
    14. "src": "test2.txt",
    15. "state": "link",
    16. "uid": 0
    17. }

       删除第一个创建的文件

    1. [root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt state=absent"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "path": "/home/student/test2.txt",
    8. "state": "absent"
    9. }

    3.copy
       复制文件

    1. [root@rhcsa ~]# ansible rhel -m copy -a "src=/root/test.txt dest=/home/student"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    8. "dest": "/home/student/test.txt",
    9. "gid": 0,
    10. "group": "root",
    11. "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    12. "mode": "0644",
    13. "owner": "root",
    14. "secontext": "unconfined_u:object_r:user_home_t:s0",
    15. "size": 0,
    16. "src": "/home/student/.ansible/tmp/ansible-tmp-1659519729.6693015-3382-265136890969898/source",
    17. "state": "file",
    18. "uid": 0
    19. }

       复制目录

    1. [root@rhcsa test_dir]# ansible rhel -m copy -a "src=/root/test_dir dest=/home/student"
    2. rhel | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/libexec/platform-python"
    5. },
    6. "changed": true,
    7. "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    8. "dest": "/home/student/test_dir/666.xt",
    9. "gid": 0,
    10. "group": "root",
    11. "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    12. "mode": "0644",
    13. "owner": "root",
    14. "secontext": "unconfined_u:object_r:user_home_t:s0",
    15. "size": 0,
    16. "src": "/home/student/.ansible/tmp/ansible-tmp-1659521079.1086252-4202-160722513414600/source",
    17. "state": "file",
    18. "uid": 0
    19. }

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

    1. [root@rhcsa test_dir]# ansible rhel -m fetch -a "dest=/root src=/home/student/test4.txt"
    2. rhel | CHANGED => {
    3. "changed": true,
    4. "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    5. "dest": "/root/rhel/home/student/test4.txt",
    6. "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    7. "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    8. "remote_md5sum": null
    9. }

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

    1. [root@rhcsa student]# ansible rhel -m synchronize -a "src=/home/student/test2_dir dest=/home/student mode=pull"
    2. rhel | CHANGED => {
    3. "changed": true,
    4. "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",
    5. "msg": "cd+++++++++ test2_dir/\n>f+++++++++ test2_dir/d\n>f+++++++++ test2_dir/e\n",
    6. "rc": 0,
    7. "stdout_lines": [
    8. "cd+++++++++ test2_dir/",
    9. ">f+++++++++ test2_dir/d",
    10. ">f+++++++++ test2_dir/e"
    11. ]
    12. }

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

    1. [root@rhcsa ~]# ansible rhel -m synchronize -a "src=/root/test6_dir dest=/home/student mode=push"
    2. rhel | CHANGED => {
    3. "changed": true,
    4. "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",
    5. "msg": "cd+++++++++ test6_dir/\n",
    6. "rc": 0,
    7. "stdout_lines": [
    8. "cd+++++++++ test6_dir/"
    9. ]
    10. }

  • 相关阅读:
    【进阶版】机器学习与深度学习之前向传播与反向传播知识(08)
    2023北京市人工智能大模型场景融合与产业发展专场活动盛大召开
    “2023知识产权试点单位” 坤驰科技成功入选
    客服岗位需要一个知识库吗?
    Go 字符串处理:fmt.Sprintf与string.Builder的比较
    使用 typescript + express 创建 NodeJs 后端服务
    程序员都无法理解的代码
    视频格式说明
    自动化测试之路 —— Appium安装教程
    【远程文件浏览器】Unity+Lua开发调试利器
  • 原文地址:https://blog.csdn.net/weixin_64051859/article/details/126143962