ansible ---- ansible.builtin.command

一、详解

command模块是ansible-core核心模块的一部分,大多数情况下可以使用简化名command;该命令会被执行在所有选择的机器上。
注意事项

  • 1、该命令并不会通过shell执行,所以对于环境变量$HOSTNAME以及一些" *", "<", ">", "|", ";" and "&" 操作将不支持,如果想要支持这些功能,可以使用ansible.builtin.shell 模块

1、参数详解

每一个模块都会有传入的参数,模块根据参数来执行具体的任务,相关参数如下:

参数 详解
cmd string 该模块待执行的命令
chdir path 在执行命令之前,先切换目录
creates path 如果机器节点上该文件存在,则该任务将不会执行(当前机器节点跳过该任务),该校验会在removes校验之前校验
removes path 如果一个文件存在,则该步骤才会执行
argv list/ elements=string 将命令以列表的形式传入

注意点

  • creates 、removes 、chdir 选项可以在cmd选项之后指定
  • 通过指定creates 、removes选项来启用校验模式
  • 执行顺序 chdir > creates > removes > cmd

2、argv 与 args 的区别

args是task任务的关键字,与模式是同一级别的;argv 是command模块的参数关键字,用于将命令行以列表的形式传入

二、实例详解

1、playbook yaml文件实例详解

yaml 复制代码
- name: Return motd to registered var
  ansible.builtin.command: cat /etc/motd
  register: mymotd

# free-form (string) arguments, all arguments on one line
# 所有的命令在同一行
- name: Run command if /path/to/database does not exist (without 'args')
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name creates=/path/to/database

# free-form (string) arguments, some arguments on separate lines with the 'args' keyword
# 'args' is a task keyword, passed at the same level as the module
# 使用task任务的关键子args,将模块的参数传入模块
- name: Run command if /path/to/database does not exist (with 'args' keyword)
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name
  args:
    # 当该文件存在的时候,该任务将会被跳过,(即文件不存在,才会执行与removes正好相反)
    creates: /path/to/database

# 'cmd' is module parameter
# cmd是command模块的参数
- name: Run command if /path/to/database does not exist (with 'cmd' parameter)
  ansible.builtin.command:
    cmd: /usr/bin/make_database.sh db_user db_name
    creates: /path/to/database
## 尽量采用这种模式的task模板,其他一般会出错
- name: Change the working directory to somedir/ and run the command as db_owner if /path/to/database does not exist
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name
  become: yes
  become_user: db_owner
  args:
    chdir: somedir/
    creates: /path/to/database

# argv (list) arguments, each argument on a separate line, 'args' keyword not necessary
# 'argv' is a parameter, indented one level from the module
- name: Use 'argv' to send a command as a list - leave 'command' empty
  ansible.builtin.command:
    argv:
      - /usr/bin/make_database.sh
      - Username with whitespace
      - dbname with whitespace
    creates: /path/to/database

- name: Run command using argv with mixed argument formats
  ansible.builtin.command:
    argv:
      - /path/to/binary
      - -v
      - --debug
      - --longopt
      - value for longopt
      - --other-longopt=value for other longopt
      - positional

- name: Safely use templated variable to run command. Always use the quote filter to avoid injection issues
  ansible.builtin.command: cat {{ myfile|quote }}
  register: myoutput

具体实例如下:

yaml 复制代码
---
- name: Update web servers
  hosts: webservers
  remote_user: kafka
  
  tasks:
  - name: ls -l
    ansible.builtin.command: ls -l 
      args:
    	chdir: somedir/
    	creates: /path/to/database

  - name: Write the apache config file
    ansible.builtin.template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

2、ansible命令实例详解

bash 复制代码
ansible  hostpattern  -i hosts  -m  command  -a 'cmd=ls -l creates=/path/file'
# command是ansible中的默认模块,即如下两个命令一致
ansible web -i hosts -m command -a "hostname"
ansible web -i hosts -a "hostname" 
# 常见的命令行实例如下
ansible webservers -m service -a "name=httpd state=started"
ansible webservers -m ping
ansible webservers -m command -a "/sbin/reboot -t now"

引用

command模块

相关推荐
服务器硬件项目经理14 分钟前
第5章消费级vs专业级vs数据中心:显卡的门派
服务器·嵌入式硬件·gru·硬件工程·硬件
邪修king16 分钟前
Re:Linux系统篇(三十六):进程间通信Chapter3:从匿名管道到命名管道:Linux 进程间通信实战(FIFO 深度解析)
linux·运维·服务器
Rabitebla23 分钟前
【Linux】 权限篇(三):深入理解 umask 位运算与粘滞位的设计思想
java·linux·服务器·c++·笔记·学习
Tech探索者28 分钟前
重装飞牛 NAS,几 KB 的 .bin 够吗?我的备份与恢复全过程
运维·服务器
讯捷蓝达38 分钟前
机房突发断电,5 盘 RAID硬盘全部掉线: 急速上门救援,现场4小时华为2288H V5服务器数据完整恢复
运维·服务器·数据库
ly76891 小时前
性能剖析与压测方法论:JMH、async-profiler 与分层瓶颈定位的可复现实验
服务器·前端·数据库·压测·jmh·性能剖析·async-profiler
风翼靓崽1 小时前
磁盘逻辑卷扩容
linux·运维·服务器
Brilliantwxx1 小时前
【STM32】RTC 完结篇:日期操作、闹钟中断与附录(时间戳 / 闹钟 / NTP)
服务器·c语言·stm32·单片机·嵌入式硬件·实时音视频
zincsweet1 小时前
Linux网络编程入门(二):从 UDP 回显服务器到多人聊天室,三个项目的渐进式实践
linux·服务器·网络
贵州诚鑫致达科技有限公司1 小时前
接入汇聚都是环怎么防环路?华为SEP加RRPP混合环
服务器·华为·php