ansible ---- ansible.builtin.command

ops/2024/10/10 15:09:06/

一、详解

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文件实例详解

- name: Return motd to registered varansible.builtin.command: cat /etc/motdregister: 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_nameargs:# 当该文件存在的时候,该任务将会被跳过,(即文件不存在,才会执行与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_namecreates: /path/to/database
## 尽量采用这种模式的task模板,其他一般会出错
- name: Change the working directory to somedir/ and run the command as db_owner if /path/to/database does not existansible.builtin.command: /usr/bin/make_database.sh db_user db_namebecome: yesbecome_user: db_ownerargs: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' emptyansible.builtin.command:argv:- /usr/bin/make_database.sh- Username with whitespace- dbname with whitespacecreates: /path/to/database- name: Run command using argv with mixed argument formatsansible.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 issuesansible.builtin.command: cat {{ myfile|quote }}register: myoutput

具体实例如下:

---
- name: Update web servershosts: webserversremote_user: kafkatasks:- name: ls -lansible.builtin.command: ls -l args:chdir: somedir/creates: /path/to/database- name: Write the apache config fileansible.builtin.template:src: /srv/httpd.j2dest: /etc/httpd.conf

ansible_105">2、ansible命令实例详解

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模块


http://www.ppmy.cn/ops/92005.html

相关文章

CnosDB 元数据集群 – 分布式时序数据库的大脑

CnosDB 是一个分布式时序数据库系统&#xff0c;其中元数据集群是核心组件之一&#xff0c;负责管理整个集群的元数据信息。 1. 概述 CnosDB 是一个分布式时序数据库系统&#xff0c;其中元数据集群是核心组件之一&#xff0c;负责管理整个集群的元数据信息。元数据包括数据库…

【leetcode】根据二叉树创建字符串、二叉树的前中后遍历(非递归链表实现二叉树)

Hi~&#xff01;这里是奋斗的明志&#xff0c;很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~~ &#x1f331;&#x1f331;个人主页&#xff1a;奋斗的明志 &#x1f331;&#x1f331;所属专栏&#xff1a;数据结构、LeetCode专栏 &#x1f4da;本系…

构建可刷卡手持终端,思路与必备元素剖析-SAAS 本地化及未来之窗行业应用跨平台架构

构建可刷卡手持终端&#xff0c;思路与必备元素剖析 一、终端开发必要性 1.终端携带方便&#xff0c;适合空间小&#xff0c;外出 2.可供电&#xff0c;外带设备比较方便 3.大多数终端可以不需要网络独立使用&#xff0c;适合特殊场景 二、终端软件爱基本功能 1.便捷的终端…

Docker 环境下使用 Traefik 3 的最佳实践:快速上手

Traefik 最近终于发布了大版本升级后的第一个修正版本&#xff0c;或许是时候正式迁移程序到新版本了。 写在前面 最近 Traefik 发布了 3.1 版本。作为从 Traefik 1.x 开始使用的用户&#xff0c;Traefik 每个大版本升级都会出现一些配置不兼容的情况&#xff0c;这次 3.x 的正…

C++参悟:stl中的比较最大最小操作

stl中的比较最大最小操作 一、概述二、最小值1. min2. min_element 三、最大值1. max2. max_element 四、混合1. minmax2. minmax_element 一、概述 记录这里C11中常用的最小值和最大值的比较函数&#xff0c;最好的参考资料其实就是 https://zh.cppreference.com 最重要的查…

Linux LVM 详解

Linux逻辑卷管理&#xff08;LVM&#xff0c;Logical Volume Manager&#xff09;是一种管理磁盘存储空间的工具。它提供了比传统分区方法更灵活和高效的磁盘管理方式。通过LVM&#xff0c;可以动态调整磁盘分区大小&#xff0c;轻松添加或移除磁盘&#xff0c;并实现快照等高级…

算法学习day30

一、最短无序连续子数组&#xff08;贪心&#xff09; 给你一个整数数组 nums &#xff0c;你需要找出一个 连续子数组 &#xff0c;如果对这个子数组进行升序排序&#xff0c;那么整个数组都会变为升序排序。请你找出符合题意的 最短 子数组&#xff0c;并输出它的长度。 输…

鸿蒙应用服务开发【自定义通知角标】

自定义通知角标 介绍 本示例主要展示了设定应用的桌面图标角标的功能&#xff0c;使用ohos.notificationManager接口&#xff0c;进行桌面角标的设置&#xff0c;通知的发送&#xff0c;获取等。 效果预览 使用说明 在主界面&#xff0c;可以看到当前应用的所有消息通知&am…