接上一微博:注意master为服务器主机,client为客户端主机!

ansible怎么实现任务计划:在讲实现任务计划之前务必先了解一下crontab这个命令;

[root@master ~]# ansible 192.168.1.112 -m cron -a "name='test cron' job='/bin/touch /tmp/12.tst' weekday=6"
192.168.1.112 | success >> {
    "changed": true, 
    "jobs": [
        "test cron"
    ]
}

[root@master ~]#

注意ansible的命令组成,-m后面加要使用的模块,name是对任务计划的命名,为了方便操作,job是要执行的命令,最后面就是cron任务计划的时间,其中分钟 minute 小时 hour日期 day 月份 month 一个月的第几周用weekday;

[root@client ~]# crontab -l    /在客户端查看任务计划!
#Ansible: test cron
* * * * 6 /bin/touch /tmp/12.tst
[root@client ~]#

若想删除任务计划可用;直接在后面加一个字段“state=absent"即可,如下,这个等待的时间有点长!

[root@master ~]# ansible 192.168.1.112 -m cron -a "name='test cron' state=absent"
192.168.1.112 | success >> {
    "changed": true, 
    "jobs": []
}

[root@master ~]#

ansible的rpm包安装与管理;在这里使用的是yum模块!我们用ansible在客户端安装一个sendmail!

[root@master ~]# ansible 192.168.1.112 -m yum -a "name=sendmail"
192.168.1.112 | success >> {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.btte.net\n * epel: mirror.premi.st\n * extras: mirrors.btte.net\n * updates: mirrors.btte.net\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package sendmail.i686 0:8.14.4-9.el6 will be installed\n--> Processing Dependency: procmail for package: sendmail-8.14.4-9.el6.i686\n--> Running transaction check\n---> Package procmail.i686 0:3.22-25.1.el6_5.1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch          Version                    Repository     Size\n================================================================================\nInstalling:\n sendmail          i686          8.14.4-9.el6               base          714 k\nInstalling for dependencies:\n procmail          i686          3.22-25.1.el6_5.1          base          159 k\n\nTransaction Summary\n================================================================================\nInstall       2 Package(s)\n\nTotal download size: 872 k\nInstalled size: 1.8 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           144 kB/s | 872 kB     00:06     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : procmail-3.22-25.1.el6_5.1.i686                              1/2 \n\r  Installing : sendmail-8.14.4-9.el6.i686                                   2/2 \n\r  Verifying  : sendmail-8.14.4-9.el6.i686                                   1/2 \n\r  Verifying  : procmail-3.22-25.1.el6_5.1.i686                              2/2 \n\nInstalled:\n  sendmail.i686 0:8.14.4-9.el6                                                  \n\nDependency Installed:\n  procmail.i686 0:3.22-25.1.el6_5.1                                             \n\nComplete!\n"
    ]
}

[root@master ~]#

检查一下客户端,是否安装sendmail。

[root@client ~]# ps aux |grep sendmail
root      1699  0.0  0.0   4356   716 pts/0    S+   04:59   0:00 grep sendmail
[root@client ~]#

怎么用ansible管理系统服务;这里使用的service模块;这里我们通过ansible启动nfs服务,先检查一下看客户端的nfs是否启动:

[root@client ~]# service nfs status
rpc.svcgssd is stopped
rpc.mountd is stopped  
nfsd is stopped                     /未启动,另一个是和通信协议相关的!
rpc.rquotad is stopped
[root@client ~]#

在服务端操作:

[root@master ~]# ansible 192.168.1.112 -m service -a "name=nfs state=started enabled=yes"
192.168.1.112 | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "nfs", 
    "state": "started"
}

[root@master ~]#    /注意一下这个命令,name指明服务对象,state指明服务对象执行的动作(英语单词为过去分词) enabled为开机启动的意思/

在客户端操作验证一下:

[root@client ~]# service nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 1924) is running...
nfsd (pid 1939 1938 1937 1936 1935 1934 1933 1932) is running...
rpc.rquotad (pid 1920) is running...
[root@client ~]# chkconfig --list |grep nfs
nfs            	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nfslock        	0:off	1:off	2:off	3:on	4:on	5:on	6:off
[root@client ~]#     /可以发现nfs服务已经启动,并且已经加入开机启动中!

ansible-doc -l  列出所有的模块,也就是查看ansible的所有模块,

ansible-doc cron 查看指定模块的文档,可以具体查看某个文档是怎么用的!

接下来我们介绍一下ansible的playbook,这个东西就类似于我们在linux系统中的shell脚本,就是把一些命令写入里面,然后我们就可以一起来执行,而不必要一条一条的敲命令!

首先写成的脚本文件必须是以.yml为后缀的,且必须放在/etc/ansible这个目录的下面!

[root@master ansible]# vim 1.yml   /写第一个脚本

---                                /脚本的规则,类似于shell脚本开头的“#!”,三个减号
- hosts: 192.168.1.112            /一个减号,空一格,hosts指明我们操作的对象,也可以是一个在ansible中已经定义的一个组。冒号后面要有空格
  remote_user: root               /指明以什么样的身份操作,同样冒号后面要空格
  tasks:                          /任务,也就是要干什么
   - name: test_playbook           /任务的名字
     shell: touch /tmp/1.test      /具体干什么

执行刚完成的脚本;

[root@master ansible]# ansible-playbook 1.yml 

PLAY [192.168.1.112] ********************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.1.112]

TASK: [test_playbook] ********************************************************* 
changed: [192.168.1.112]

PLAY RECAP ******************************************************************** 
192.168.1.112              : ok=2    changed=1    unreachable=0    failed=0

在客户端发现新建的文件

[root@client ~]# ls -l !$
ls -l /tmp/test1
-rw-r--r--. 1 root root 28 Mar  7 06:01 /tmp/test1