美文网首页ansible常用模块
ansible 核心模块之 shell

ansible 核心模块之 shell

作者: wh0am11 | 来源:发表于2020-12-14 11:22 被阅读0次

    shell模块

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

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

    • chdir

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

    • creates

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

    • removes

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

    • executable

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

    • = free_form

    必须参数,指定需要远程执行的命令,但是并没有具体的一个参数名叫 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.

    • 如果要安全可靠地执行命令,最好改用 command 模块, 除非明确需要 shell 模块。 运行临时命令时,请根据您的最佳判断
    • 如果你需要安全的使用带有变量的 shell 模块, 使用 {{ var | quote }} 代替 {{ var }} , 确保输入不包含分号或者流式操作
    • 对于 Windows 目标,请改用 win_shell 模块
    • 不要使用本地文档在 playbook 中创建多行脚本,而是使用 script 模块

    区别:

    • 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: 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
    

    相关文章

      网友评论

        本文标题:ansible 核心模块之 shell

        本文链接:https://www.haomeiwen.com/subject/ytzmgktx.html