java分布式系统部署学习(六)ansible Ad-hoc与commands模块

来源:互联网 发布:控制软件 编辑:程序博客网 时间:2024/06/10 08:10

Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令后面会说playbook。讲到Ad-hoc 就要提到模块,所有的命令执行都要依赖于事先写好的模块,默认安装好的ansible 里面已经自带了很多模块,如:command、raw、shell、file、cron等,具体可以通过ansible-doc -l 进行查看 。

一、Ad-hoc

1、直接执行

这里还是先来一个上几篇幅经常用到的一个例子:

# ansible localhost -a 'uptime'localhost | SUCCESS | rc=0 >>15:03  up 25 days,  3:30, 2 users, load averages: 1.68 2.35 2.23

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

ansible 主机或组  -m 模块名 -a '模块参数'  ansible参数

主机和组,是在/etc/ansible/hosts 里进行指定的部分,当然动态Inventory 使用的是脚本从外部应用里获取的主机,这部分具体可以参考Dynamic Inventory ;
模块名,可以通过ansible-doc -l 查看目前安装的模块,默认不指定时,使用的是command模块,具体可以查看/etc/ansible/ansible.cfg 的“#module_name = command ” 部分,默认模块可以在该配置文件中进行修改;
模块参数,可以通过 “ansible-doc 模块名” 查看具体的用法及后面的参数;
ansible参数,可以通过ansible命令的帮忙信息里查看到,这里有很多参数可以供选择,如是否需要输入密码、是否sudo等。

2、后台执行

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

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

示例如下:

# ansible 10.212.52.252 -B 3600 -P 0 -a 'watch ls'background launch...10.212.52.252 | success >> {    "ansible_job_id": "411650646689.13501",    "results_file": "/root/.ansible_async/411650646689.13501",    "started": 1}# ansible 10.212.52.252 -m async_status -a 'jid=411650646689.13501'10.212.52.252 | success >> {    "ansible_job_id": "411650646689.13501",    "changed": false,    "finished": 0,    "results_file": "/root/.ansible_async/411650646689.13501",    "started": 1}

不指定-P或-P参数为非0时,该任务就会按-P直接的参数一直刷新下去,直到超出-B参数指定的时间或命令执行完成:

# ansible 10.212.52.252 -B 3600  -a 'watch ls'background launch...10.212.52.252 | success >> {    "ansible_job_id": "397200656414.15008",    "results_file": "/root/.ansible_async/397200656414.15008",    "started": 1}10.212.52.252 | success >> {    "ansible_job_id": "397200656414.15008",    "changed": false,    "finished": 0,    "results_file": "/root/.ansible_async/397200656414.15008",    "started": 1}<job 397200656414.15008> polling on 10.212.52.252, 3585s remaining…………………………………………略

但是此处在我使用的ansible上是直接过期输出失败信息,并不会定时打印状态信息,此处存疑。

# ansible localhost -B 60 -P 10 -a 'top'localhost | FAILED | rc=-1 >>async task did not complete within the requested time

二、commands模块

上面已经提到,ansbile自身已经自带了很多模块,可以通过ansible-doc -l 进行查看。这里就结合command、shell、raw、script模块了解下其用法。
上面四个模块都属于commands 类。

  • command模块,该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则执行不成功 “ so variables like $HOME and operations like “<”, “>”, “|”, and “&” will not work(use the shell module if you need these features).”;
  • shell 模块,用法其本和command一样,不过的是其是通过/bin/sh进行执行,所以shell模块可以执行任何命令,就像在本机执行一样,“ It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.”;
  • raw模块,用法和shell 模块一样 ,其也可以执行任意命令,就像在本机执行一样,“Executes a low-down and dirty SSH command, not going through the module subsystem. There is no change handler support for this module. This module does not require python on the remote system”
  • script模块,其是将管理端的shell 在被管理主机上执行,其原理是先将shell复制到远程主机,再在远程主机上执行,原理类似于raw模块,“This module does not require python on the remote system, much like the raw module.” 。

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

command模块包含如下选项:

  • creates:一个文件名,当该文件存在,则该命令不执行
  • free_form:要执行的linux指令
  • chdir:在执行指令之前,先切换到该指定的目录
  • removes:一个文件名,当该文件不存在,则该选项不执行
  • executable:切换shell来执行指令,该执行路径必须是一个绝对路径

