展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 系统应用 > Ansible Shell 模块

Ansible Shell 模块

{"position":0,"title":"介绍","layout":"doc-fullscreen","text":"## 介绍nn在本实验中,你将学习如何使用 Ansible 的 Shell 模块在远程主机上执行 shell 命令。当现有 Ansible 模块无法覆盖你需要运行的 shell

本文目录
    {"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}
    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    系统应用
    上一篇: Ansible Script 模块
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    Aperty macOS版
    Aperty macOS版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    AirServer Windows版
    AirServer Windows版

    AirServer Windows版可把电脑变成AirPlay、Google Cast和Miracast无线投屏接收端,适合会议演示、课堂教学及家庭大屏分享。发送设备通常无需另装客户端,并可同时接收和智能排列多个镜像会话。