#: command可以在受管主机上执行shell命令,但是不支持环境变量和操作符,也无法使用管道符,并要求受管主机安装python;
shell模块调用的、bin/sh指令执行,要求在受管主机安装python;
raw不需要受管主机上安装python,直接使用远程shell运行命令,将结果返回给主控服务器;
#:command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo "I amcommand module"
(1)首先去被控主机中去创建command.sh脚本:可以在所有被控主机创建,也可以指定哪个被控主机创建,我这里指定在node1主机中进行创建:
[root@rhces1 ~]# vim command.sh [root@rhces1 ~]# chmod +x command.sh [root@rhces1 ~]# ./command.sh I am command module [root@rhces1 ~]# cat command.sh #!/bin/bash echo "I am command module" [root@rhces1 ~]#(2)在控制主机中执行command.sh脚本:
[root@rhce-128 ~]# ansible node -m command -a "bash /home/student/command.sh" node2.example.com | CHANGED | rc=0 >> I am command module node1.example.com | CHANGED | rc=0 >> I am command modual [root@rhce-128 ~]##:shell模块执行命令 ls /root | grep txt
[root@rhces1 ~]# touch file{1..2}.txt [root@rhces1 ~]# ls /root|grep txt file1.txt file2.txt [root@rhces1 ~]# ----------------------------------------------- [root@rhces2 ~]# touch file{1..2}.txt [root@rhces2 ~]# ls /root | grep txt file1.txt file2.txt [root@rhces2 ~]#
[root@rhce-128 ~]# ansible node -m shell -a "ls /root|grep txt chdir=/root" node2.example.com | CHANGED | rc=0 >> file1.txt file2.txt node1.example.com | CHANGED | rc=0 >> file1.txt file2.txt [root@rhce-128 ~]##:raw模块执行pwd命令
[root@rhce-128 ~]# ansible node -m raw -a "pwd" node2.example.com | CHANGED | rc=0 >> /root Shared connection to node2.example.com closed. node1.example.com | CHANGED | rc=0 >> /root Shared connection to node1.example.com closed. [root@rhce-128 ~]##:script模块执行 script.sh文件,文件的内容为 echo "I am script module"
[root@rhce-128 ~]# vim /root/script.sh [root@rhce-128 ~]# chmod +x script.sh [root@rhce-128 ~]# ansible node -m script -a "script.sh chdir=/root" node2.example.com | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to node2.example.com closed.\r\n", "stderr_lines": [ "Shared connection to node2.example.com closed." ], "stdout": " I am script module\r\n", "stdout_lines": [ " I am script module" ] } 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" ] } [root@rhce-128 ~]#
#:创建文件,并指定用户,用户组为student, 且权限为600
[root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test.txt state=file owner=student group=student mode=600" node1.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1013, "group": "student", "mode": "0600", "owner": "student", "path": "/home/student/test.txt", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 6, "state": "file", "uid": 1009 } node2.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1013, "group": "student", "mode": "0600", "owner": "student", "path": "/home/student/test.txt", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 6, "state": "file", "uid": 1009 } [root@rhce-128 ~]##:去被控主机查看:
[root@rhces1 student]# ll total 0 drw-------. 2 student student 6 Aug 3 21:01 test.txt [root@rhces1 student]#
[root@rhces2 student]# ll total 0 drw-------. 2 student student 6 Aug 3 21:00 test.txt [root@rhces2 student]##:创建目录,并指定用户,用户组为student, 且权限为755
[root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test state=directory owner=student group=student mode=600" node1.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1013, "group": "student", "mode": "0600", "owner": "student", "path": "/home/student/test", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 6, "state": "directory", "uid": 1009 } node2.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1013, "group": "student", "mode": "0600", "owner": "student", "path": "/home/student/test", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 6, "state": "directory", "uid": 1009 } [root@rhce-128 ~]##:到被控主机查询结果:
[root@rhces1 student]# ll total 0 drw-------. 2 student student 6 Aug 3 21:01 test [root@rhces1 student]#
[root@rhces2 student]# ll total 0 drw-------. 2 student student 6 Aug 3 21:00 test [root@rhces2 student]##:创建链接文件
[root@rhce-128 ~]# ansible node -m file -a "path=/home/student/linkfile state=link src=/home/student/test.txt" node2.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/home/student/linkfile", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 22, "src": "/home/student/test.txt", "state": "link", "uid": 0 } node1.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/home/student/linkfile", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 22, "src": "/home/student/test.txt", "state": "link", "uid": 0 } [root@rhce-128 ~]##:被控主机结果显示为:
[root@rhces1 student]# ll total 0 lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt -rw-------. 1 student student 0 Aug 3 21:10 test.txt [root@rhces1 student]#
[root@rhces2 student]# ll total 0 lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt -rw-------. 1 student student 0 Aug 3 21:09 test.txt [root@rhces2 student]##:删除第一个创建的文件
[root@rhce-128 ~]# ansible node -m file -a "path=/home/student/test.txt state=absent" node1.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/home/student/test.txt", "state": "absent" } node2.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/home/student/test.txt", "state": "absent" } [root@rhce-128 ~]##:被控主机结果显示:
[root@rhces1 student]# ll total 0 lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt [root@rhces1 student]#
[root@rhces2 student]# ll total 0 lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt [root@rhces2 student]#
复制文件
[root@rhce-128 ~]# ansible node -m copy -a "src=/root/tt dest=/home/student" node2.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "dest": "/home/student/tt", "gid": 0, "group": "root", "md5sum": "6662e6f920171897156556dad8032a3b", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 41, "src": "/root/.ansible/tmp/ansible-tmp-1659532784.44-40534-89814385207502/source", "state": "file", "uid": 0 } node1.example.com | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "dest": "/home/student/tt", "gid": 0, "group": "root", "md5sum": "6662e6f920171897156556dad8032a3b", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 41, "src": "/root/.ansible/tmp/ansible-tmp-1659532784.39-40533-22108167373706/source", "state": "file", "uid": 0 } [root@rhce-128 ~]##:被控主机显示如下所示:
[root@rhces1 student]# ll total 4 lrwxrwxrwx. 1 root root 22 Aug 3 21:14 linkfile -> /home/student/test.txt -rw-r--r--. 1 root root 41 Aug 3 21:20 tt [root@rhces1 student]#
[root@rhces2 student]# ll total 4 lrwxrwxrwx. 1 root root 22 Aug 3 21:13 linkfile -> /home/student/test.txt -rw-r--r--. 1 root root 41 Aug 3 21:19 tt [root@rhces2 student]#复制目录
[root@rhce-128 ~]# ansible node -m copy -a "src=/root/test dest=/root" node1.example.com | CHANGED => { "changed": true, "dest": "/root/", "src": "/root/test" } node2.example.com | CHANGED => { "changed": true, "dest": "/root/", "src": "/root/test" } [root@rhce-128 ~]#
[root@rhces1 ~]# ll -d test drwxr-xr-x. 3 root root 68 Aug 3 21:38 test [root@rhces1 ~]#
[root@rhces2 ~]# ll total 0 drwxr-xr-x. 3 root root 68 Aug 3 21:37 test [root@rhces2 ~]#
从被控制主机上取文件
[root@rhce-128 ~]# ansible node -m fetch -a "src=/home/student/tt dest=/root" node1.example.com | CHANGED => { "changed": true, "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "dest": "/root/node1.example.com/home/student/tt", "md5sum": "6662e6f920171897156556dad8032a3b", "remote_checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "remote_md5sum": null } node2.example.com | CHANGED => { "changed": true, "checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "dest": "/root/node2.example.com/home/student/tt", "md5sum": "6662e6f920171897156556dad8032a3b", "remote_checksum": "dfecb4295e9152e6f24ed2fe0310c2ec5c911596", "remote_md5sum": null } [root@rhce-128 ~]#
pull: 从被控制主机上拉取目录
[root@rhce-128 ~]# ansible node1.example.com -m synchronize -a "src=/home/student/test dest=/root/ mode=pull" node1.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 --out-format=<>%i %n%L node1.example.com:/home/student/test /root/" , "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", "rc": 0, "stdout_lines": [ ".d..t...... test/", ">f+++++++++ test/file1.tar", ">f+++++++++ test/file2.txt", ">f+++++++++ test/file3.txt", "cd+++++++++ test/tar/", ">f+++++++++ test/tar/file1.txt" ] } [root@rhce-128 ~]##:被控主机执行结果:
[root@rhce-128 ~]# ll -d test drwxr-xr-x. 3 root root 68 Aug 3 21:38 test [root@rhce-128 ~]#push:往被控制主机上推送目录
[root@rhce-128 ~]# ansible node1.example.com -m synchronize -a "src=/root/dir1 dest=/home/student/ mode=push" node1.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 --out-format=<>%i %n%L /root/dir1 node1.example.com:/home/student/" , "msg": "cd+++++++++ dir1/\n", "rc": 0, "stdout_lines": [ "cd+++++++++ dir1/" ] } [root@rhce-128 ~]##:被控主机执行结果:
[root@rhces1 ~]# ll -d /home/student/dir1 drwxr-xr-x. 2 root root 6 Aug 3 22:44 /home/student/dir1 [root@rhces1 ~]#