The group module of ansible

ansible group module

The group module is used to add or delete groups

First use ansible doc to see usage

[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.

From the above parameter list, we can see that the group module has several important attributes

OPTIONS (= is mandatory): those with = in front of the options are required parameters

I. name

= name
        Name of the group to manage.
        type: str

Group name of the group to operate, string type, required

1.1, example

Create a group named test.

[root@note0 ~]# ansible local -m group -a "name=test"
176.16.128.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    <font color="red">"changed": true,</font>#You can see that the changed status is true, which means that the group has been added to the host successfully.
    "gid": 1000, 
    "name": "test", 
    "state": "present", 
    "system": false
}

View host / etc/group file validation

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

Two, state

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

state specifies whether the user group is changed or deleted on the remote host, string type.
There are two options: absent, present. The default value is present, and absent is the delete group.

2.1, example

Let's delete the group we just created.

[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"
}

Three, gid

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

gid Used to set up user groups gid,int Type, default is empty

3.1, example

Create a group with a gid of 1005 and a name of 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
}

Looking at the host / etc/group file, we can see that the newly created group gid is 1005.

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

Four, system

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

system Used to specify whether the created user group is a system group, boolean type, available options false,true,Default is false

4.1, example

Create a system group named 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
}

View host / etc/group file validation

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

It can be seen that the gid of the test group is 994, and the gid less than 1000 is the system group.

Tags: Linux ansible Python less

Posted on Sat, 02 Nov 2019 22:58:30 -0700 by kjelle392