实例学习ansible系列 4 常用模块之command/shell/raw

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

知识点:使用module command或者shell或者raw都能调用对象机器上的某条指令或者某个可执行文件。

使用方法

[root@host31 ~]# ansible localhost -m command -a "echo hello"localhost | SUCCESS | rc=0 >>hello[root@host31 ~]# ansible localhost -m shell -a "echo hello"localhost | SUCCESS | rc=0 >>hello[root@host31 ~]# ansible localhost -m raw -a "echo hello"localhost | SUCCESS | rc=0 >>hello[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

是否支持管道

               
module是否支持管道
command不支持管道
shell支持管道
raw支持管道
[root@host31 ~]# ansible localhost -m command -a "ps -ef |wc -l"localhost | FAILED | rc=1 >>error: garbage optionUsage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>'  or 'ps --help <s|l|o|t|m|a>' for additional help text.For more details see ps(1).[root@host31 ~]# ansible localhost -m shell -a "ps -ef |wc -l"localhost | SUCCESS | rc=0 >>448[root@host31 ~]# ansible localhost -m raw -a "ps -ef |wc -l"localhost | SUCCESS | rc=0 >>445[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

直接执行某个文件

[root@host31 ~]# ansible host32 -m command -a "/tmp/ttt.sh"host32 | SUCCESS | rc=0 >>hello world[root@host31 ~]# ansible host32 -m raw -a "/tmp/ttt.sh"host32 | SUCCESS | rc=0 >>hello world[root@host31 ~]# ansible host32 -m shell -a "/tmp/ttt.sh"host32 | SUCCESS | rc=0 >>hello world[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
 

都是支持的,但是需要注意/tmp/ttt.sh应该有执行权限

ansible-doc -s取得更详细信息

 

希望知道更加详细的module的信息,最好的方法是使用ansible自带的ansible-doc的-s选项

[root@host31 ~]# ansible-doc -s raw- name: Executes a low-down and dirty SSH command  action: raw      executable             # change the shell used to execute the command. Should be an absolute path to the executable.      free_form=             # the raw module takes a free form command to run[root@host31 ~]#[root@host31 ~]# ansible-doc -s shell- name: Execute commands in nodes.  action: shell      chdir                  # cd into this directory before running the command      creates                # a filename, when it already exists, this step will *not* be run.      executable             # change the shell used to execute the command. Should be an absolute path to the executable.      free_form=             # The shell module takes a free form command to run, as a string.  There's not an actual option                               named "free form".  See the examples!      removes                # a filename, when it does not exist, this step will *not* be run.      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to                               no/false.[root@host31 ~]#[root@host31 ~]# ansible-doc -s command- name: Executes a command on a remote node  action: command      chdir                  # cd into this directory before running the command      creates                # a filename or (since 2.0) glob pattern, when it already exists, this step will *not* be run.      executable             # change the shell used to execute the command. Should be an absolute path to the executable.      free_form=             # the command module takes a free form command to run.  There is no parameter actually named 'free                               form'. See the examples!      removes                # a filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be run.      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to                               no/false.[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值