Ansible命令模块(group模块 user模块 cron模块 )

1.group模块

1)帮助语法

EXAMPLES:
- name: Ensure group "somegroup" exists
  group:
    name: somegroup                #组名字
    state: 
        present                    #创建用户组
        absent                    #删除用户组
    gid: 666                    #用户组ID

2)实例

#创建用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www state=present gid=666'

#删除用户组
[root@m01 ~]# ansible web_group -m group -a 'name=www state=absent'

2.user模块

1)帮助语法

- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
  user:
    name: johnd                            #用户名
    comment: John Doe                    #用户的注释
    uid: 1040                            #用户id
    group: admin                        #用户的组
    groups: admins,developers             #指定附加组
    shell: /bin/bash                    #指定登录脚本
    append: yes                            #添加附加组时使用
    remove: yes                            #移除家目录
    generate_ssh_key: yes                 #是否生成密钥对
    ssh_key_bits: 2048                    #秘钥加密的位数
    ssh_key_file: .ssh/id_rsa             #秘钥文件
    expires: 1422403387                     #用户的有效时间
    state:
        present                            #添加用户
        absent                            #删除用户
    create_home:yes/no                     #是否创建家目录
    password                            #给用户添加密码(单引号)

2)实践

#1.创建用户
[root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present'

#2.仅删除用户
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent'

#3.删除用户及用户组
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes'

#注意:
    1.如果用户名字跟组名字相同,删除用户是会将组也删除
    2.当组下面有多个用户,删除的与组同名的用户也不会删除组

3.cron模块

1)帮助语法

EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d
  cron:
    name: "check dirs"                    #定时任务的注释
    minute: "0"                            #分钟
    hour: "5,2"                            #小时
    day: "2"                            #日
    month: "2"                            #月
    weekday: "2"                        #周
    job: "ls -alh > /dev/null"              #定时任务的内容
    state: 
        absent                            #删除定时任务
        present                            #添加定时任务

2)实践

#1.添加定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'

#2.修改定时任务(不修改名字,只修改内容)
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'

#3.删除定时任务(一定要用name参数)
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" state=absent'

#4.注释定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes'
原文地址:https://www.cnblogs.com/chenlifan/p/13777248.html