• Linux--CE--ansible常用模块(1)


    1.command,shell,raw,script模块的作用和区别

    #:   command可以在受管主机上执行shell命令,但是不支持环境变量和操作符,也无法使用管道符,并要求受管主机安装python;

            shell模块调用的、bin/sh指令执行,要求在受管主机安装python;

            raw不需要受管主机上安装python,直接使用远程shell运行命令,将结果返回给主控服务器;

    #:command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo "I amcommand module"

    (1)首先去被控主机中去创建command.sh脚本:可以在所有被控主机创建,也可以指定哪个被控主机创建,我这里指定在node1主机中进行创建:

    1. [root@rhces1 ~]# vim command.sh
    2. [root@rhces1 ~]# chmod +x command.sh
    3. [root@rhces1 ~]# ./command.sh
    4. I am command module
    5. [root@rhces1 ~]# cat command.sh
    6. #!/bin/bash
    7. echo "I am command module"
    8. [root@rhces1 ~]#

    (2)在控制主机中执行command.sh脚本:

    1. [root@rhce-128 ~]# ansible node -m command -a "bash /home/student/command.sh"
    2. node2.example.com | CHANGED | rc=0 >>
    3. I am command module
    4. node1.example.com | CHANGED | rc=0 >>
    5. I am command modual
    6. [root@rhce-128 ~]#

    #:shell模块执行命令 ls /root | grep txt

    1. [root@rhces1 ~]# touch file{1..2}.txt
    2. [root@rhces1 ~]# ls /root|grep txt
    3. file1.txt
    4. file2.txt
    5. [root@rhces1 ~]#
    6. -----------------------------------------------
    7. [root@rhces2 ~]# touch file{1..2}.txt
    8. [root@rhces2 ~]# ls /root | grep txt
    9. file1.txt
    10. file2.txt
    11. [root@rhces2 ~]#
    1. [root@rhce-128 ~]# ansible node -m shell -a "ls /root|grep txt chdir=/root"
    2. node2.example.com | CHANGED | rc=0 >>
    3. file1.txt
    4. file2.txt
    5. node1.example.com | CHANGED | rc=0 >>
    6. file1.txt
    7. file2.txt
    8. [root@rhce-128 ~]#

    #:raw模块执行pwd命令

    1. [root@rhce-128 ~]# ansible node -m raw -a "pwd"
    2. node2.example.com | CHANGED | rc=0 >>
    3. /root
    4. Shared connection to node2.example.com closed.
    5. node1.example.com | CHANGED | rc=0 >>
    6. /root
    7. Shared connection to node1.example.com closed.
    8. [root@rhce-128 ~]#

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

    1. [root@rhce-128 ~]# vim /root/script.sh
    2. [root@rhce-128 ~]# chmod +x script.sh
    3. [root@rhce-128 ~]# ansible node -m script -a "script.sh chdir=/root"
    4. node2.example.com | CHANGED => {
    5. "changed": true,
    6. "rc": 0,
    7. "stderr": "Shared connection to node2.example.com closed.\r\n",
    8. "stderr_lines": [
    9. "Shared connection to node2.example.com closed."
    10. ],
    11. "stdout": " I am script module\r\n",
    12. "stdout_lines": [
    13. " I am script module"
    14. ]
    15. }
    16. node1.example.com | CHANGED => {
    17. "changed": true,
    18. "rc": 0,
    19. "stderr": "Shared connection to node1.example.com closed.\r\n",
    20. "stderr_lines": [
    21. "Shared connection to node1.example.com closed."
    22. ],
    23. "stdout": " I am script module\r\n",
    24. "stdout_lines": [
    25. " I am script module"
    26. ]
    27. }
    28. [root@rhce-128 ~]#

    2.file模块:

    #:创建文件,并指定用户,用户组为student, 且权限为600

    1. [root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test.txt state=file owner=student group=student mode=600"
    2. node1.example.com | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python"
    5. },
    6. "changed": true,
    7. "gid": 1013,
    8. "group": "student",
    9. "mode": "0600",
    10. "owner": "student",
    11. "path": "/home/student/test.txt",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 6,
    14. "state": "file",
    15. "uid": 1009
    16. }
    17. node2.example.com | CHANGED => {
    18. "ansible_facts": {
    19. "discovered_interpreter_python": "/usr/bin/python"
    20. },
    21. "changed": true,
    22. "gid": 1013,
    23. "group": "student",
    24. "mode": "0600",
    25. "owner": "student",
    26. "path": "/home/student/test.txt",
    27. "secontext": "unconfined_u:object_r:user_home_t:s0",
    28. "size": 6,
    29. "state": "file",
    30. "uid": 1009
    31. }
    32. [root@rhce-128 ~]#

    #:去被控主机查看:

    1. [root@rhces1 student]# ll
    2. total 0
    3. drw-------. 2 student student 6 Aug 3 21:01 test.txt
    4. [root@rhces1 student]#
    1. [root@rhces2 student]# ll
    2. total 0
    3. drw-------. 2 student student 6 Aug 3 21:00 test.txt
    4. [root@rhces2 student]#

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

    1. [root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test state=directory owner=student group=student mode=600"
    2. node1.example.com | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python"
    5. },
    6. "changed": true,
    7. "gid": 1013,
    8. "group": "student",
    9. "mode": "0600",
    10. "owner": "student",
    11. "path": "/home/student/test",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 6,
    14. "state": "directory",
    15. "uid": 1009
    16. }
    17. node2.example.com | CHANGED => {
    18. "ansible_facts": {
    19. "discovered_interpreter_python": "/usr/bin/python"
    20. },
    21. "changed": true,
    22. "gid": 1013,
    23. "group": "student",
    24. "mode": "0600",
    25. "owner": "student",
    26. "path": "/home/student/test",
    27. "secontext": "unconfined_u:object_r:user_home_t:s0",
    28. "size": 6,
    29. "state": "directory",
    30. "uid": 1009
    31. }
    32. [root@rhce-128 ~]#

    #:到被控主机查询结果:

    1. [root@rhces1 student]# ll
    2. total 0
    3. drw-------. 2 student student 6 Aug 3 21:01 test
    4. [root@rhces1 student]#
    1. [root@rhces2 student]# ll
    2. total 0
    3. drw-------. 2 student student 6 Aug 3 21:00 test
    4. [root@rhces2 student]#

    #:创建链接文件

    1. [root@rhce-128 ~]# ansible node -m file -a "path=/home/student/linkfile state=link src=/home/student/test.txt"
    2. node2.example.com | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python"
    5. },
    6. "changed": true,
    7. "dest": "/home/student/linkfile",
    8. "gid": 0,
    9. "group": "root",
    10. "mode": "0777",
    11. "owner": "root",
    12. "secontext": "unconfined_u:object_r:user_home_t:s0",
    13. "size": 22,
    14. "src": "/home/student/test.txt",
    15. "state": "link",
    16. "uid": 0
    17. }
    18. node1.example.com | CHANGED => {
    19. "ansible_facts": {
    20. "discovered_interpreter_python": "/usr/bin/python"
    21. },
    22. "changed": true,
    23. "dest": "/home/student/linkfile",
    24. "gid": 0,
    25. "group": "root",
    26. "mode": "0777",
    27. "owner": "root",
    28. "secontext": "unconfined_u:object_r:user_home_t:s0",
    29. "size": 22,
    30. "src": "/home/student/test.txt",
    31. "state": "link",
    32. "uid": 0
    33. }
    34. [root@rhce-128 ~]#

    #:被控主机结果显示为:

    1. [root@rhces1 student]# ll
    2. total 0
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt
    4. -rw-------. 1 student student 0 Aug 3 21:10 test.txt
    5. [root@rhces1 student]#
    1. [root@rhces2 student]# ll
    2. total 0
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt
    4. -rw-------. 1 student student 0 Aug 3 21:09 test.txt
    5. [root@rhces2 student]#

    #:删除第一个创建的文件

    1. [root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test.txt state=absent"
    2. node1.example.com | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python"
    5. },
    6. "changed": true,
    7. "path": "/home/student/test.txt",
    8. "state": "absent"
    9. }
    10. node2.example.com | CHANGED => {
    11. "ansible_facts": {
    12. "discovered_interpreter_python": "/usr/bin/python"
    13. },
    14. "changed": true,
    15. "path": "/home/student/test.txt",
    16. "state": "absent"
    17. }
    18. [root@rhce-128 ~]#

    #:被控主机结果显示:

    1. [root@rhces1 student]# ll
    2. total 0
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt
    4. [root@rhces1 student]#
    1. [root@rhces2 student]# ll
    2. total 0
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt
    4. [root@rhces2 student]#

    3.copy模块

    复制文件

    1. [root@rhce-128 ~]# ansible node -m copy -a "src=/root/tt dest=/home/student"
    2. node2.example.com | CHANGED => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python"
    5. },
    6. "changed": true,
    7. "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    8. "dest": "/home/student/tt",
    9. "gid": 0,
    10. "group": "root",
    11. "md5sum": "6662e6f920171897156556dad8032a3b",
    12. "mode": "0644",
    13. "owner": "root",
    14. "secontext": "unconfined_u:object_r:user_home_t:s0",
    15. "size": 41,
    16. "src": "/root/.ansible/tmp/ansible-tmp-1659532784.44-40534-89814385207502/source",
    17. "state": "file",
    18. "uid": 0
    19. }
    20. node1.example.com | CHANGED => {
    21. "ansible_facts": {
    22. "discovered_interpreter_python": "/usr/bin/python"
    23. },
    24. "changed": true,
    25. "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    26. "dest": "/home/student/tt",
    27. "gid": 0,
    28. "group": "root",
    29. "md5sum": "6662e6f920171897156556dad8032a3b",
    30. "mode": "0644",
    31. "owner": "root",
    32. "secontext": "unconfined_u:object_r:user_home_t:s0",
    33. "size": 41,
    34. "src": "/root/.ansible/tmp/ansible-tmp-1659532784.39-40533-22108167373706/source",
    35. "state": "file",
    36. "uid": 0
    37. }
    38. [root@rhce-128 ~]#

    #:被控主机显示如下所示:

    1. [root@rhces1 student]# ll
    2. total 4
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt
    4. -rw-r--r--. 1 root root 41 Aug 3 21:20 tt
    5. [root@rhces1 student]#
    1. [root@rhces2 student]# ll
    2. total 4
    3. lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt
    4. -rw-r--r--. 1 root root 41 Aug 3 21:19 tt
    5. [root@rhces2 student]#

    复制目录

    1. [root@rhce-128 ~]# ansible node -m copy -a "src=/root/test dest=/root"
    2. node1.example.com | CHANGED => {
    3. "changed": true,
    4. "dest": "/root/",
    5. "src": "/root/test"
    6. }
    7. node2.example.com | CHANGED => {
    8. "changed": true,
    9. "dest": "/root/",
    10. "src": "/root/test"
    11. }
    12. [root@rhce-128 ~]#
    1. [root@rhces1 ~]# ll -d test
    2. drwxr-xr-x. 3 root root 68 Aug 3 21:38 test
    3. [root@rhces1 ~]#
    1. [root@rhces2 ~]# ll
    2. total 0
    3. drwxr-xr-x. 3 root root 68 Aug 3 21:37 test
    4. [root@rhces2 ~]#

    4.fetch模块

    从被控制主机上取文件

    1. [root@rhce-128 ~]# ansible node -m fetch -a "src=/home/student/tt dest=/root"
    2. node1.example.com | CHANGED => {
    3. "changed": true,
    4. "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    5. "dest": "/root/node1.example.com/home/student/tt",
    6. "md5sum": "6662e6f920171897156556dad8032a3b",
    7. "remote_checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    8. "remote_md5sum": null
    9. }
    10. node2.example.com | CHANGED => {
    11. "changed": true,
    12. "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    13. "dest": "/root/node2.example.com/home/student/tt",
    14. "md5sum": "6662e6f920171897156556dad8032a3b",
    15. "remote_checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596",
    16. "remote_md5sum": null
    17. }
    18. [root@rhce-128 ~]#

    5.synchronize模块

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

    1. [root@rhce-128 ~]# ansible node1.example.com -m synchronize -a "src=/home/student/test dest=/root/ mode=pull"
    2. node1.example.com | 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 --out-format=<>%i %n%L node1.example.com:/home/student/test /root/",
    5. "msg": ".d..t...... test/\n>f+++++++++ test/file1.tar\n>f+++++++++ test/file2.txt\n>f+++++++++ test/file3.txt\ncd+++++++++ test/tar/\n>f+++++++++ test/tar/file1.txt\n",
    6. "rc": 0,
    7. "stdout_lines": [
    8. ".d..t...... test/",
    9. ">f+++++++++ test/file1.tar",
    10. ">f+++++++++ test/file2.txt",
    11. ">f+++++++++ test/file3.txt",
    12. "cd+++++++++ test/tar/",
    13. ">f+++++++++ test/tar/file1.txt"
    14. ]
    15. }
    16. [root@rhce-128 ~]#

    #:被控主机执行结果:

    1. [root@rhce-128 ~]# ll -d test
    2. drwxr-xr-x. 3 root root 68 Aug 3 21:38 test
    3. [root@rhce-128 ~]#

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

    1. [root@rhce-128 ~]# ansible node1.example.com -m synchronize -a "src=/root/dir1 dest=/home/student/ mode=push"
    2. node1.example.com | 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 --out-format=<>%i %n%L /root/dir1 node1.example.com:/home/student/",
    5. "msg": "cd+++++++++ dir1/\n",
    6. "rc": 0,
    7. "stdout_lines": [
    8. "cd+++++++++ dir1/"
    9. ]
    10. }
    11. [root@rhce-128 ~]#

    #:被控主机执行结果:

    1. [root@rhces1 ~]# ll -d /home/student/dir1
    2. drwxr-xr-x. 2 root root 6 Aug 3 22:44 /home/student/dir1
    3. [root@rhces1 ~]#
  • 相关阅读:
    Pandas合并excel表格的两种方式
    Rust 力扣 - 643. 子数组最大平均数 I
    基于遗传算法的新能源电动汽车充电桩与路径选择(Matlab代码实现)
    【C++】STL之list深度剖析及模拟实现
    DSP28335模块配置模板系列——定时器中断配置模板
    MySQL SQL语句限制参数
    D-莲子的物理热力学
    网络安全—黑客技术—自学笔记
    音视频开发常见问题(四):视频花屏和绿屏
    Spring bean 的生命周期(总结)
  • 原文地址:https://blog.csdn.net/AiTTTTTT/article/details/126144230