ansible常用模块

ansible 核心模块之 shell

2020-12-14  本文已影响0人  wh0am11

shell模块

shell 模块用于用于在远程主机上执行命令

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

在执行对应的命令之前,会先进入到此参数指定的目录中
[Default: (null)]
version_added: 0.6

当指定的文件存在时,就不执行对应命令
[Default: (null)]

当指定的文件不存在时,就不执行对应命令
[Default: (null)]
version_added: 0.8

通过使用绝对路径修改并指定shell解释器来执行命令
[Default: (null)]
version_added: 0.9

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

注意:
* If you want to execute a command securely and predictably, it may be better to use the [command] module instead. Best practices when writing playbooks will follow the trend of using [command] unless the
`shell' module is explicitly required. When running ad-hoc commands, use your best judgement.
* To sanitize any variables passed to the shell module, you should use "{{ var | quote }}" instead of just "{{ var }}" to make sure they don't include evil things like semicolons.
* For Windows targets, use the [win_shell] module instead.
* Rather than using here documents to create multi-line scripts inside playbooks, use the [script] module instead.

区别:

实例:

- 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

- 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
上一篇 下一篇

猜你喜欢

热点阅读