运维自动化神器ansible之group模块
admin
2023-03-01 04:41:26
0

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类型,必填项

1.1、示例

创建一个名字为test的组。

[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为删除组。

2.1、示例

我们来删除一下刚才创建的组。

[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类型,默认值为空

3.1、示例

创建一个gid为1005,名字为test的组。

[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文件,我们可以看到新创建的组gid为1005。

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

四、system

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

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

4.1、示例

创建一个名字为test的系统组。

[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为系统组。

相关内容

热门资讯

美媒:特朗普下令暂停空袭伊朗 美国“Axios”新闻网7月25日援引知情人士报道,特朗普24日指示美军不得对伊朗发动新的打击,打破...
法国野火“创历史纪录”:政坛吵... 综合法媒BFM新闻台、《费加罗报》报道,当地时间7月25日中午,内政部长努内兹(Laurent Nu...
51.79亿,携程真的疼了吗? 2026年7月25日,市场监管总局对携程反垄断案作出行政处罚:罚没款合计51.79亿元。但这个数字被...
一艘越南籍渔船在南沙遇险,29... 新华社三沙7月25日电(记者赵颖全)记者从三沙海上搜救中心获悉,25日傍晚,一艘越南籍渔船在南沙永暑...
成都如何耕好新型工业化“试验田... 7月24日,APEC数字周城市侧配套活动——制造强国建设专家研讨暨新型工业化推进交流会举行,院士专家...
活力中国调研行丨检测设备“长腿... 从排放控制的细分龙头到智能装备的新入局者,凯龙高科的转型路径,为观察传统制造业如何切入新赛道提供了一...
原创 别... 别被顶级奖项光环忽悠透了!拿菲尔兹奖的王虹,道出基础科研最现实的人间落差 开篇:大众脑补的名利双收,...
温州划龙桥惠耳——如何应对老年... 年近古稀的老人,近来时常出现耳鸣。周日,孩子们来了,跟她说话,声音小了她听不清,声音大了她嫌吵得慌,...
民调:包括共和党人在内,美国人... 据美国有线电视新闻网(CNN)当地时间24日报道,多项最新民调结果显示,包括许多共和党人在内,美国民...
一个人害得全县买不了榴莲,恶意... 作者:何涛 责编:任绍敏据央视报道,近日,湖南衡山县人民法院审结一起案件:当地一男子用AI伪造榴莲、...