商城首页欢迎来到中国正版软件门户

您的位置:首页 >Ansible Shell 模块

Ansible Shell 模块

  发布于2026-08-13 阅读(0)

扫一扫,手机访问

{"position":0,"title":"介绍","layout":"doc-fullscreen","text":"## 介绍nn在本实验中,你将学习如何使用 Ansible 的 Shell 模块在远程主机上执行 shell 命令。当现有 Ansible 模块无法覆盖你需要运行的 shell 命令,或者你需要对执行过程有更高的灵活性和控制时,Shell 模块非常有用。n","need_verify":false,"has_solution":false},{"position":1,"title":"执行一个简单的 Shell 命令","layout":"doc-workbench-split","text":"## 执行一个简单的 Shell 命令nn在这一步中,你将使用 Ansible 的 Shell 模块在远程主机上执行一个简单的 shell 命令。nn首先,完成 `/home/labex/project/simple_shell_command.yml` 文件。 n在 playbook 文件中添加以下内容:nn```yamln- name: Execute a Simple Shell Commandn hosts: localhostn gather_facts: falsenn tasks:n - name: Execute ls -l commandn shell: ls -ln register: command_outputnn - name: Display command outputn debug:n var: command_output.stdout_linesn```nn- `hosts: localhost`:执行此 Ansible playbook 的目标主机是 `localhost`。n- `gather_facts: false`:禁用主机信息的收集。n- `shell: ls -l`:指定要执行的命令。nn总结来说,此 playbook 的目的是在本地主机上执行 `ls -l` 命令并显示命令的输出。nn然后,在 Ansible playbook 中显示命令的输出。nn```bashnansible-playbook /home/labex/project/simple_shell_command.ymln```nn示例输出:nn```bashn[WARNING]: No inventory was parsed, only implicit localhost is a vailablen[WARNING]: provided hosts list is empty, only localhost is a vailable. Note thatnthe implicit localhost does not match 'all'nnPLAY [Execute a Simple Shell Command] ******************************************nnTASK [Execute ls -l command] ***************************************************nchanged: [localhost]nnTASK [Display command output] **************************************************nok: [localhost] => {n "command_output.stdout_lines": [n "total 16",n "-rwxrwxrwx 1 labex root 285 Mar 8 13:33 complex_shell_commands.yml",n "-rwxrwxrwx 1 labex root 221 Mar 8 13:33 handle_command_failure.yml",n "-rwxrwxrwx 1 labex root 222 Mar 8 13:33 pass_variables.yml",n "-rwxrwxrwx 1 labex root 266 Mar 8 13:36 simple_shell_command.yml"n ]n}nnPLAY RECAP *********************************************************************nlocalhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0n```nn这里的 `"command_output.stdout_lines":[...]` 是目标主机上 `ls -l` 命令的输出。n","need_verify":true,"has_solution":false},{"position":2,"title":"向 Shell 命令传递变量","layout":"doc-workbench-split","text":"## 向 Shell 命令传递变量nn在这一步中,你将学习如何从 Ansible playbook 向使用 Shell 模块执行的 shell 命令传递变量。nn首先,完成 `/home/labex/project/pass_variables.yml 文件。 在 playbook 文件中加入以下内容: ```yaml - name: Pass Variables to Shell Commands hosts: localhost gather_facts: false vars: directory_path: /home/labex tasks: - name: Execute ls command with a variable shell: ls "{{ directory_path }}" register: variable_command_output - name: Display variable command output debug: var: variable_command_output.stdout_lines ``` - `hosts: localhost`:表示这个 Ansible playbook 的目标主机是 `localhost`。 - `vars`:定义了一个名为 `directory_path` 的变量,值为 `/home/labex`。 - `shell: ls "{{ directory_path }}"`:执行 `ls` 命令,输出由 `directory_path` 变量指定的目录内容。 总的来说,这个 playbook 会在本地主机上使用表示目录路径的变量(`/home/labex`)执行 `ls` 命令,并显示命令输出。 然后,在 Ansible playbook 中显示命令的输出。 ```bash ansible-playbook /home/labex/project/pass_variables.yml ``` 示例输出:nn```bashn[WARNING]: No inventory was parsed, only implicit localhost is a vailablen[WARNING]: provided hosts list is empty, only localhost is a vailable. Note thatnthe implicit localhost does not match 'all'nnPLAY [Pass Variables to Shell Commands] ****************************************nnTASK [Execute ls command with a variable] **************************************nchanged: [localhost]nnTASK [Display variable command output] *****************************************nok: [localhost] => {n "variable_command_output.stdout_lines": [n "Code",n "Desktop",n "golang",n "project"n ]n}nnPLAY RECAP *********************************************************************nlocalhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0n```nn这里的 `"variable_command_output.stdout_lines": [...]` 是目标主机上 `ls /home/labex` 命令的输出。n","need_verify":true,"has_solution":false},{"position":3,"title":"处理命令失败","layout":"doc-workbench-split","text":"## 处理命令失败nn在这一步中,你将学习如何在使用 Shell 模块执行 shell 命令时处理命令失败的情况。你将处理 shell 命令返回非零退出状态(表示失败)的情况。nn首先,完成 `/home/labex/project/handle_command_failure.yml` 文件。 n在 playbook 文件中添加以下内容:nn```yamln- name: Handle Command Failuren hosts: localhostn gather_facts: falsenn tasks:n - name: Execute a failing commandn shell: failing-commandn register: failing_command_outputn ignore_errors: yesnn - name: Handle command failuren debug:n msg: "The command failed. Performing fallback action."n when: failing_command_output.failedn```nn- `hosts: localhost`:执行此 Ansible playbook 的目标主机是 `localhost`。n- `shell: failing-command`:这一行使用 shell 模块执行命令 `failing-command`。该命令的输出将被捕获并存储在 `failing_command_output` 变量中。由于 `failing-command` 脚本不存在,此命令必定会失败。n- `ignore_errors: yes`:这一行指示 Ansible 忽略执行 `failing-command` 时发生的任何错误。这使得 playbook 在命令失败后仍能继续执行。n- `debug`:此模块使用 debug 模块显示一条消息,内容为 "The command failed. Performing fallback action."。此消息仅在 `failing_command_output.failed` 条件满足时显示,表示之前的命令执行失败。nn总结来说,此 playbook 尝试执行一个预期会失败的命令,忽略执行过程中发生的任何错误,然后根据命令的失败状态显示一条消息,表明命令失败并执行回退操作。nn然后,在 Ansible playbook 中显示命令的输出。nn```bashnansible-playbook /home/labex/project/handle_command_failure.ymln```nn示例输出:nn```bashn[WARNING]: No inventory was parsed, only implicit localhost is a vailablen[WARNING]: provided hosts list is empty, only localhost is a vailable. Note thatnthe implicit localhost does not match 'all'nnPLAY [Handle Command Failure] **************************************************nnTASK [Execute a failing command] ***********************************************nfatal: [localhost]: FAILED! => {"changed": true, "cmd": "failing-command", "delta": "0:00:00.009169", "end": "2024-03-08 13:46:22.701946", "msg": "non-zero return code", "rc": 127, "start": "2024-03-08 13:46:22.692777", "stderr": "/bin/sh: line 1: failing-command: command not found", "stderr_lines": ["/bin/sh: line 1: failing-command: command not found"], "stdout": "", "stdout_lines": []}n...ignoringnnTASK [Handle command failure] **************************************************nok: [localhost] => {n "msg": "The command failed. Performing fallback action."n}nnPLAY RECAP *********************************************************************nlocalhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1n```nn`"msg": "The command failed. Performing fallback action."` 的存在表明 `failing-command` 命令的执行失败。n","need_verify":true,"has_solution":false},{"position":4,"title":"执行复杂的 Shell 命令","layout":"doc-workbench-split","text":"## 执行复杂的 Shell 命令nn在这一步中,你将使用 Shell 模块执行更复杂的 shell 命令。你将探索命令重定向、管道和复杂命令结构的用法。nn首先,完成 `/home/labex/project/complex_shell_commands.yml` 文件。 n在 playbook 文件中添加以下内容:nn```yamln- name: Execute Complex Shell Commandsn hosts: localhostn gather_facts: falsenn tasks:n - name: Execute complex commandn shell: ls -l /home/labex | grep 'project' > /home/labex/output.txtn register: complex_command_outputnn - name: Display complex command outputn debug:n var: complex_command_output.stdout_linesn```nn- `hosts: localhost`:执行此 Ansible playbook 的目标主机是 `localhost`。n- `shell`:将 `ls -l /home/labex` 的输出重定向到 `/home/labex/output.txt` 文件,并使用 `grep` 过滤与 `project` 相关的内容。nn总结来说,此 playbook 在本地主机上执行一个复杂的 shell 命令,过滤并将输出重定向到文件,然后显示命令执行的输出。nn然后,执行 Ansible playbook 命令。nn```bashnansible-playbook /home/labex/project/complex_shell_commands.ymln```nn示例输出:nn```bashn[WARNING]: No inventory was parsed, only implicit localhost is a vailablen[WARNING]: provided hosts list is empty, only localhost is a vailable. Note thatnthe implicit localhost does not match 'all'nnPLAY [Execute Complex Shell Commands] ******************************************nnTASK [Execute complex command] *************************************************nchanged: [localhost]nnTASK [Display complex command output] ******************************************nok: [localhost] => {n "complex_command_output.stdout_lines": []n}nnPLAY RECAP *********************************************************************nlocalhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0n```nn最后,查看 `/home/labex/output.txt` 文件内容:nn```bashncat /home/labex/output.txtn```nn示例输出:nn```bashndrwxr-xr-x 1 labex labex 4096 Mar 8 13:49 projectn```n","need_verify":true,"has_solution":false},{"position":5,"title":"总结","layout":"doc-fullscreen","text":"## 总结nn恭喜!你已经成功完成了 Ansible Shell 模块的实验。你学会了如何使用 Shell 模块在远程主机上执行 shell 命令、向命令传递变量、处理命令失败以及执行复杂的 shell 命令。nnShell 模块为你在 Ansible playbook 中执行 shell 命令提供了极大的灵活性和控制力。然而,在使用时需要谨慎,因为运行任意的 shell 命令可能会带来安全隐患。请始终确保你信任这些命令,并对任何用户提供的输入进行清理。nn现在你已经对 Shell 模块有了很好的理解,可以利用它来执行各种自动化任务并高效管理你的远程主机。祝你编写 Ansible 脚本愉快!n","need_verify":false,"has_solution":false}
本文转载于:https://labex.io/zh/tutorials/ansible-ansible-shell-module-289409 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注