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

ansible 核心模块之 command

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

    command模块

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

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

    • chdir

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

    • creates

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

    • removes

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

    • = free_form

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

    注意:

    • 如果要安全可靠地执行命令,最好改用 command 模块, 除非明确需要 shell 模块。 运行临时命令时,请根据您的最佳判断
    • 在命令后可以指定 creates, removes 和 chdir
    • 自版本2.4起删除了 executable 参数。 如果需要此参数,请改用 shell 模块
    • 对于 Windows 目标,请改用 win_shell 模块

    区别:

    • 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: return motd to registered var
      command: cat /etc/motd
      register: mymotd
    
    - name: Run the command if the specified file does not exist.
      command: /usr/bin/make_database.sh arg1 arg2
      args:
        creates: /path/to/database
    
    # 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 /path/to/database doesn't exist.
      command: /usr/bin/make_database.sh arg1 arg2
      args:
        chdir: somedir/
        creates: /path/to/database
    
    - name: use argv to send the command as a list.  Be sure to leave command empty
      command:
      args:
        argv:
          - echo
          - testing
    
    - name: safely use templated variable to run command. Always use the quote filter to avoid injection issues.
      command: cat {{ myfile|quote }}
      register: myoutput
    

    相关文章

      网友评论

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

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