欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

关于 Ansible 的模块 ⑦ - 命令模块

最编程 2024-04-19 07:16:46
...

ansible的默认模块是command,可以使用-m command指定使用command模块也可以直接省略。

command在远程节点上执行命令和shell模块类似,但不支持$HOME、"<"、">"、"|"、";"、"&"等操作。

1. 常用参数

参数 默认值 含义
cmd null 要运行的命令
chdir null 在运行命令之前,先进入该目录
creates null 如文件存在,则不运行此步骤
removes null 如文件存在,则运行此步骤
argv null 以list而非string模式传递命令
free_form null shell模块接受一个*形式的命令作为字符串来运行
stdin null 将命令的stdin直接设置为指定的值
stdin_add_newline true 是否向标准输入数据追加换行符
strip_empty_ends true 在结果中去除 stdout/stderr 末尾的空行。
warn 是否启用任务告警

2. 使用示例

2.1 简单执行命令

ansible all -m command -a "free -h"
ansible all -a "free -h"   #省略-m参数默认就使用command模块

2.2 command模块与shell模块一样支持执行命令前更换目录

ansible all -m command  -a "chdir=/tmp  cat shell.txt"

2.3 不支持">"等符号

ansible all -m command -a 'echo "this is command test">/tmp/cmd.txt'