Ansible是一款强大的自动化运维工具,通过其模块化的设计,可以方便地管理和配置远程主机。作为Ansible的一个常用模块,shell 模块使得我们可以在目标主机上执行复杂的命令或脚本。无论是单一的命令,还是复杂的Shell脚本, Ansible shell 模块都能够轻松处理。

本文将从入门到精通,全面讲解 Ansible shell模块的使用,并结合最佳实践,帮助你更高效地使用这个模块执行自动化任务。
Ansible shell模块概述
Ansible的shell 模块允许我们在目标主机上执行任意的Shell命令。它支持标准的 Shell命令语法,包括管道、重定向等操作,因此非常适合处理一些需要复杂命令或脚本的场景。
shell 模块的基本语法
ansible <host-pattern> -m shell -a '<command>'
<host-pattern>: 要执行命令的主机或主机组,可以是单个主机、多个主机或主机组。
-m shell: 指定使用shell模块执行命令。
-a '<command>': 通过-a传递要执行的命令,这里是uptime。
还可以使用ansible-playbook方式,执行shell:
- name: Run a shell commandhosts: alltasks:- name: Run a command to check disk spaceshell: <command> # "df -h"register: disk_space- name: Show the outputdebug:var: disk_space.stdout
<command>是你希望在目标主机上执行的Shell命令,例如:df -h。与其他
Ansible模块不同,shell模块通常需要在命令字符串中明确写出所需的操作。
register: disk_space捕获命令df -h的返回结果,并将其存储在disk_space变量中。
shell 模块的常用参数
-
chdir:指定在执行命令前切换到的目录。 -
creates:如果该文件或目录已经存在,则不会执行该命令。适用于防止重复执行任务。 -
removes:与creates类似,但如果文件或目录不存在,命令才会执行。
基础用法:执行简单命令
示例 1:执行 uptime 命令
使用 ansible -m shell -a 'uptime' 命令可以直接在Ansible管理的远程主机上执行 uptime 命令。该命令使用了shell模块,并通过-a参数传递了命令参数。 成功执行如下所示:
root@ansible:~/shell# ansible hp -m shell -a 'uptime'
192.168.31.232 | CHANGED | rc=0 >>12:11:06 up 17 min, 2 users, load average: 0.77, 0.66, 0.44
192.168.31.231 | CHANGED | rc=0 >>12:11:06 up 17 min, 2 users, load average: 0.77, 0.66, 0.44
使用ansible-palybook执行,先要编写yml文件,内容如下:
- name: Execute a simple shell commandhosts: alltasks:- name: Run `uptime` commandshell: uptimeregister: uptime_output- name: Show the outputdebug:var: uptime_output.stdout
上述内容使用
shell模块,执行uptime,把输出的结果存入uptime_output,并以标准输出到终端。

此示例在所有目标主机上执行 uptime 命令,获取系统运行时间。
示例 2:运行多个命令
Ansible的shell模块支持执行多个命令,可以通过管道 (&& 或 ;) 来串联命令。

还可以通过ansible-palybook执行
- name: Run multiple shell commandshosts: hptasks:- name: Check disk space and system loadshell: "df -h && uptime"register: output_vars- name: Show the outputdebug:var: output_vars.stdout
在该示例中,df -h 用于查看磁盘空间,执行完后再执行 uptime 来查看系统负载。

进阶应用:多行命令与脚本执行
shell模块支持多行命令的执行,允许你在一个任务中运行一个完整的Shell脚本。
示例 3:执行多行命令
- name: Execute a multi-line shell scripthosts: alltasks:- name: Run setup scriptshell: |echo "Starting setup..."mkdir -p /tmp/setupcd /tmp/setupcurl -O https://example.com/setup.shchmod +x setup.sh./setup.sh
在这个示例中,| 符号表示一个多行命令,任务会:
-
创建目录
/tmp/setup -
下载脚本
setup.sh -
为脚本添加执行权限
-
执行该脚本
示例 4:使用条件判断执行命令
shell 模块还可以结合条件判断来执行命令。如下所示,只有在 /tmp/mydir 不存在时才会创建该目录。
- name: Run a conditional shell commandhosts: alltasks:- name: Check if a directory exists and create itshell: |if [ ! -d "/tmp/mydir" ]; thenmkdir /tmp/mydirfi
高级功能:避免重复执行和工作目录
示例 5:避免重复执行命令
有时我们不希望每次运行Playbook时都执行相同的命令。creates参数可以帮助我们避免这种情况。
- name: Run a command only if a file does not existhosts: alltasks:- name: Create a file if it doesn't already existshell: touch /tmp/example.txtargs:creates: /tmp/example.txt
如果 /tmp/example.txt 文件已经存在,任务就不会执行,从而避免重复创建文件。
示例 6:在特定目录中执行命令
通过 chdir 参数,可以指定命令执行的工作目录。这对于需要在某个项目目录中执行命令时非常有用。
- name: Execute command in a specific directoryhosts: alltasks:- name: Pull the latest code from Gitshell: git pullargs:chdir: /path/to/project
该任务会进入 /path/to/project 目录,并执行 git pull 命令更新代码。
常见问题与最佳实践
使用 shell 模块时的注意事项
-
避免执行简单命令:对于简单的命令,尽量使用
command模块,它比shell模块更加安全和高效。command模块不会在命令行中处理任何Shell特性(如管道、重定向等),因此对于简单任务,建议优先选择command。 -
确保幂等性:
Ansible的任务应当是幂等的,即任务在多次执行时不会产生副作用。对于需要执行的命令,最好通过条件判断来确保只有在必要时才执行。 -
避免在命令中暴露敏感信息:如果命令中包含敏感信息(如密码),尽量避免将其硬编码在
Playbook中。可以考虑使用Ansible Vault来加密敏感信息。
性能优化
-
减少命令的执行次数:通过合理利用
creates或removes参数,避免不必要的命令执行,提升Playbook的执行效率。 -
合理分割任务:将较为复杂的脚本分割成多个任务,确保每个任务的执行目标单一,便于排查问题。
总结
Ansible的 shell 模块提供了强大的功能,使得我们能够在目标主机上执行复杂的命令和脚本。通过合理使用 shell 模块的多种特性,可以大大简化自动化运维工作,提升生产效率。
推荐阅读
- 深入了解Ansible配置与主机清单:轻松管理自动化任务
- 效率提升神器:Ansible入门,开启自动化运维之旅
- Kubernetes 镜像拉取策略全解析:如何根据需求选择最佳配置?
- ncdu:Linux磁盘分析的利器,你值得拥有
- Linux磁盘爆满?别慌!这几个命令轻松搞定
- fnOS搭建音乐服务Navidrome