运维自动化神器ansible之group模块

 

ansible之group模块

group模块是用来添加或者删除组

 

首先使用ansible-doc来查看用法

[root@note0 ansible]# ansible-doc -s group
- name: Add or remove groups
  group:
      gid:                   # Optional `GID' to set for the group.
      local:                 # Forces the use of "local" command alternatives on platforms that implement it. This is useful in environments that use centralized
                               authentication when you want to manipulate the local groups. (e.g. it uses `lgroupadd' instead of
                               `groupadd'). This requires that these commands exist on the targeted host, otherwise it will be a fatal
                               error.
      name:                  # (required) Name of the group to manage.
      non_unique:            # This option allows to change the group ID to a non-unique value. Requires `gid'. Not supported on macOS or BusyBox distributions.
      state:                 # Whether the group should be present or not on the remote host.
      system:                # If `yes', indicates that the group created is a system group.

 

 

通过上面的参数列表我们可以了解到group模块有几个重要属性

OPTIONS (= is mandatory):选项前面为=的为必填参数

 

一、name

= name
        Name of the group to manage.

        type: str

 要操作的group的组名,string类型,必填项

 

用法

[root@note0 ~]# ansible local -m group -a "name=test"
176.16.128.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true,  #可以看到changed状态为true,代表已经在主机添加组成功。
    "gid": 1000, 
    "name": "test", 
    "state": "present", 
    "system": false
}

 

查看主机/etc/group文件验证

[root@note1 ~]# cat /etc/group
test:x:1000:

 

二、state

- state
        Whether the group should be present or not on the remote host.
        (Choices: absent, present)[Default: present]
        type: str

state用于指定用户组在远程主机上是否被更改或删除,string类型。

有两个选项:absent,present。默认值为present,absent为删除组。

 

用法,我们来删除一下刚才创建的组

[root@note0 ~]# ansible local -m group -a "name=test state=absent"
176.16.128.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "test", 
    "state": "absent"
}

 

三、gid

- gid
        Optional `GID' to set for the group.
        [Default: (null)]
        type: int

gid用于设定用户组gid,int类型,默认值为空

 

用法

[root@note0 ~]# ansible local -m group -a "name=test gid=1005 state=present"
176.16.128.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1005, 
    "name": "test", 
    "state": "present", 
    "system": false
}

 

查看主机/etc/group文件

[root@note1 ~]# cat /etc/group
test:x:1005:

 我们可以新创建的组gid为1005

 

四、system

- system
        If `yes', indicates that the group created is a system group.
        [Default: False]
        type: bool

system用于指定创建的用户组是否为系统组,布尔类型,可用选项false,true,默认为false

 

用法

[root@note0 ~]# ansible local -m group -a "name=test state=present system=true"
176.16.128.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 994, 
    "name": "test", 
    "state": "present", 
    "system": true
}

 

查看主机/etc/group文件验证

[root@note1 ~]# cat /etc/group
test:x:994:

可以看到test组的gid为994,gid小于1000为系统组。

 

转载于:https://www.cnblogs.com/miaocunf/p/11132015.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值