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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值