ansible常用模块之group模块和user模块(八)

简介: 1.group模块语法格式

1.group模块

语法格式

ansible 模块名 -m group -a "name=组名 gid=组id"
参数
name    //需要管理的组名,也就是要对那个组进行管理
gid     //设置组id
state   //执行状态
  absent    //删除
  present   //创建(默认)

案例1:创建组名www,并设置gid为777

all表示所有主机组

[root@ansible ~]# ansible all -m group -a "name=group1 gid=777 "
192.168.81.230 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 777, 
    "name": "group1", 
    "state": "present", 
    "system": false
}

案例2:修改www组的gid为888

[root@ansible ~]# ansible all -m group -a "name=www gid=888"

案例3:删除www组

[root@ansible ~]# ansible all -m group -a "name=www gid=666 state=absent"

删除组的时候可能会报错,下图报错是有程序正在使用该组

解决方法,将对应的进程杀掉即可
[root@ansible ~]# ansible all -m shell -a "ps aux | awk '/^www/{print $2}' | xargs kill -9"

2.user模块

语法格式

ansible 主机组 -m user -a "name=用户名,uid=用户id,group=组id或者组名"
参数
name    //用户名
uid     //用户的uid
group     //组id或者组名
state     //执行状态
  absent  //删除
  present //创建
shell   //登录shell,/bin/bash /sbin/nologin
create_home   //创建用户时,是否创建家目录
password    //用户密码,不能使用明文

案例1:创建一个而用户jiangxl,指定uid为10000,gid为10007,并设置密码为123

注意:要使用加密的密码

1.创建加密密码
[root@ansible ~]# echo "123" | openssl passwd -1 -stdin 
$1$c1D.OvTM$Ar9Yy8WXVmtGiU2O3FbPi.
passwd -1表示使用MD5进行加密
2.创建用户,注意password后面的字符串要用双引号引用
[root@ansible ~]# ansible all -m user -a 'name=jiang uid=7777 group=6666 password="$1$c1D.OvTM$Ar9Yy8WXVmtGiU2O3FbPi."'

案例2:创建一个程序用户linux,指定uid6666,gid6666,不允许登录,不允许创建家目录

1.首先创建gid为6666的组
[root@ansible ~]# ansible all -m group -a "name=linux gid=6666"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 6666, 
    "name": "linux", 
    "state": "present", 
    "system": false
}
2.再创建程序用户
[root@ansible ~]# ansible all -m user -a "name=linux uid=6666 group=6666 shell=/sbin/nologin create_home=no"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": false, 
    "group": 6666, 
    "home": "/home/linux", 
    "name": "linux", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": false, 
    "uid": 6666
}

案例3:删除jiang用户

[root@ansible ~]# ansible all -m user -a "name=jiang uid=7777 state=absent"
目录
相关文章
|
6月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
|
8月前
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
144 0
|
6月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
|
9月前
|
Shell
ansible模块大全上【建议收藏】
ansible模块大全上【建议收藏】
104 0
ansible模块大全上【建议收藏】
|
5月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
|
5月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
47 0
|
8月前
Ansible模块管理——磁盘管理模块、mount模块
Ansible模块管理——磁盘管理模块、mount模块
250 0
|
8月前
|
网络协议 网络安全 数据安全/隐私保护
Ansible模块介绍——配置网络模块、上传下载文件模块
Ansible模块介绍——配置网络模块、上传下载文件模块
273 0
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块
|
8月前
|
数据安全/隐私保护 Python
Ansible模块介绍——用户管理模块
Ansible模块介绍——用户管理模块