本文主要介绍了ansible的Ad-hoc命令。

Ansible提供两种方式去完成任务,一是 ad-hoc 命令,一是写 Ansible playbook.前者可以解决一些简单的任务, 后者解决较复杂的任务.

ad-hoc 是相对于写 Ansible playbook 来说的.类似于在命令行敲入shell命令和 写shell scripts两者之间的关系。

Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook。那我们会在什么情境下去使用ad-hoc 命令呢?比如说因为圣诞节要来了,想要把所有实验室的电源关闭,我们只需要执行一行命令 就可以达成这个任务,而不需要写 playbook 来做这个任务.至于说做配置管理或部署这种事,还是要借助 playbook 来完成,即使用 ‘/usr/bin/ansible-playbook’ 这个命令.

Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块。 如:command、raw、shell、file、cron等,具体可以通过ansible-doc -l 进行查看 。

一、Ad-hoc命令

1.1、命令格式说明

一个ad-hoc命令的执行,需要按以下格式进行执行:

ansible 主机或组 -m 模块名 -a '模块参数' ansible参数
  • 主机和组,是在/etc/ansible/hosts 里进行指定的部分,当然动态Inventory 使用的是脚本从外部应用里获取的主机;
  • 模块名,可以通过ansible-doc -l 查看目前安装的模块,默认不指定时,使用的是command模块,具体可以查看/etc/ansible/ansible.cfg 的module_name = command 部分,默认模块可以在该配置文件中进行修改;
  • 模块参数,可以通过 “ansible-doc -s 模块名” 查看具体的用法及后面的参数;
  • ansible参数,可以通过ansible命令的帮助信息里查看到,这里有很多参数可以供选择,如是否需要输入密码、是否sudo等。
ansible all -m ping
ansible all -m command -a 'uptime'
ansible all -m yum -a 'name=nginx state=latest'
ansible all -m service -a "name=nginx state=started enabled=yes“
ansible all -m shell -a 'systemctl status nginx'
ansible all -m shell -a 'systemctl list-unit-files|grep nginx'

1.2、后台执行

当命令执行时间比较长时,也可以放到后台执行,使用-B、-P参数,如下:

ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff" #后台执行命令3600s,-B 表示后台执行的时间, 命令会返回一个jid
ansible all -m async_status -a "jid=123456789" #检查任务的状态
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" #后台执行命令最大时间是1800s即30分钟,-P 每60s检查下状态,默认15s

1.3、查看所有模块

[root@ansible ~]# ansible-doc -l |wc -l
773

二、使用ansible-doc 查看模块使用方法

可以使用ansible-doc -s module来查看某个模块的参数,也可以使用ansible-doc help module来查看该模块更详细的信息。

[root@ansible ansible]# ansible-doc command
> 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 or (since 2.0) glob pattern, 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 or (since 2.0) glob pattern, 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模块:该模块通过-a跟上要执行的命令可以直接执行
  • shell 模块:用法基本和command一样,不过其是通过/bin/sh进行执行,所以shell 模块可以执行任何命令,就像在本机执行一样;
  • raw模块:用法和shell 模块一样 ,其也可以执行任意命令,就像在本机执行一样;
  • script模块:其是将管理端的shell 在被管理主机上执行,其原理是先将shell 复制到远程主机,再在远程主机上执行,原理类似于raw模块。

注:raw模块和comand、shell 模块不同的是其没有chdir、creates、removes参数,chdir参数的作用就是先切到chdir指定的目录后,再执行后面的命令,这在后面很多模块里都会有该参数 。

ansible的Ad-hoc命令的更多相关文章

  1. Linux中级之ansible概念及hoc命令行调用模式

    一.Ansible简介 ansible是新出现的开源的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统 ...

  2. ansible常用ad hoc操作

    ansible group001 -i hosts.ip -m shell -a -v

  3. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  4. Ad hoc sql

    SQL Server如何启用Ad Hoc Distributed Queries? 2011-08-11 14:53 wangdingbang CSDN博客 字号:T | T   本文主要介绍了SQL ...

  5. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问

    消息 15281,级别 16,状态 1,第 2 行SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/Open ...

  6. XE7 & IOS开发之开发账号(3):证书、AppID、设备、授权profile的申请使用,附Debug真机调试、Ad hoc下iPA文件生成演示(XCode5或以上版本推荐,有图有真相)

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 注意,以下讨论都是以&q ...

  7. XE7 & IOS开发之开发账号(2):发布证书、发布授权profile的申请使用,附Ad hoc真机调试、生成ipa文件演示(XCode所有版本通用,有图有真相)

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 注意,以下讨论都是以&q ...

  8. 启用与关闭 Ad Hoc Distributed Queries

    在数据库里执行以下脚本: 启用: exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Dis ...

  9. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...

  10. 解除SQL对组件"Ad Hoc Distributed Queries"的"STATEMENT'OpenRowset OpenDatasource"的访问

      SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为 ...

随机推荐

  1. SAP下载文档为乱码

    通过事物WE60下载的文档为乱码,主要原因是编码格式的不匹配,通常默认的编码格式为ANSI编码,那么我们需要将源码的编码格式转换成UTF-8,这样问题可以解决了.   附:编码格式介绍 不同的国家和地 ...

  2. Navicat linux 官方最新版安装破解

    我们直接去官网下载linux版navicat 下载好之后执行命令打开软件 chmod +x navicat15-premium-cs.AppImage ./navicat15-premium-cs.A ...

  3. JS实现鼠标移入DIV随机变换颜色

    今天分享一个在 JavaScript中,实现一个鼠标移入可以随机变换颜色,本质就是js的随机数运用. 代码如下: <!DOCTYPE html> <html> <head ...

  4. Django-发上云服务器遇到的问题

    1.服务器启动后外网访问显示A server error occurred. Please contact the administrator. 解决方法:原文:https://www.cnblogs ...

  5. 转 11 jmeter之图形监控扩展

    11 jmeter之图形监控扩展   Jmeter默认监听器的缺陷 Jmeter默认的监听器在表格.文字方面比较健全,但是在图形监控方面比较逊色,尤其在监控Windows或Linux的系统资源方面.但 ...

  6. Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732

    Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79 ...

  7. 京东零售mockRpc实践

    https://mp.weixin.qq.com/s/A0T6ySub0DfQiXJAbWm2Qg jsf协议是基于tcp的而且对数据进行了序列化.加密等操作,直接截获的方式很难实现.最后决定注入自己 ...

  8. 使用JWT创建安全的ASP.NET Core Web API

    在本文中,你将学习如何在ASP.NET Core Web API中使用JWT身份验证.我将在编写代码时逐步简化.我们将构建两个终结点,一个用于客户登录,另一个用于获取客户订单.这些api将连接到在本地 ...

  9. 关于MongoDB的简单理解(二)--Java篇

    一.声明 本文依赖于 MongoDB JVM DRIVERS 4.1 版本编写. 本文案例依赖于 Maven 项目管理工具. 二.本文主要讲解哪些内容? 如何连接到MongoDB 通过TLS/SSL连 ...

  10. 透明小电视上线——GitHub 热点速览 v.21.05

    作者:HelloGitHub-小鱼干 这周的 GitHub Trending 真是棒极了.小鱼干喜欢的科技博主又开源了他的硬件玩具,一个透明的小电视机,HG 的小伙伴看完项目,再买个电路板和分光棱镜, ...