ansible模块command、shell、raw、script
时间:2022-07-23
本文章向大家介绍ansible模块command、shell、raw、script,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
简介
环境:
ansible端:
ip:192.168.100.129
hostname:node1.lansgg.com
client端:
ip:192.168.100.131
hostname:v2.lansgg.com
ip:192.168.100.132
hostname:v3.lansgg.com
[root@node1 ansible]# pwd
/etc/ansible
[root@node1 ansible]# cat hosts
[testservers]
192.168.100.131
192.168.100.132
[root@node1 ansible]#
1、命令格式
SYNOPSIS
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
模块命令详细信息查询
ansible-doc moduleName
如:
[root@node1 ansible]# ansible-doc 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
> COMMAND
The [command] module takes the command name followed by a list of
space-delimited arguments. The given command will be executed on all
selected nodes. It will not be processed through the shell, so
variables like `$HOME' and operations like `"<"', `">"', `"|"', and
`"&"' will not work (use the [shell] module if you need these
features).
Options (= is mandatory):
- chdir
cd into this directory before running the command [Default:
None]
- creates
a filename, when it already exists, this step will *not* be
run. [Default: None]
- executable
change the shell used to execute the command. Should be an
absolute path to the executable. [Default: None]
= free_form
the command module takes a free form command to run. There is
no parameter actually named 'free form'. See the examples!
[Default: None]
- removes
a filename, when it does not exist, this step will *not* be
run. [Default: None]
- warn
if command warnings are on in ansible.cfg, do not warn about
this particular line if set to no/false. [Default: True]
Notes: If you want to run a command through the shell (say you are using
`<', `>', `|', etc), you actually want the [shell] module
instead. The [command] module is much more secure as it's not
affected by the user's environment. `creates', `removes', and
`chdir' can be specified after the command. For instance, if
you only want to run a command if a certain file does not
exist, use this.
EXAMPLES:
# Example from Ansible Playbooks.
- command: /sbin/shutdown -t now
# Run the command if the specified file does not exist.
- command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database
# You can also use the 'args' form to provide the options. 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
解释:command为模块名, chdir、create 等此模块特有的参数
2、ansible模块之command
此模块为ansible默认执行的模块,也是常用模块之一
示例: 查看远程主机的passwd最后两行
[root@node1 ansible]# ansible testservers -m command -a 'tail -2 /etc/passwd'
192.168.100.131 | success | rc=0 >>
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
192.168.100.132 | success | rc=0 >>
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
使用参数,修改当然工作目录
[root@node1 ansible]# ansible testservers -m command -a 'pwd'
192.168.100.132 | success | rc=0 >>
/root
192.168.100.131 | success | rc=0 >>
/root
[root@node1 ansible]# ansible testservers -m command -a 'chdir=/tmp/ pwd'
192.168.100.131 | success | rc=0 >>
/tmp
192.168.100.132 | success | rc=0 >>
/tmp
区别和使用场景
command模块 [执行远程命令]
[root@node1 ansible]# ansible testservers -m command -a 'uname -n'
script模块 [在远程主机执行主控端的shell/python脚本 ] (使用相对路径)
[root@node1 ansible]# ansible testservers -m script -a '/etc/ansible/test.sh
shell模块 [执行远程主机的shell/python脚本]
[root@node1 ansible]# ansible testservers -m shell -a 'bash /root/test.sh'
raw模块 [类似于command模块、支持管道传递]
[root@node1 ansible]# ansible testservers -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print $2}' |awk -F: '{print $2}'"
- Redis 应用案例 - 在问题中不断成长
- OpenCV中神经网络介绍与使用
- OpenCV中导向滤波介绍与应用
- 学习SQL【9】-集合与联结
- 爬取《The Hitchhiker’s Guide to Python!》python进阶书并制成pdf
- OpenCV中积分图介绍与应用
- OpenCV 3.4发布啦!
- 使用 NodeJS 实现反向代理
- 小程序示例 - 不同页面间的消息传递
- Kafka 消息的生产消费方式
- 塔秘 | 从Zero到Hero,一文掌握Python关键代码
- 微信小程序请求server时遇到的2个问题
- Android NDK层编译OpenCV代码开发详解
- 实时通信框架 Socket.IO
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- android与asp.net服务端共享session的方法详解
- Android实现多个连续带数字圆圈效果
- Android录制mp3格式文件
- Linux内核设备驱动之虚拟文件系统笔记整理
- linux服务器上使用nginx访问本地静态资源的方法
- Android控件之菜单的创建方式
- 可支持快速搜索筛选的Android自定义选择控件
- Android编程实现的短信编辑器功能示例
- SELinux 入门详解
- Android EditText限制输入字符的方法总结
- Android使用API实现图像扭曲效果示例
- linux网络配置工具的使用
- Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解
- Android中使用AsyncTask实现下载文件动态更新进度条功能
- 在CentOS VPS上通过SSH安装 MySQL的方法图解