本文主要介绍了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. os-hackos-3-docker提权

    0x00 cewl http://192.168.43.179/websec/爬取页面所有的单词做成字典 hydra -l contact@hacknos.com -P cewl.txt 192.16 ...

  2. css animation @keyframes 动画

    需求:语音播放动态效果 方案:使用如下图片,利用 css animation @keyframes  做动画 html <span class="horn" :class=& ...

  3. 大促密集,CDN如何保障电商体验如丝般顺滑?

    简介: 前不久,阿里云技术天团空降CSDN在线峰会,对核心技术竞争力进行解读.其中,阿里云高级技术专家曾福华分享了<双11: CDN如何保障电商大促如丝般顺滑>的议题.俗话说:养兵千日,用 ...

  4. MongoDB数据库的基本使用!

    MongoDB数据库的基本使用! 1 进入mongoose数据库 在控制台中输入 mongo; 2 查看所有的数据库 show dbs; 3 查看当前数据库的名称 db; 4 查看数据库中的所有的表 ...

  5. Docker的Ubuntu镜像安装的容器无ifconfig命令和ping命令

    就这三步骤,下面的是实例不看也罢. apt-get update ###第一步一定要先执行这个更新下.不更新下面的安装命令会显示找不到网络包 //ifconfig apt install net-to ...

  6. 对于两个输入文件,即文件A 和文件B ,请编写MapReduce程序,对两个文件进行合并排除其中重复的内容,得到一个新的输出文件C。

    package org.apache.hadoop.examples; import java.util.HashMap; import java.io.IOException; import jav ...

  7. Service Locator Pattern 服务定位

    https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...

  8. SpringBoot+Spring常用注解总结

    为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数). ...

  9. Linux命令之Crontab定时任务,利用Crontab定时执行spark任务

    Linux命令之Crontab定时任务,利用Crontab定时执行spark任务 一.Linux命令之Crontab定时任务 1.1 常见Crontab任务 1.1.1 安装crontab 1.1.2 ...

  10. python yield初探 (转)

    1. iterator叠代器最简单例子应该是数组下标了,且看下面的c++代码: int array[10]; for ( int i = 0; i < 10; i++ )    printf(& ...