ansible之command和raw模块

command模块

command模块为ansible默认模块,不指定-m参数时,使用的就是command模块; 
comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,运行中的命令不支持变量使用以及不支持管道; 
示例:显示当前用户

[[email protected] ~]# ansible all -a ‘whoami‘
db | SUCCESS | rc=0 >>
root
web | SUCCESS | rc=0 >>
root
192.168.33.132 | SUCCESS | rc=0 >>
root

command用法

[[email protected] ~]# ansible-doc -s command
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: Executes a command on a remote node
  action: command
      chdir                  # cd into this directory before running the command
      creates                # a filename or (since 2.0) glob pattern, when it already exists, this step will *not* be run.
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.
      free_form=             # the command module takes a free form command to run.  There is no parameter actually named ‘free
                               form‘. See the examples!
      removes                # a filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be run.
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to
                               no/false.

- name: 在远程节点执行命令
action: command
chdir           # 在执行命令之前,先切换到该目录
creates         # 一个文件名,当这个文件存在,则该命令不执行
executable      # 切换shell来执行命令,需要使用命令的绝对路径
free_form=      #要执行的Linux指令,一般使用Ansible的-a参数代替。
removes         #一个文件名,这个文件不存在,则该命令不执行

raw,类似command,但可以传递管道

用法

[[email protected] ~]# ansible-doc -s raw
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: Executes a low-down and dirty SSH command
  action: raw
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.
      free_form=             # the raw module takes a free form command to run

示例,查看web服务器上swap状态

[[email protected] ~]# ansible web -m raw -a ‘free -m|grep Swap‘
web | SUCCESS | rc=0 >>
Swap:         2047          0       2047
时间: 2024-08-18 21:25:52

ansible之command和raw模块的相关文章

Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]

本文主要介绍Ansible的几个命令模块,包括: command - 在远程节点上执行命令 shell - 让远程主机在shell进程下执行命令 script - 将本地script传送到远程主机之后执行 raw - 执行低级的和脏的SSH命令 expect - 执行命令并响应提示 telnet - 执行低级的和脏的telnet命令 command模块 简介 command模块用于在给的的节点上运行系统命令,比如echo hello. 它不会通过shell处理命令,因此不支持像$HOME这样的变

关于ansbile工具的shell、command、script、raw模块的区别和使用场景

之前一直误解为shell和command一样是执行远程命令的,最近看了一下官方帮助文档才发现,犯了低级错误,其实每个模块都有自己的应用常用:简单说明一下ansible几个常用模块的区别,以便自己总结. 1.模块命令详细信息查询 ansible-doc 模块名称 2.command模块 [执行远程命令] ansible client -m command -a "uname -n" -s 3.script模块 [在远程主机执行主控端的shell/python脚本] ansible cli

ansible的安装与介绍、host-pattern格式、ansible的command模块、ansible的shell模块、ansible的script模块、ansible的copy模块

一.ansible的安装与介绍 ansible需要安装在管控机上 首先安装wget(如果机器上有可以忽略) 下载安装wget yum install -y wget 将epel源下载安装到本地 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 安装ansible yum install -y ansible ansible的命令格式 Usage: ansible <host-pattern>

ansible基础—安装与常用模块

ansible介绍: ansible是一个基于python开发的轻量级自动化运维管理工具,可以用来批量执行命令,安装程序,支持playbook编排.它通过ssh协议来连接主机,省去了在每一台主机安装客户端的麻烦,相对比puppet和saltstack,显得更为简单和轻量. ansible命令参数: Usage: ansible <host-pattern> [options] Options:   -a MODULE_ARGS, --args=MODULE_ARGS              

L13 ansible 基础应用与常见模块

ansible 基础应用与常见模块 ansible中文手册:http://www.simlinux.com/books/Ansible-notes.pdf 1,运维工具简介 运维工具: 系统安装(物理机.虚拟机)--> 程序安装.配置.服务启动 --> 批量操作(批量运行命令) --> 程序发布 --> 监控 ansible 能够实现:程序安装.配置.服务启动 --> 批量操作(批量运行命令) --> 程序发布 ansible的核心组件: ansible core ho

Ansible 之 概念和常用模块介绍

1  概述 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能. ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.主要包括如下的特性: (1).连接插件connection plugins:负责和被监控端实现通信: (2).host inventory:

ansible的几个常用模块

Ansible是什么呢?ansible是最新出现的自动化运维工具,基于python开发,集合了众多的运维工具(puppet,cfengine,chef等)的优点,实现了批量管理系统配置,批量程序部署,反正啥都是批量实现就对了. ansible有如下优点: ? 1.部署简单,需要在主控端安装ansible环境,被控端不需要任何操作. ? 2.默认使用ssh协议对设备进行管理 ? 3.可扩展性强且支持API及自定义模块,可通过Python扩展 ? 4.通过Playbooks来指定强大的配置,状态管理

ansible自动化部署之第三方模块添加(时间计时模块)

一.时间计时模块 ansible中可以加入一个计时模块在执行ansible-playbook时显示执行时长.方便使用. 1.配置方法 cd /etc/ansible mkdir callback_plugins cd callback_plugins wget https://raw.githubusercontent.com/jlafon/ansible-  \                     profile/master/callback_plugins/profile_tasks.

ansible学习系列2-ansible常用模块使用

1. 查看支持的模块 [[email protected] ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [[email protected] ~]# ansible-doc -l |wc -l #查看支持的模块个数 1039 [[email protected] ~]# ansible --version #查看我们的ansible版本号 ansible 2.3.1.0 config file = /etc/ansible/ansible.cfg confi