command模块
command模块为ansible默认模块,不指定-m参数时,使用的就是command模块;
comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,运行中的命令不支持变量使用以及不支持管道;
示例:显示当前用户
[[email protected] ~]# ansible all -a ‘whoami‘ db | SUCCESS | rc=0 >> root web | SUCCESS | rc=0 >> root 192.168.33.132 | SUCCESS | rc=0 >> root
command用法
[[email protected] ~]# ansible-doc -s command less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less - 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.
- name: 在远程节点执行命令
action: command
chdir # 在执行命令之前,先切换到该目录
creates # 一个文件名,当这个文件存在,则该命令不执行
executable # 切换shell来执行命令,需要使用命令的绝对路径
free_form= #要执行的Linux指令,一般使用Ansible的-a参数代替。
removes #一个文件名,这个文件不存在,则该命令不执行
raw,类似command,但可以传递管道
用法
[[email protected] ~]# ansible-doc -s raw less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less - 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
示例,查看web服务器上swap状态
[[email protected] ~]# ansible web -m raw -a ‘free -m|grep Swap‘ web | SUCCESS | rc=0 >> Swap: 2047 0 2047
时间: 2024-10-25 02:53:58