{"id":103561,"date":"2024-08-30T14:53:12","date_gmt":"2024-08-30T06:53:12","guid":{"rendered":"https:\/\/gulass.cn\/?p=103561"},"modified":"2024-08-30T14:53:12","modified_gmt":"2024-08-30T06:53:12","slug":"ansible-ad-hoc-commands","status":"publish","type":"post","link":"https:\/\/gulass.cn\/ansible-ad-hoc-commands.html","title":{"rendered":"\u4ecb\u7ecdansible\u7684Ad-hoc\u4e0ecommands\u6a21\u5757"},"content":{"rendered":"
Ad-Hoc \u662f\u6307ansible\u4e0b\u4e34\u65f6\u6267\u884c\u7684\u4e00\u6761\u547d\u4ee4\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u4fdd\u5b58\u7684\u547d\u4ee4\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u547d\u4ee4\u540e\u9762\u4f1a\u8bf4playbook\u3002\u8bb2\u5230Ad-hoc \u5c31\u8981\u63d0\u5230\u6a21\u5757\uff0c\u6240\u6709\u7684\u547d\u4ee4\u6267\u884c\u90fd\u8981\u4f9d\u8d56\u4e8e\u4e8b\u5148\u5199\u597d\u7684\u6a21\u5757\uff0c\u9ed8\u8ba4\u5b89\u88c5\u597d\u7684ansible \u91cc\u9762\u5df2\u7ecf\u81ea\u5e26\u4e86\u5f88\u591a\u6a21\u5757\uff0c\u5982\uff1acommand\u3001raw\u3001shell\u3001file\u3001cron\u7b49\uff0c\u5177\u4f53\u53ef\u4ee5\u901a\u8fc7ansible-doc -l \u8fdb\u884c\u67e5\u770b \u3002<\/p>\n
\u8fd9\u91cc\u8fd8\u662f\u5148\u6765\u4e00\u4e2a\u4e0a\u51e0\u7bc7\u5e45\u7ecf\u5e38\u7528\u5230\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n
[root@361way ~]# ansible 10.212.52.252 -a 'uptime' -k\r\nSSH password:\r\n10.212.52.252 | success | rc=0 >>\r\n10:10am up 27 days 19:33, 2 users, load average: 0.39, 0.34, 0.33\r\n<\/pre>\n\u4e00\u4e2aad-hoc\u547d\u4ee4\u7684\u6267\u884c\uff0c\u9700\u8981\u6309\u4ee5\u4e0b\u683c\u5f0f\u8fdb\u884c\u6267\u884c\uff1a<\/p>\n
\n
- ansible <\/span>\u4e3b\u673a\u6216\u7ec4<\/span> -<\/span>m <\/span>\u6a21\u5757\u540d<\/span> -<\/span>a <\/span>'\u6a21\u5757\u53c2\u6570'<\/span> ansible<\/span>\u53c2\u6570<\/span><\/li>\n<\/ol>\n
\n
- \u4e3b\u673a\u548c\u7ec4\uff0c\u662f\u5728\/etc\/ansible\/hosts \u91cc\u8fdb\u884c\u6307\u5b9a\u7684\u90e8\u5206\uff0c\u5f53\u7136\u52a8\u6001Inventory \u4f7f\u7528\u7684\u662f\u811a\u672c\u4ece\u5916\u90e8\u5e94\u7528\u91cc\u83b7\u53d6\u7684\u4e3b\u673a\uff0c\u8fd9\u90e8\u5206\u5177\u4f53\u53ef\u4ee5\u53c2\u8003ansible\u5c0f\u7ed3\uff08\u4e94\uff09Dynamic Inventory <\/a>\uff1b<\/li>\n
- \u6a21\u5757\u540d\uff0c\u53ef\u4ee5\u901a\u8fc7ansible-doc -l \u67e5\u770b\u76ee\u524d\u5b89\u88c5\u7684\u6a21\u5757\uff0c\u9ed8\u8ba4\u4e0d\u6307\u5b9a\u65f6\uff0c\u4f7f\u7528\u7684\u662fcommand\u6a21\u5757\uff0c\u5177\u4f53\u53ef\u4ee5\u67e5\u770b\/etc\/ansible\/ansible.cfg \u7684\u201c#module_name = command \u201d \u90e8\u5206\uff0c\u9ed8\u8ba4\u6a21\u5757\u53ef\u4ee5\u5728\u8be5\u914d\u7f6e\u6587\u4ef6\u4e2d\u8fdb\u884c\u4fee\u6539\uff1b<\/li>\n
- \u6a21\u5757\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7 \u201cansible-doc \u6a21\u5757\u540d\u201d \u67e5\u770b\u5177\u4f53\u7684\u7528\u6cd5\u53ca\u540e\u9762\u7684\u53c2\u6570\uff1b<\/li>\n
- ansible\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7ansible\u547d\u4ee4\u7684\u5e2e\u5fd9\u4fe1\u606f\u91cc\u67e5\u770b\u5230\uff0c\u8fd9\u91cc\u6709\u5f88\u591a\u53c2\u6570\u53ef\u4ee5\u4f9b\u9009\u62e9\uff0c\u5982\u662f\u5426\u9700\u8981\u8f93\u5165\u5bc6\u7801\u3001\u662f\u5426sudo\u7b49\u3002<\/li>\n<\/ul>\n
2\u3001\u540e\u53f0\u6267\u884c<\/strong><\/span><\/div>\n\u5f53\u547d\u4ee4\u6267\u884c\u65f6\u95f4\u6bd4\u8f83\u957f\u65f6\uff0c\u4e5f\u53ef\u4ee5\u653e\u5230\u540e\u53f0\u6267\u884c\uff0c\u8fd9\u91cc\u4f1a\u7528\u5230-B\u3001-P\u53c2\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n
ansible all -B 3600 -a \"\/usr\/bin\/long_running_operation --do-stuff\" \r\n\\\\\u540e\u53f0\u6267\u884c\u547d\u4ee4 3600s\uff0c-B \u8868\u793a\u540e\u53f0\u6267\u884c\u7684\u65f6\u95f4\r\nansible all -m async_status -a \"jid=123456789\" \r\n\\\\\u68c0\u67e5\u4efb\u52a1\u7684\u72b6\u6001\r\nansible all -B 1800 -P 60 -a \"\/usr\/bin\/long_running_operation --do-stuff\" \r\n\\\\\u540e\u53f0\u6267\u884c\u547d\u4ee4\u6700\u5927\u65f6\u95f4\u662f 1800s \u5373 30 \u5206\u949f\uff0c-P \u6bcf 60s \u68c0\u67e5\u4e0b\u72b6\u6001\u9ed8\u8ba4 15s\r\n<\/pre>\n\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n
[root@361way ~]# ansible 10.212.52.252 -B 3600 -P 0 -a 'watch ls'\r\nbackground launch...\r\n10.212.52.252 | success >> {\r\n\"ansible_job_id\": \"411650646689.13501\",\r\n\"results_file\": \"\/root\/.ansible_async\/411650646689.13501\",\r\n\"started\": 1\r\n}\r\n[root@361way ~]# ansible 10.212.52.252 -m async_status -a 'jid=411650646689.13501'\r\n10.212.52.252 | success >> {\r\n\"ansible_job_id\": \"411650646689.13501\",\r\n\"changed\": false,\r\n\"finished\": 0,\r\n\"results_file\": \"\/root\/.ansible_async\/411650646689.13501\",\r\n\"started\": 1\r\n}<\/pre>\n\u4e0d\u6307\u5b9a-P\u6216-P\u53c2\u6570\u4e3a\u975e0\u65f6\uff0c\u8be5\u4efb\u52a1\u5c31\u4f1a\u6309-P\u76f4\u63a5\u7684\u53c2\u6570\u4e00\u76f4\u5237\u65b0\u4e0b\u53bb\uff0c\u76f4\u5230\u8d85\u51fa-B\u53c2\u6570\u6307\u5b9a\u7684\u65f6\u95f4\u6216\u547d\u4ee4\u6267\u884c\u5b8c\u6210\uff1a<\/p>\n
[root@361way ~]# ansible 10.212.52.252 -B 3600 -a 'watch ls'\r\nbackground launch...\r\n10.212.52.252 | success >> {\r\n\"ansible_job_id\": \"397200656414.15008\",\r\n\"results_file\": \"\/root\/.ansible_async\/397200656414.15008\",\r\n\"started\": 1\r\n}\r\n10.212.52.252 | success >> {\r\n\"ansible_job_id\": \"397200656414.15008\",\r\n\"changed\": false,\r\n\"finished\": 0,\r\n\"results_file\": \"\/root\/.ansible_async\/397200656414.15008\",\r\n\"started\": 1\r\n}\r\n polling on 10.212.52.252, 3585s remaining\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u7565<\/pre>\n\u4e8c\u3001commands\u6a21\u5757<\/strong><\/div>\n\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\uff0cansbile\u81ea\u8eab\u5df2\u7ecf\u81ea\u5e26\u4e86\u5f88\u591a\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7ansible-doc -l \u8fdb\u884c\u67e5\u770b\u3002\u8fd9\u91cc\u5c31\u7ed3\u5408command\u3001shell\u3001raw\u3001script\u6a21\u5757\u4e86\u89e3\u4e0b\u5176\u7528\u6cd5\u3002<\/p>\n
\u4e0a\u9762\u56db\u4e2a\u6a21\u5757\u90fd\u5c5e\u4e8ecommands \u7c7b<\/a>\u3002<\/p>\n
\n
- command\u6a21\u5757<\/a>\uff0c\u8be5\u6a21\u5757\u901a\u8fc7-a\u8ddf\u4e0a\u8981\u6267\u884c\u7684\u547d\u4ee4\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\uff0c\u4e0d\u8fc7\u547d\u4ee4\u91cc\u5982\u679c\u6709\u5e26\u6709\u5982\u4e0b\u5b57\u7b26\u90e8\u5206\u5219\u6267\u884c\u4e0d\u6210\u529f \u201c so variables like $HOME and operations like \"<\", \">\", \"|\", and \"&\" will not work (use the shell module if you need these features).\u201d\uff1b<\/li>\n
- shell \u6a21\u5757<\/a>\uff0c\u7528\u6cd5\u5176\u672c\u548ccommand\u4e00\u6837\uff0c\u4e0d\u8fc7\u7684\u662f\u5176\u662f\u901a\u8fc7\/bin\/sh\u8fdb\u884c\u6267\u884c\uff0c\u6240\u4ee5shell \u6a21\u5757\u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u547d\u4ee4\uff0c\u5c31\u50cf\u5728\u672c\u673a\u6267\u884c\u4e00\u6837\uff0c\u201c It is almost exactly like the command module but runs the command through a shell (\/bin\/sh) on the remote node.\u201d\uff1b<\/li>\n
- raw\u6a21\u5757<\/a>\uff0c\u7528\u6cd5\u548cshell \u6a21\u5757\u4e00\u6837 \uff0c\u5176\u4e5f\u53ef\u4ee5\u6267\u884c\u4efb\u610f\u547d\u4ee4\uff0c\u5c31\u50cf\u5728\u672c\u673a\u6267\u884c\u4e00\u6837\uff0c\u201cExecutes a low-down and dirty SSH command, not going through the module subsystem. There is no change handler support for this module. This module does not require python on the remote system\u201d<\/li>\n
- script\u6a21\u5757<\/a>\uff0c\u5176\u662f\u5c06\u7ba1\u7406\u7aef\u7684shell \u5728\u88ab\u7ba1\u7406\u4e3b\u673a\u4e0a\u6267\u884c\uff0c\u5176\u539f\u7406\u662f\u5148\u5c06shell \u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\uff0c\u518d\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u6267\u884c\uff0c\u539f\u7406\u7c7b\u4f3c\u4e8eraw\u6a21\u5757\uff0c\u201cThis module does not require python on the remote system, much like the raw module.\u201d \u3002<\/li>\n<\/ul>\n
\u6ce8\uff1araw\u6a21\u5757\u548ccomand\u3001shell \u6a21\u5757\u4e0d\u540c\u7684\u662f\u5176\u6ca1\u6709chdir\u3001creates\u3001removes\u53c2\u6570\uff0cchdir\u53c2\u6570\u7684\u4f5c\u7528\u5c31\u662f\u5148\u5207\u5230chdir\u6307\u5b9a\u7684\u76ee\u5f55\u540e\uff0c\u518d\u6267\u884c\u540e\u9762\u7684\u547d\u4ee4\uff0c\u8fd9\u5728\u540e\u9762\u5f88\u591a\u6a21\u5757\u91cc\u90fd\u4f1a\u6709\u8be5\u53c2\u6570\u00a0\u3002<\/p>\n
command\u6a21\u5757\u5305\u542b\u5982\u4e0b\u9009\u9879\uff1a<\/p>\n
\n
- creates\uff1a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5f53\u8be5\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u8be5\u547d\u4ee4\u4e0d\u6267\u884c<\/li>\n
- free_form\uff1a\u8981\u6267\u884c\u7684linux\u6307\u4ee4<\/li>\n
- chdir\uff1a\u5728\u6267\u884c\u6307\u4ee4\u4e4b\u524d\uff0c\u5148\u5207\u6362\u5230\u8be5\u6307\u5b9a\u7684\u76ee\u5f55<\/li>\n
- removes\uff1a\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5f53\u8be5\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u8be5\u9009\u9879\u4e0d\u6267\u884c<\/li>\n
- executable\uff1a\u5207\u6362shell\u6765\u6267\u884c\u6307\u4ee4\uff0c\u8be5\u6267\u884c\u8def\u5f84\u5fc5\u987b\u662f\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84<\/li>\n<\/ul>\n
command\u6a21\u5757\u3001raw\u6a21\u5757\u3001shell\u6a21\u5757\u793a\u4f8b\uff1a<\/strong><\/p>\n
[root@361way ~]# ansible 10.212.52.252 -m command -a 'ps auxf|grep snmp'\r\n10.212.52.252 | FAILED | rc=1 >>\r\nERROR: Unsupported option (BSD syntax)\r\n********* simple selection ********* ********* selection by list *********\r\n-A all processes -C by command name\r\n-N negate selection -G by real group ID (supports names)\r\n-a all w\/ tty except session leaders -U by real user ID (supports names)\r\n-d all except session leaders -g by session OR by effective group name\r\n-e all processes -p by process ID\r\nT all processes on this terminal -s processes in the sessions given\r\na all w\/ tty, including other users -t by tty\r\ng OBSOLETE -- DO NOT USE -u by effective user ID (supports names)\r\nr only running processes U processes for specified users\r\nx processes w\/o controlling ttys t by tty\r\n*********** output format ********** *********** long options ***********\r\n-o,o user-defined -f full --Group --User --pid --cols --ppid\r\n-j,j job control s signal --group --user --sid --rows --info\r\n-O,O preloaded -o v virtual memory --cumulative --format --deselect\r\n-l,l long u user-oriented --sort --tty --forest --version\r\n-F extra full X registers --heading --no-heading --context\r\n********* misc options *********\r\n-V,V show version L list format codes f ASCII art forest\r\n-m,m,-L,-T,H threads S children in sum -y change -l format\r\n-M,Z security data c true command name -c scheduling class\r\n-w,w wide output n numeric WCHAN,UID -H process hierarchy\r\n[root@361way ~]# ansible 10.212.52.252 -m raw -a 'ps auxf|grep snmp'\r\n10.212.52.252 | success | rc=0 >>\r\nroot 5580 25.0 0.0 12876 1792 pts\/2 Ss+ 12:36 0:00 \\_ bash -c ps auxf|grep snmp\r\nroot 5607 0.0 0.0 5720 832 pts\/2 S+ 12:36 0:00 \\_ grep snmp\r\nroot 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 \/usr\/sbin\/snmpd -r -A -LF i \/var\/log\/net-snmpd.log\r\n -p \/var\/run\/snmpd.pid\r\n[root@361way ~]# ansible 10.212.52.252 -m shell -a 'ps auxf|grep snmp'\r\n10.212.52.252 | success | rc=0 >>\r\nroot 5803 0.0 0.0 11308 1308 pts\/2 S+ 12:36 0:00 \\_ \/bin\/sh -c ps auxf|grep snmp\r\nroot 5805 0.0 0.0 4260 572 pts\/2 S+ 12:36 0:00 \\_ grep snmp\r\nroot 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 \/usr\/sbin\/snmpd -r -A -LF i \/var\/log\/net-snmpd.log\r\n -p \/var\/run\/snmpd.pid<\/pre>\n\u4e0a\u9762\u7684\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u8fd9\u91cc\u52a0\u4e86\u7ba1\u9053\uff0ccommand\u6a21\u5757\u6267\u884c\u65f6\u51fa\u9519\uff0c\u800c\u4f7f\u7528raw\u6a21\u5757\u548cshell \u6a21\u5757\u90fd\u6b63\u5e38\u3002<\/p>\n
\u4f7f\u7528chdir\u7684\u793a\u4f8b\uff1a<\/strong><\/p>\n
[root@361way ~]# ansible 10.212.52.252 -m command -a 'chdir=\/tmp\/361way touch test.file'\r\n10.212.52.252 | success | rc=0 >>\r\n[root@361way ~]# ansible 10.212.52.252 -m shell -a 'chdir=\/tmp\/361way touch test2.file'\r\n10.212.52.252 | success | rc=0 >>\r\n[root@361way ~]# ansible 10.212.52.252 -m raw -a 'chdir=\/tmp\/361way touch test3.file'\r\n10.212.52.252 | success | rc=0 >>\r\n<\/pre>\n\u4ece\u4e0a\u9762\u6267\u884c\u7ed3\u679c\u6765\u770b\uff0c\u4e09\u4e2a\u547d\u4ee4\u90fd\u6267\u884c\u6210\u529f\u4e86\u3002\u4e0d\u8fc7\u901a\u8fc7\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u67e5\u770b\uff0c\u524d\u4e24\u4e2a\u6587\u4ef6\u88ab\u6210\u529f\u521b\u5efa\uff1a<\/p>\n
linux-wdh1:\/tmp\/361way # ls \/tmp\/361way\r\ntest.file test2.file\r\n<\/pre>\n\u4f7f\u7528raw\u6a21\u5757\u7684\u6267\u884c\u7684\u7ed3\u679c\u6587\u4ef6\u4e5f\u88ab\u6b63\u5e38\u521b\u5efa\u4e86\uff0c\u4e0d\u8fc7\u4e0d\u662f\u5728chdir \u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u800c\u662f\u5728\u5f53\u524d\u6267\u884c\u7528\u6237\u7684\u5bb6\u76ee\u5f55\u3002<\/p>\n
linux-wdh1:~ # ls ~\/test3.file\r\n\/root\/test3.file\r\n<\/pre>\ncreates\u4e0eremoves\u793a\u4f8b\uff1a<\/strong><\/p>\n
\u8fd9\u91cc\u6211\u5728\u6d4b\u8bd5\u4e3b\u673a\u4e0a\u521b\u5efa\/tmp\/361way\/server.txt\u6587\u4ef6\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n
[root@361way ~]# ansible 10.212.52.252 -a 'creates=\/tmp\/361way\/server.txt uptime'\r\n10.212.52.252 | success | rc=0 >>\r\nskipped, since \/tmp\/361way\/server.txt exists\r\n[root@361way ~]# ansible 10.212.52.252 -a 'removes=\/tmp\/361way\/server.txt uptime'\r\n10.212.52.252 | success | rc=0 >>\r\n15:11pm up 28 days 0:34, 2 users, load average: 0.75, 0.46, 0.39\r\n<\/pre>\nscript\u6a21\u5757\u793a\u4f8b\uff1a<\/strong>
\n[root@361way ~]# cat script.sh
\n#!\/bin\/bash
\ndf -hl
\nifconfig
\nps auxf|grep snmp
\n[root@361way ~]# ansible 10.212.52.252 -m script -a 'scrip.sh'
\n10.212.52.252 | FAILED => file or module does not exist: \/root\/scrip.sh
\n[root@361way ~]# ansible 10.212.52.252 -m script -a 'script.sh'
\n10.212.52.252 | success >> {
\n\"changed\": true,
\n\"rc\": 0,
\n\"stderr\": \"OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013\\ndebug1: Reading configuration data \/etc\/ssh\/ssh_config\\r\\ndebug1: Applying options for *\\r\\ndebug1: auto-mux: Trying existing master\\r\\nControl socket connect(\/root\/.ansible\/cp\/ansible-ssh-10.212.52.252-22-root): Connection refused\\r\\ndebug1: Connecting to 10.212.52.252 [10.212.52.252] port 22.\\r\\ndebug1: fd 3 clearing O_NONBLOCK\\r\\ndebug1: Connection established.\\r\\ndebug1: permanently_set_uid: 0\/0\\r\\ndebug1: identity file \/root\/.ssh\/identity type -1\\r\\ndebug1: identity file \/root\/.ssh\/identity-cert type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_rsa type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_rsa-cert type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_dsa type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_dsa-cert type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_ecdsa type -1\\r\\ndebug1: identity file \/root\/.ssh\/id_ecdsa-cert type -1\\r\\ndebug1: Remote protocol version 2.0, remote software version OpenSSH_6.2\\r\\ndebug1: match: OpenSSH_6.2 pat OpenSSH*\\r\\ndebug1: Enabling compatibility mode for protocol 2.0\\r\\ndebug1: Local version string SSH-2.0-OpenSSH_5.3\\r\\ndebug1: SSH2_MSG_KEXINIT sent\\r\\ndebug1: SSH2_MSG_KEXINIT received\\r\\ndebug1: kex: server->client aes128-ctr hmac-md5 zlib@openssh.com\\r\\ndebug1: kex: client->server aes128-ctr hmac-md5 zlib@openssh.com\\r\\ndebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent\\r\\ndebug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP\\r\\ndebug1: SSH2_MSG_KEX_DH_GEX_INIT sent\\r\\ndebug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY\\r\\ndebug1: Host '10.212.52.252' is known and matches the RSA host key.\\r\\ndebug1: Found key in \/root\/.ssh\/known_hosts:1\\r\\ndebug1: ssh_rsa_verify: signature correct\\r\\ndebug1: SSH2_MSG_NEWKEYS sent\\r\\ndebug1: expecting SSH2_MSG_NEWKEYS\\r\\ndebug1: SSH2_MSG_NEWKEYS received\\r\\ndebug1: SSH2_MSG_SERVICE_REQUEST sent\\r\\ndebug1: SSH2_MSG_SERVICE_ACCEPT received\\r\\ndebug1: Authentications that can continue: publickey,password,keyboard-interactive\\r\\ndebug1: Next authentication method: keyboard-interactive\\r\\ndebug1: Enabling compression at level 6.\\r\\ndebug1: Authentication succeeded (keyboard-interactive).\\r\\ndebug1: setting up multiplex master socket\\r\\nControlSocket \/root\/.ansible\/cp\/ansible-ssh-10.212.52.252-22-root already exists, disabling multiplexing\\r\\ndebug1: channel 0: new [client-session]\\r\\ndebug1: Requesting no-more-sessions@openssh.com\\r\\ndebug1: Entering interactive session.\\r\\ndebug1: Sending environment.\\r\\ndebug1: Sending env LANG = en_US.UTF-8\\r\\ndebug1: Sending command: LANG=C LC_CTYPE=C \/root\/.ansible\/tmp\/ansible-tmp-1431924855.88-242473611260231\/script.sh \\r\\ndebug1: client_input_channel_req: channel 0 rtype exit-status reply 0\\r\\ndebug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0\\r\\ndebug1: channel 0: free: client-session, nchannels 1\\r\\ndebug1: fd 1 clearing O_NONBLOCK\\r\\ndebug1: fd 2 clearing O_NONBLOCK\\r\\nConnection to 10.212.52.252 closed.\\r\\nTransferred: sent 1928, received 3920 bytes, in 0.1 seconds\\r\\nBytes per second: sent 37017.0, received 75262.7\\r\\ndebug1: Exit status 0\\r\\ndebug1: compress outgoing: raw data 537, compressed 375, factor 0.70\\r\\ndebug1: compress incoming: raw data 1837, compressed 1019, factor 0.55\\r\\n\",
\n\"stdout\": \"Filesystem Size Used Avail Use% Mounted on\\r\\n\/dev\/sda2 9.9G 872M 8.5G 10% \/\\r\\nudev 3.9G 128K 3.9G 1% \/dev\\r\\ntmpfs 3.9G 76K 3.9G 1% \/dev\/shm\\r\\n\/dev\/sda3 5.0G 219M 4.5G 5% \/boot\\r\\n\/dev\/sda8 40G 15G 23G 40% \/home\\r\\n\/dev\/sda9 9.9G 5.2G 4.3G 55% \/opt\\r\\n\/dev\/sda6 5.0G 2.7G 2.1G 57% \/tmp\\r\\n\/dev\/sda5 9.9G 3.4G 6.0G 36% \/usr\\r\\n\/dev\/sda7 9.9G 823M 8.6G 9% \/var\\r\\neth0 Link encap:Ethernet HWaddr 00:50:56:A8:65:7E \\r\\n inet addr:10.212.52.252 Bcast:10.212.52.255 Mask:255.255.255.0\\r\\n inet6 addr: fe80::250:56ff:fea8:657e\/64 Scope:Link\\r\\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\\r\\n RX packets:24112135 errors:0 dropped:792372 overruns:0 frame:0\\r\\n TX packets:10697339 errors:0 dropped:0 overruns:0 carrier:0\\r\\n collisions:0 txqueuelen:1000 \\r\\n RX bytes:17137233328 (16343.3 Mb) TX bytes:13390377826 (12770.0 Mb)\\r\\n\\r\\nlo Link encap:Local Loopback \\r\\n inet addr:127.0.0.1 Mask:255.0.0.0\\r\\n inet6 addr: ::1\/128 Scope:Host\\r\\n UP LOOPBACK RUNNING MTU:16436 Metric:1\\r\\n RX packets:3407332 errors:0 dropped:0 overruns:0 frame:0\\r\\n TX packets:3407332 errors:0 dropped:0 overruns:0 carrier:0\\r\\n collisions:0 txqueuelen:0 \\r\\n RX bytes:262675450 (250.5 Mb) TX bytes:262675450 (250.5 Mb)\\r\\n\\r\\nroot 25332 0.0 0.0 4260 568 pts\/2 S+ 12:54 0:00 \\\\_ grep snmp\\r\\nroot 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 \/usr\/sbin\/snmpd -r -A -LF i \/var\/log\/net-snmpd.log -p \/var\/run\/snmpd.pid\\r\\n\"
\n}
\n<\/code>
\n\u8f93\u51fa\u7ed3\u679c\u5f88\u591a\uff0c\u770b\u8d77\u6765\u4e5f\u5f88\u4e71\uff0c\u4e0d\u8fc7\u67e5\u4e0bstdout\u90e8\u5206\uff0c\u8fd9\u4e2a\u90e8\u5206\u662f\u5b9e\u9645\u4e0a\u6267\u884c\u540e\u7684\u7ed3\u679c\u3002\u8fd9\u91cc\u53ef\u4ee5\u914d\u5408\u7ba1\u9053\u4e00\u8d77\u4f7f\u7528\uff0c\u53ef\u4ee5\u5982\u4e0b\u4f7f\u7528\uff1a<\/p>\n[root@361way ~]# ansible 10.212.52.252 -m script -a 'script.sh' |egrep '>>|stdout'\r\n<\/pre>\n\u7bc7\u5e45\u6240\u9650\uff0c\u672c\u6765\u60f3\u628a\u5e38\u7528\u6a21\u5757\u90fd\u653e\u5728\u8be5\u7bc7\u6765\u5199\uff0c\u611f\u89c9\u592a\u5197\u957f\uff0c\u540e\u9762\u518d\u5355\u72ec\u5206\u5f00\u76f8\u5e94\u7684\u7bc7\u5e45\u505a\u6a21\u5757\u7684\u4ecb\u7ecd\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.361way.com\/ad-hoc-commands-modules\/4408.html<\/a><\/p>\n