command模块、raw模块、shell模块示例:

# ansible 10.212.52.252 -m command -a 'ps auxf|grep snmp'10.212.52.252 | FAILED | rc=1 >>ERROR: Unsupported option (BSD syntax)********* simple selection *********  ********* selection by list *********-A all processes                      -C by command name-N negate selection                   -G by real group ID (supports names)-a all w/ tty except session leaders  -U by real user ID (supports names)-d all except session leaders         -g by session OR by effective group name-e all processes                      -p by process IDT  all processes on this terminal     -s processes in the sessions givena  all w/ tty, including other users  -t by ttyg  OBSOLETE -- DO NOT USE             -u by effective user ID (supports names)r  only running processes             U  processes for specified usersx  processes w/o controlling ttys     t  by tty*********** output format **********  *********** long options ***********-o,o user-defined  -f full            --Group --User --pid --cols --ppid-j,j job control   s  signal          --group --user --sid --rows --info-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect-l,l long          u  user-oriented   --sort --tty --forest --version-F   extra full    X  registers       --heading --no-heading --context                    ********* misc options *********-V,V  show version      L  list format codes  f  ASCII art forest-m,m,-L,-T,H  threads   S  children in sum    -y change -l format-M,Z  security data     c  true command name  -c scheduling class-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy# ansible 10.212.52.252 -m raw -a 'ps auxf|grep snmp'10.212.52.252 | success | rc=0 >>root      5580 25.0  0.0  12876  1792 pts/2    Ss+  12:36   0:00      \_ bash -c ps auxf|grep snmproot      5607  0.0  0.0   5720   832 pts/2    S+   12:36   0:00          \_ grep snmproot     24364  0.0  0.0  70416  6696 ?        SNl  May15   0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/snmpd.pid# ansible 10.212.52.252 -m shell -a 'ps auxf|grep snmp'10.212.52.252 | success | rc=0 >>root      5803  0.0  0.0  11308  1308 pts/2    S+   12:36   0:00              \_ /bin/sh -c ps auxf|grep snmproot      5805  0.0  0.0   4260   572 pts/2    S+   12:36   0:00                  \_ grep snmproot     24364  0.0  0.0  70416  6696 ?        SNl  May15   0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/snmpd.pid

上面的执行结果可以看到,我这里加了管道,command模块执行时出错,而使用raw模块和shell 模块都正常。
使用chdir的示例:

# ansible 10.212.52.252 -m command -a 'chdir=/tmp/361way touch test.file'10.212.52.252 | success | rc=0 >># ansible 10.212.52.252 -m shell -a 'chdir=/tmp/361way touch test2.file'10.212.52.252 | success | rc=0 >># ansible 10.212.52.252 -m raw -a 'chdir=/tmp/361way touch test3.file'10.212.52.252 | success | rc=0 >>

从上面执行结果来看,三个命令都执行成功了。不过通过在远程主机上查看,前两个文件被成功创建:

# ls /tmp/361waytest.file  test2.file

使用raw模块的执行的结果文件也被正常创建了,不过不是在chdir 指定的目录,而是在当前执行用户的家目录。

# ls ~/test3.file/root/test3.file

creates与removes示例:

这里我在测试主机上创建/tmp/361way/server.txt文件,执行结果如下:

# ansible 10.212.52.252 -a 'creates=/tmp/361way/server.txt uptime'10.212.52.252 | success | rc=0 >>skipped, since /tmp/361way/server.txt exists# ansible 10.212.52.252 -a 'removes=/tmp/361way/server.txt uptime'10.212.52.252 | success | rc=0 >> 15:11pm  up 28 days  0:34,  2 users,  load average: 0.75, 0.46, 0.39

script模块示例:

# cat script.sh#!/bin/bashdf -hlifconfigps auxf|grep snmp# ansible 10.212.52.252 -m script -a 'script.sh'10.212.52.252 | success >> {    "changed": true,    "rc": 0,    "stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb .....略"}

输出结果很多,看起来也很乱,不过查下stdout部分,这个部分是实际上执行后的结果。这里可以配合管道一起使用,可以如下使用:

# ansible 10.212.52.252 -m script -a 'script.sh' |egrep '>>|stdout'

转自:http://www.361way.com/ad-hoc-commands-modules/4408.html

原创粉丝点击