ansible shell 模块

ansible shell 模块

概要

  • shell模块采用命令名称,后跟以空格分隔的参数列表。
  • 需要自由格式的命令或cmd参数,请参见示例。
  • 它几乎与命令/bin/sh模块完全相同,但通过远程节点上的shell ( ) 运行命令。
  • 对于 Windows 目标,请改用win_shell模块。

参数

parameterchoices/defaults(选项)comments(注解)
chdir在运行该命令之前,切换到该目录。
cmd运行的命令后跟可选参数
creates当文件名已经存在时,此步骤将不运行
executable更改用于执行该命令的shell。这需要可执行文件的绝对路径。
free_formshell模块接受一个以字符串形式运行的自由形式命令。没有名为“free form”的实际参数。
removes当文件名不存在时,此步骤将不运行。
stdin将命令的stdin直接设置为指定的值。
stdin_add_newlineyes or no,默认yes是否向stdin数据追加换行符。
warnyes or no,默认yes是否启用任务警告。

笔记

  • 如果您想安全且可预测地执行命令,最好改用command模块。编写剧本时的最佳实践将遵循使用command的趋势,除非shell明确需要模块。运行临时命令时,请使用您的最佳判断。
  • creates通过或时支持检查模式removes。如果在检查模式下运行并且指定了其中任何一个,模块将检查文件是否存在并报告正确的更改状态。如果未提供这些,则将跳过该任务。
  • 要清理传递给 shell 模块的任何变量,您应该使用而不是仅仅确保它们不包含诸如分号之类的邪恶内容。{{ var | quote }}``{{ var }}
  • 在此模块中使用内联 shell 脚本的另一种方法是将script模块可能与template模块一起使用。
  • 对于重新启动系统,请使用rebootwin_reboot模块。

例子

- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
  shell: somescript.sh >> somelog.txt

- name: Change the working directory to somedir/ before executing the command.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/

# You can also use the 'args' form to provide the options.
- name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
    creates: somelog.txt

# You can also use the 'cmd' parameter instead of free form format.
- name: This command will change the working directory to somedir/.
  shell:
    cmd: ls -l | grep log
    chdir: somedir/

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
  shell: cat < /tmp/*txt
  args:
    executable: /bin/bash

- name: Run a command using a templated variable (always use quote filter to avoid injection)
  shell: cat {{ myfile|quote }}

# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
  shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }}

    expect "password:"
    send "{{ cimc_password }}\n"

    expect "\n{{ cimc_name }}"
    send "connect host\n"

    expect "pxeboot.n12"
    send "\n"

    exit 0
  args:
    executable: /usr/bin/expect
  delegate_to: localhost

# Disabling warnings
- name: Using curl to connect to a host via SOCKS proxy (unsupported in uri). Ordinarily this would throw a warning.
  shell: curl --socks5 localhost:9000 http://www.ansible.com
  args:
    warn: noyamml
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Ansibleshell模块允许在远程主机上执行Shell命令。与其他模块不同,它不需要在远程主机上安装Python。使用该模块,您可以在远程主机上执行各种Shell命令,例如创建文件、修改文件权限、启动服务等等。 然而,需要注意的是,使用shell模块执行Shell命令时要谨慎,因为它可能会导致安全问题和不可预测的行为。因此,建议在必要时使用,并使用适当的参数来确保执行命令的安全性和正确性。 ### 回答2: Ansible是一个开源的自动化运维工具,可以对不同的操作系统和网络设备进行管理和配置。而其中的Ansible Shell模块是其中的一项重要功能,它通常用于在目标主机上执行指定的Shell命令或脚本。在本文中,我们将详细介绍Ansible Shell模块的使用方法以及在Ansible自动化运维工作中的实际应用。 Ansible Shell模块的基本使用 1. 参数设置 - name:选填,任务名称; - args:选填,命令或脚本的参数; - creates:选填,检查标志,如果文件/目录已经存在就跳过执行命令; - executable:选填,命令的执行路径; - chdir:选填,切换工作目录; - stdin:选填,可选的标准输入。 - warn:选填,一个布尔值来设置一个命令如果失败是否会使任务失败,这是对某些命令(例如touch)特别有用,因为无法以错误的方式执行。 2. Shell模块的使用方法 - 直接使用命令字符串: ``` - name: run "ls" command shell: ls ``` - 通过变量传递命令: ``` - name: run the command dynamically passing vars shell: "{{ command }}" vars: command: "ls" ``` - 执行多个命令: ``` - name: execute multiple commands shell: | command1 command2 ``` 通过这样一个Shell模块的使用,可以完成对指定的命令或脚本的执行,可以根据实际需要进行灵活配置。 Ansible Shell模块在自动化运维中的应用 Ansible Shell模块在自动化运维中具有广泛的应用场景,主要包括以下几个方面。 1. 配置环境 通过Shell模块,可以在目标主机上执行各种命令或脚本来配置环境,例如安装各种依赖包、启动/停止服务、部署应用程序等等。 2. 系统管理 在系统管理方面,Shell模块可用于执行诸如用户管理、文件操作、进程管理等任务。 3. 监控和报警 结合脚本使用监控进程,当系统资源、应用程序发生异常或错误时,Shell模块可以配合报警系统进行实时通知,反馈系统信息的变化。 4. 数据库管理 对于数据库管理,Shell模块可用于执行诸如数据备份、恢复、查询、导入导出等操作,实现对数据库的管理。 综上所述,Ansible Shell模块是自动化运维的一项重要功能,它可以通过执行Shell命令或脚本来进行环境配置、系统管理、监控与报警、数据库管理等工作。通过对Shell模块的灵活应用,可以提高自动化运维效率,减轻管理员工作负担,提高工作效率和准确性。 ### 回答3: Ansible是一种自动化工具,可以轻松管理多台服务器Ansible shell模块是其重要的组成部分之一。该模块将命令直接发送到远程服务器shell中,并收集输出结果。 Ansible shell模块使用简单,灵活性高,可以执行任何可以在远程服务器上执行的命令。它通常用于第一次设置服务器、安装软件包、更新系统等任务。 当使用Ansible shell模块时,用户可以手动输入命令,也可以使用Ansible变量。例如,可以使用变量来将命令与本地文件结合起来,或从行上读取变量。这使得任务更容易自动化和重复使用。 Ansible shell模块还支持使用sudo和su命令进行用户切换,这使得用户可以在不退出当前用户的情况下在远程服务器上运行命令。 需要注意的是,Ansible shell模块是直接在远程服务器上执行命令的,因此务必谨慎执行。如果命令有可能影响服务器的正常运行,请先在测试环境中进行测试,并确保命令是正确的。 综上所述,Ansible shell模块Ansible自动化工具中不可或缺的一部分。通过使用它,用户可以轻松管理多台服务器,完成各种任务,从而提高生产效率。但是,用户必须小心谨慎,以确保命令不会影响服务器的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值