• ansible的Ad-hoc命令


    本文主要介绍了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指定的目录后,再执行后面的命令,这在后面很多模块里都会有该参数 。

  • 相关阅读:
    Error #2044: 未处理的 NetStatusEvent:。 level=error, code=NetConnection.Call.BadVersion。
    Decode amf3 object using PHP
    Papervision3D Proximity Grid Example
    array容易混淆的几个有用的方法
    输出流实际上不写对象的值,而是对象吧自己本身写在流上
    OM Unit Selling Price 与 Price List Setup 不一致
    CoreApiHtml.sql 3< (INVItemCt115h.sql ) Note: 223702.1
    LOT NUMBER / PO / RECEIPT NO Relation.
    CoreApiHtml.sql 2< (INVItemCt115h.sql ) Note: 223702.1
    CoreApiHtml.sql 1< (INVItemCt115h.sql ) Note: 223702.1
  • 原文地址:https://www.cnblogs.com/txlsz/p/13475793.html
Copyright © 2020-2023  润新知