ansible command、shell、script、raw的区别

  1. command:执行远程命令
    ansible -s -i hosts localhost -m command -a "df -h ."
  2. shell:执行远程脚本(shell/pyhton)
    ansible -s -i hosts localhost -m shell -a "/etc/test.sh"
  3. script:在远程服务器上执行本地脚本
    ansible -s -i hosts localhost -m script -a "/etc/test.sh"
  4. raw: 和command 功能相似,支持管道符
    ansible -s -i hosts localhost -m raw -a "df -h . | tail -n1"

raw 文档:

> RAW    (/usr/lib/python2.7/site-packages/ansible/modules/commands/raw.py)

        Executes a low-down and dirty SSH command, not going through the module subsystem. This is useful and should only be done in
        two cases. The first case is installing `python-simplejson' on older (Python 2.4 and before) hosts that need it as a dependency
        to run modules, since nearly all core modules require it. Another is speaking to any devices such as routers that do not have
        any Python installed. In any other case, using the [shell] or [command] module is much more appropriate. Arguments given to
        `raw' are run directly through the configured remote shell. Standard output, error output and return code are returned when
        available. There is no change handler support for this module. This module does not require python on the remote system, much
        like the [script] module. This module is also supported for Windows targets.

  * note: This module has a corresponding action plugin.

OPTIONS (= is mandatory):

- executable
        change the shell used to execute the command. Should be an absolute path to the executable.
        when using privilege escalation (`become'), a default shell will be assigned if one is not provided as privilege escalation
        requires a shell.
        [Default: (null)]
        version_added: 1.0

= free_form
        the raw module takes a free form command to run. There is no parameter actually named 'free form'; see the examples!



NOTES:
      * If using raw from a playbook, you may need to disable fact gathering using `gather_facts: no' if you're using `raw' to
        bootstrap python onto the machine.
      * If you want to execute a command securely and predictably, it may be better to use the [command] or [shell] modules
        instead.
      * the `environment' keyword does not work with raw normally, it requires a shell which means it only works if `executable'
        is set or using the module with privilege escalation (`become').
      * This module is also supported for Windows targets.

AUTHOR: Ansible Core Team, Michael DeHaan
        METADATA:
          status:
          - stableinterface
          supported_by: core


EXAMPLES:
- 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 command、shell、script、raw的区别)