ansible 核心模块之 raw

rwa模块

raw 模块用于在远程主机上执行 ansible 管理主机上的脚本

参数 (=号后面的参数强制要求):

  • = free_form

必须参数,指定需要远程执行的命令,但是并没有具体的一个参数名叫 free_form

  • executable
    通过使用绝对路径修改并指定 shell 解释器来执行命令
    当使用特权升级 become 时,如果没有提供 shell,因为特权升级需要一个 shell,所以默认外壳将被分配
    [Default: (null)]
    version_added: 1.0

注意:

  • 如果要安全且可预测地执行命令,最好改用 command 或 shell 模块
  • windows 目标也支持此模块

区别:

  • command、shell、raw 和 script 这四个模块的作用和用法都类似,都用于远程执行命令或脚本:
  • command 模块:执行简单的远程 shell 命令,但不支持解析特殊符号 < > | ; & 等,比如需要重定向时不能使用 command 模块,而应该使用shell模块。但command 模块更安全,因为他不受用户环境的影响, 也很大的避免了潜在的 shell 注入风险
  • shell 模块:和command相同,但是支持解析特殊 shell 符号,但这样有潜在的 shell 注入风险
  • raw 模块:执行底层 shell 命令。command 和 shell 模块都是通过目标主机上的 python 代码启动 /bin/bash 来执行命令的,但目标主机上可能没有安装 python,这时只能使用 raw 模块在远程主机上直接启动
  • script 模块:在远程主机上执行脚本文件 ,和 raw 模块一样,不要求目标主机上已经装好 python

实例:

- name: Bootstrap a legacy python 2.4 host
  raw: yum -y install python-simplejson

- name: Bootstrap a host without python2 installed
  raw: dnf install -y python2 python2-dnf libselinux-python

- 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)
  raw: cat < /tmp/*txt
  args:
    executable: /bin/bash

- name: safely use templated variables. Always use quote filter to avoid injection issues.
  raw: "{{package_mgr|quote}} {{pkg_flags|quote}} install {{python_simplejson|quote}}"

你可能感兴趣的:(ansible 核心模块之 raw)