8. cron   周期计划模块

用的不多,好像做时间同步的时候用了下

1. 创建周期任务

#每分钟执行一次输出
ansible all -m cron -a 'minute=*  job="/usr/bin/wall hello world" name=test-cron-job'


#参数说明
minute=*  #每分钟执行一次
weekday=*   #工作日,这里可以写1,2,3,4,5,6,7 ,表示周几执行
job="/usr/bin/wall warning"   #要执行的任务
name=test-cron-job       #关于这个job任务的名称


#查看周期任务
[root@web1 ansible]# crontab -l
#Ansible: test-cron-job
* * * * * /usr/bin/wall warning

2. 隔离周期任务

#停止,但不删除周期任务
ansible all -m cron -a 'disabled=true job="/usr/bin/wall hello world" name=test-cron-job'

#查看任务
[root@web1 ansible]# crontab -l
#Ansible: test-cron-job
#* * * * * /usr/bin/wall hello world

3. 恢复周期任务

#启动周期任务
ansible all -m cron -a 'disabled=false job="/usr/bin/wall hello world" name=test-cron-job'

[root@web1 ansible]# crontab -l
#Ansible: test-cron-job
* * * * * /usr/bin/wall hello world

4. 删除周期任务

ansible all -m cron -a 'job="/usr/bin/wall hello world" name=test-cron-job state=absent'

 

9. yum 仓库模块

      主机上必须要有yum源可用,用于主机上批量安装服务 

      #环境yum不太全,没用过,自己玩玩就好

#安装httpd
ansible -i hosts web -m yum -a "name=httpd"

#查看是否执行成功
ansible -i hosts web -m shell -a "echo $?"

#查看所有安装的包
ansible -i hosts web -m yum -a "list=installed"

#卸载安装的httpd服务
ansible -i hosts web -m yum -a "name=httpd state=removed"

#查看是否卸载
ansible -i hosts web -m shell -a "rpm -q httpd"


#参数说明
present     #默认安装
installed   #查看已安装的包
latest      #安装最新的包吧
removed     #删除指定的包

其他案例

#安装多个包(逗号隔开)
ansible all -m yum -a "name=vsftpd,memcached,httpd" 

#卸载多个
ansible all -m yum -a "name=vsftpd,memcached,httpd state=removed" 


#使用本地的一个rpm包安装到其他所有主机
mkdir /data
#将软件包放入/data目录
ansible all -m copy -a "src=/data/rpm包 dest=/root"

ansible all -m yum -a "name=/root/rpm包 disable_gpg_check=yes"
#这里可以忽略gpg检测



#更新缓存
ansible all -m yum -a "update_cache=yes"

#安装监控工具
ansible all -m yum -a "name=dstat update_cache=yes" 

 

 

 

10. service 服务模块

   在部署服务的时候用来启动、停止、重启服务用的

#启动服务(httpd) 并设置为开启自启
ansible -i hosts web -m service -a "name=httpd state=started enabled=yes"


#查看运行端口
ansible -i hosts web -m shell -a "ss -ntl | grep 80"



#停止服务
ansible -i hosts web -m service -a "name=httpd state=stopd"


#重启服务
ansible -i hosts web -m service -a "name=httpd state=restartd"

 

11 user  用户模块

 这还用说吗 (~﹃~)~zZ

#创建用户
ansible -i hosts web -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin uid=80 comment="nginx service"'


#参数说明
shell=/sbin/nologin  #是否允许登陆
system=yes           #是否为系统用户
home=/var/nginx      #指定家目录
groups=root          #指定用户组为root,bin两个组
uid=80               #uid值
comment="nginx service"  #用户描述信息 通过cat /etc/passwd | grep nginx查看



#查看用户id
ansible -i hosts web -a 'getent passwd nginx'


#删除用户(remove=yes 是否一起删除家目录)
ansible -i hosts web -m user -a "name=nginx state=absent remove=yes"

 

12  group 用户组模块

#创建组
ansible -i hosts web -m group -a "name=nginx system=yes gid=80"

#添加nginx用户
ansible -i hosts web -a "getent group nginx"

#同时删除用户、组
ansible -i hosts web -m group -a "name=nginx state=absent"