用ansible 完成一次性的工作(ad-Hoc)工作

ansible 真正强大的功能是它的playbook,但是在日常的工作中通过会遇到一些工作,它们只是需要我们偶尔操作一下;比较说重启一下

操作系统;像这样的工作就用不着ansible-playbook这样的牛刀了,用ansible就行了。

一、ad-Hoc 在多台主机上并行执行命令:

  在刚开始进入linux 世界的时候呀,为了在多台linux主机上启动mysql数据库,我会一个个的登录上对应的linux主机,然后执行service mysqld start

  混了一段时间后深知,勤劳也架不主机器多呀!后来呀就用bash 写一个for 循环,这样自己就从手工的劳动的释放出来了;但是这样事实上还是不是

  太行的,主要表现在for 循环是串行执行的,也就是只有在启动完第一台主机的mysql后才会去启动第二台机器的mysql;也许你想到了"&"号可以使命令

  能在后台执行,但是这个也是有问题的,因为这样就会一有太多的进程在后台执行,不知道自己主机的配置能不能跟的上;虽然我的理由有点牵强,但是

  我想表达的是for 循环的形式的控制粒度太“粗糙”了。 看ansible 怎么做

  1、启动work这台主机上的mysql服务

ansible work -a'systemctl start mysql ' -uroot --become -f 1
work | SUCCESS | rc=0 >>

ps -ef | grep mysql
mysql      5146      1  8 14:48 ?        00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root       5184   4538  0 14:48 pts/1    00:00:00 grep --color=auto mysql

  从ps 中的内容可以看到mysql数据库已经启动了

  -u 用于指定sudo 的目标用户,我这里设定的是root用户

  --become 是一个开着,它用于控制-u这个参数是不生效

  -f 用于设置并行度,我这里设置为了1,因为work就是我的本机,也就是说目标主机只有一台,开并行并没有什么用。

  2、文件传输把本地的/etc/my.cnf 传到目标机器的/tmp/my.cnf

ansible work -m copy -a'dest=/tmp/my.cnf src=/etc/my.cnf'
work | SUCCESS => {
    "changed": true,
    "checksum": "591767b936bdf730031e7964d548547327e30ad0",
    "dest": "/tmp/my.cnf",
    "gid": 1000,
    "group": "jianglexing",
    "md5sum": "278fe9fd1d837086edb3adbb2cd627df",
    "mode": "0664",
    "owner": "jianglexing",
    "size": 4376,
    "src": "/home/jianglexing/.ansible/tmp/ansible-tmp-1501052194.1641386-183941868489754/source",
    "state": "file",
    "uid": 1000
}

  3、软件包管理(安装httpd)

ansible work -m yum -a'name=httpd state=present' -uroot --become
work | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7.centos for package: httpd-2.4.6-45.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-45.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch          Version                    Repository    Size
================================================================================
Installing:
 httpd              x86_64        2.4.6-45.el7.centos        local        2.7 M
Installing for dependencies:
 apr                x86_64        1.4.8-3.el7                local        103 k
 apr-util           x86_64        1.5.2-6.el7                local         92 k
 httpd-tools        x86_64        2.4.6-45.el7.centos        local         84 k

Transaction Summary
================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               18 MB/s | 3.0 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7.x86_64                                       1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/4 
  Installing : httpd-tools-2.4.6-45.el7.centos.x86_64                       3/4 
  Installing : httpd-2.4.6-45.el7.centos.x86_64                             4/4 
  Verifying  : httpd-tools-2.4.6-45.el7.centos.x86_64                       1/4 
  Verifying  : apr-1.4.8-3.el7.x86_64                                       2/4 
  Verifying  : httpd-2.4.6-45.el7.centos.x86_64                             3/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  4/4 

Installed:
  httpd.x86_64 0:2.4.6-45.el7.centos                                            

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                     apr-util.x86_64 0:1.5.2-6.el7    
  httpd-tools.x86_64 0:2.4.6-45.el7.centos    

Complete!
"
    ]
}

----

原文地址:https://www.cnblogs.com/JiangLe/p/7239758.html