• ansible command 模块


    文章目录

    command 模块

    作用:在远程节点上执行一个命令

    我们可以使用 ansible-doc -s command 查看该模块支持的参数,command 是默认模块,可忽略 -m 选项。我们也可以修改默认模块,在 /etc/ansible/ansible.cfg 中修改默认参数

    # default module name for /usr/bin/ansible
    #module_name = command
    
    • 1
    • 2
    [root@master ~]# ansible-doc -s command
    - name: Execute commands on targets
      command:
          argv:                  # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would
                                   otherwise be interpreted incorrectly (for example "user name"). Only
                                   the string or the list form can be provided, not both.  One or the
                                   other must be provided.
          chdir:                 # Change into this directory before running the command.
          cmd:                   # The command to run.
          creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
          free_form:             # The command module takes a free form command to run. There is no actual parameter named 'free form'.
          removes:               # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
          stdin:                 # Set the stdin of the command directly to the specified value.
          stdin_add_newline:     # If set to `yes', append a newline to stdin data.
          strip_empty_ends:      # Strip empty lines from the end of stdout/stderr in result.
          warn:                  # Enable or disable task warnings.
          
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    参数解析:

    参数说明
    chdir在执行命令之前,通过 cd 进入到该参数指定的目录
    creates在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作
    free_form该参数即为输入的 linux 系统命令,实现远程执行和管理
    removes判断一个文件是否存在,如果存在则执行前面的动作,如果不存在则跳过前面的内容(和上面的 creates 是相反的)
    warn是否提供告警信息

    注意:

    • 使用 command 模块时,不得出现 shell 变量 $name ,也不得使用特殊符号 > < | ; & 等,如果需要使用前面的特殊符号则可以使用 shell 模块来实现

    案例:

    • 获取所有被管理机器的负载信息
    [root@master ~]# ansible dong -m command -a "uptime"
    192.168.169.162 | CHANGED | rc=0 >>
     12:00:51 up  1:17,  2 users,  load average: 0.03, 0.04, 0.05
    192.168.169.161 | CHANGED | rc=0 >>
     12:00:51 up  1:18,  2 users,  load average: 0.20, 0.06, 0.05
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    • 让客户端机器,先切换到 /tmp 目录下,然后打印出当前的目录信息
    [root@master ~]# ansible dong -m command -a "pwd"		# 先看下默认工作目录是什么
    192.168.169.161 | CHANGED | rc=0 >>
    /root
    192.168.169.162 | CHANGED | rc=0 >>
    /root
    [root@master ~]# ansible dong -m command -a "pwd chdir=/tmp"
    192.168.169.162 | CHANGED | rc=0 >>
    /tmp
    192.168.169.161 | CHANGED | rc=0 >>
    /tmp
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    • creates 参数练习

    该参数的作用是在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作

    # 判断 /dong 文件是否存在,存在则不执行前面的 pwd 命令,不存在则执行 pwd 命令
    [root@master ~]# ansible 192.168.169.161 -m command -a "pwd creates=/dong"
    192.168.169.161 | CHANGED | rc=0 >>
    /root
    [root@master ~]# ansible 192.168.169.161 -m command -a "pwd creates=/opt"
    192.168.169.161 | SUCCESS | rc=0 >>		# 返回命令执行成功
    skipped, since /opt exists				# 提示命令跳过,/opt 目录已经存在
    
    # 使用 cp 命令拷贝文件
    [root@master ~]# ansible dong -a "cp /etc/passwd /root creates=/root/passwd"
    192.168.169.162 | CHANGED | rc=0 >>
    
    192.168.169.161 | CHANGED | rc=0 >>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • removes 参数练习

    判断一个文件是否存在,如果存在则执行前面的动作,如果不存在则跳过前面的内容(和上面的 creates 是相反的)

    [root@master ~]# ansible 192.168.169.161 -m command -a "pwd removes=/dong"
    192.168.169.161 | SUCCESS | rc=0 >>
    skipped, since /dong does not exist
    [root@master ~]# ansible 192.168.169.161 -m command -a "pwd removes=/opt"
    192.168.169.161 | CHANGED | rc=0 >>
    /root
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • warn 参数练习

    是否提供告警信息

    # 没有使用 warn 参数,在使用一些命令时(例如:chmod),系统会提示一些告警信息
    [root@master ~]# ansible dong -a "chmod u+x /root/passwd"
    [WARNING]: Consider using the file module with mode rather than running 'chmod'.  If you need to use command because file is
    insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
    message.
    192.168.169.161 | CHANGED | rc=0 >>
    
    192.168.169.162 | CHANGED | rc=0 >>
    
    # 使用 warn=false 参数后则没有告警信息显示了
    [root@master ~]# ansible dong -a "chmod u+x /root/passwd warn=false"
    192.168.169.162 | CHANGED | rc=0 >>
    
    192.168.169.161 | CHANGED | rc=0 >>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

     
     
     
     
     

  • 相关阅读:
    UG NX二次开发(C++)-采用std::vector对体对象的质心进行排序
    人工智能的发展前景如何
    鸿蒙LiteOs读源码教程+向LiteOS中添加一个简单的基于线程运行时的短作业优先调度策略
    docker 安装 nginx 镜像 保姆级别教程
    R语言计算data.table数据中指定数值变量大于一个固定值、另外两个分组变量的交叉分组对应的统计值(中位数)最小的分组、中位数最小的那个分组
    JavaScript 是什么
    JAVA基础总结【面试】
    面试经典150题——Day36
    Vue 前置 后置 路由守卫 独享 路由权限控制 自定义属性
    【C# 调试】.net中的 .pdb文件是什么,有什么用
  • 原文地址:https://blog.csdn.net/D1179869625/article/details/126173879