Ansible服务常用命令模块详细解析(2)

[root@promote ~]# ansible-doc -s cron              #查看cron模块信息
- name: Manage cron.d and crontab entries
  cron:
      backup:                # If set, create a backup of the crontab before it
                              is modified. The
                              location of the
                              backup is
                              returned in the
                              `backup_file'
                              variable by this
                              module.
......

添加任务计划:

[root@promote ~]# ansible web -m cron -a 'minute="*/1" job="/usr/bin/echo hehe"'
192.168.199.130 | SUCCESS => {
    "changed": false,
    "envs": [],
    "jobs": [
        "test hehe"
    ]
}
[root@promote ~]# ansible web -a 'crontab -l'            #查看web主机的计划性任务
192.168.199.130 | CHANGED | rc=0 >>
#Ansible: test hehe
*/1 * * * * /usr/bin/echo hehe

移除任务计划:

[root@promote ~]# ansible web -m cron -a 'name="test hehe" state=absent'
192.168.199.130 | CHANGED => {
    "changed": true,
    "envs": [],
    "jobs": []
}
[root@promote ~]# ansible web -a 'crontab -l'
192.168.199.130 | CHANGED | rc=0 >>

3 user模块
ansible中的user模块用于创建新用户和更改,删除已存在的用户,其中name项用来指明创建的用户名称
user模块是请求的是useadd,userdel,usermod三个指令

创建一个名为test01的用户:

[root@promote ~]# ansible all -m user -a 'name=test01'
192.168.199.130 | CHANGED => {
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/test01",
    "name": "test01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}

删除test01用户:

[root@promote ~]# ansible all -m user -a 'name=test01 state=absent'
192.168.199.130 | CHANGED => {
    "changed": true,
    "force": false,
    "name": "test01",
    "remove": false,
    "state": "absent"
}

4 group 模块
ansible中的group模块用于对用户组进行管理
group模块请求的是groupadd,groupdel,groupmod三个指令

[root@promote ~]# ansible-doc -s group
- name: Add or remove groups
 group:
 gid:                  # Optional `GID' to set for the group.
 name:                  # (required) Name of the group to manage.
 state:                # Whether the group should be present or not onthe remote host.
 system:                # If `yes', indicates that the group created is asystem group.

下面我创建mysql组,将mysql用户添加到mysql组中

[root@promote ~]# ansible web -m group -a 'name=mysql gid=306 system=yes'
192.168.199.130 | CHANGED => {
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": true
}

[root@promote ~]# ansible web -m user -a 'name=mysql uid=306 system=yes group=mysql'
192.168.199.130 | CHANGED => {
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 306,
    "home": "/home/mysql",
    "name": "mysql",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 306
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/18866a283c06ff4c51d1bfc086a35575.html