ansible script模块用来为远端目标主机运行管理机(本地)的脚本
参数 | 是否必须 | 默认 | 选项 | 说明 |
creates | no | 当文件存在,这步不运行 | ||
free_form | yes | 运行参数 | ||
removes | no | 当文件不存在时,这步不允许 |
- 运行脚本示例
# cat ckl.sh #!/bin/bash ps -ef | grep httpd | grep -v grep date echo "*******************"
# ansible webserver -m script -a "/root/ckl.sh" 172.16.110.48 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.110.48 closed.\r\n", "stdout": "root 44242 1 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44243 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44244 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44245 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44246 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44247 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\nWed Dec 28 23:09:14 CST 2016\r\n*******************\r\n", "stdout_lines": [ "root 44242 1 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44243 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44244 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44245 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44246 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44247 44242 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "Wed Dec 28 23:09:14 CST 2016", "*******************" ] } 172.16.110.47 | SUCCESS => { "changed": true, "rc": 0,
"stderr": "Shared connection to 172.16.110.47 closed.\r\n", "stdout": "root 44301 1 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44303 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44304 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44305 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44306 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache 44307 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND\r\nWed Dec 28 23:09:14 CST 2016\r\n*******************\r\n", "stdout_lines": [ "root 44301 1 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44303 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44304 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44305 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44306 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "apache 44307 44301 0 22:30 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND", "Wed Dec 28 23:09:14 CST 2016", "*******************" ] }
2.添加参数的脚本
# cat ckl.sh #!/bin/bash ARG=$1 ps -ef | grep $1 | grep -v grep date echo "*******************"
# ansible webserver -m script -a "/root/ckl.sh sshd free_form=yes" 172.16.110.47 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.110.47 closed.\r\n", "stdout": "root 1023 1 0 Dec27 ? 00:00:00 /usr/sbin/sshd\r\nroot 10338 1023 0 Dec27 ? 00:00:00 sshd: [email protected]/0\r\nroot 46439 1023 0 23:15 ? 00:00:00 sshd: [email protected]/1\r\nroot 46517 46439 0 23:15 pts/1 00:00:00 /bin/sh -c /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes && sleep 0\r\nroot 46529 46517 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes\r\nWed Dec 28 23:15:54 CST 2016\r\n*******************\r\n", "stdout_lines": [ "root 1023 1 0 Dec27 ? 00:00:00 /usr/sbin/sshd", "root 10338 1023 0 Dec27 ? 00:00:00 sshd: [email protected]/0", "root 46439 1023 0 23:15 ? 00:00:00 sshd: [email protected]/1", "root 46517 46439 0 23:15 pts/1 00:00:00 /bin/sh -c /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes && sleep 0", "root 46529 46517 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes", "Wed Dec 28 23:15:54 CST 2016", "*******************" ] } 172.16.110.48 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.110.48 closed.\r\n", "stdout": "root 1019 1 0 Dec27 ? 00:00:00 /usr/sbin/sshd\r\nroot 43902 1019 0 21:43 ? 00:00:00 sshd: [email protected]/0\r\nroot 46357 1019 0 23:15 ? 00:00:00 sshd: [email protected]/1\r\nroot 46435 46357 0 23:15 pts/1 00:00:00 /bin/sh -c /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes && sleep 0\r\nroot 46447 46435 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nroot 46449 46447 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nroot 46450 46447 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nWed Dec 28 23:15:54 CST 2016\r\n*******************\r\n", "stdout_lines": [ "root 1019 1 0 Dec27 ? 00:00:00 /usr/sbin/sshd", "root 43902 1019 0 21:43 ? 00:00:00 sshd: [email protected]/0", "root 46357 1019 0 23:15 ? 00:00:00 sshd: [email protected]/1", "root 46435 46357 0 23:15 pts/1 00:00:00 /bin/sh -c /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes && sleep 0", "root 46447 46435 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", "root 46449 46447 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", "root 46450 46447 0 23:15 pts/1 00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", "Wed Dec 28 23:15:54 CST 2016", "*******************" ] }
时间: 2024-08-08 08:14:46