3.ansible 软件管理模块 cron yum service systemd

1.cron 定时任务模块:

作用:批量添加设置定时任务信息	
*       *      *     *       *        定时任务信息 &>/dev/null
分      时     日    月     周
minute  hour   day   month  weekday   job='任务信息 &>/dev/null'

参数:
minute  hour   day   month  weekday  --- 时间参数
job     -- 指定定时任务信息
name    -- 添加注释信息
state   -- absent 删除定时任务 present 添加定时任务
disabled   -- 注释定时任务 yes  取消定时任务注释 no

批量编写定时任务: 每隔5分钟,时间同步

ansible backup -m cron -a "name='date ntpdate crond02'  minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"
172.16.1.41 | CHANGED => {
"ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"
 }, 
        "changed": true, 
        "envs": [], 
        "jobs": [
            "date ntpdate crond", 
            "date ntpdate crond02"
        ]
    }

1.1 批量删除定时任务信息:

[root@m01 ~]# ansible backup -m cron -a "name='date ntpdate crond' state=absent"
    172.16.1.41 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": []
    }

1.2 批量注释定时任务信息:

[root@m01 ~]# ansible backup -m cron -a "name='date ntpdate crond'  minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null' disabled=yes"
    172.16.1.41 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": [
            "date ntpdate crond"
        ]
    }

2.yum 批量下载安装软件

作用:安装部署软件

	参数:
	name:  指定软件名称
	state: 指定动作信息  installed 
                present		安装
		absent		删除
		latest		最新版
		
	enablerepo		通过哪个仓库获取
	disablerepo		不使用哪些仓库的包
	exclude			kernel排除

2.1 安装软件

[root@m01 ~]# ansible backup -m yum -a "name=nmap state=present"
    172.16.1.41 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "msg": "", 
        "rc": 0, 
        "results": [
            "2:nmap-6.40-16.el7.x86_64 providing nmap is already installed"
        ]
    }

2.2 删除vsftpd

ansible webservers -m yum -a 'name=vsftpd state=absent'

2.3 安装httpd服务,必须从epel仓库中安装(所有的被控都有这个epel仓库)

ansible webservers -m yum -a 'name=httpd state=present enablerepo=epel'

2.4 更新所有的软件包,唯独kernel程序不更新;

ansible webservers -m yum -a 'name=* state=present exclude="kernel*"'

3.service 管理服务状态模块(service服务)

作用:批量启动/停止服务程序  设置服务是否开机自动启动
参数:
name:   定义服务名称
state:  是否启动 started 停止 stopped 重启 restarted 平滑重启 reloaded
enabled:设置服务是否开机自动启动 

设置服务启动/停止:
ansible backup -m service -a "name=firewalld state=started enabled=yes"
ansible backup -m service -a "name=firewalld state=stopped enabled=no"

4. systemd 管理服务状态(systemd)

启动: systemd
	name			服务名称
	state			服务状态
		started		启动
		stopped		停止
		restarted	重启
		reloaded	重载
	enabled			开启自启动|  yes 启   no 不
	daemon_reload: yes

1.启动服务,并加入开机自启动

ansible webservers -m systemd -a 'name=nfs state=started enabled=yes'

2.停止服务,并关闭开机自启动

ansible webservers -m systemd -a 'name=nfs state=stopped enabled=no'
posted @ 2021-03-27 16:22  老夫聊发少年狂88  阅读(72)  评论(0编辑  收藏  举报