Ansible学习笔记7

user模块:

user模块用于管理用户账户和用户属性。

如果是windows要换一个win_user模块。

创建用户:present:

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=aaa state=present"
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/aaa",
    "name": "aaa",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/aaa",
    "name": "aaa",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}
bash 复制代码
[root@agent1 tmp]# id aaa
uid=1000(aaa) gid=1000(aaa) groups=1000(aaa)


[root@agent2 ~]# id aaa
uid=1000(aaa) gid=1000(aaa) groups=1000(aaa)

更改用户uid::这些内容都是Linux基础。

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=aaa uid=2000"
192.168.17.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "append": false,
    "changed": false,
    "comment": "",
    "group": 1000,
    "home": "/home/aaa",
    "move_home": false,
    "name": "aaa",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 2000
}
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "append": false,
    "changed": false,
    "comment": "",
    "group": 1000,
    "home": "/home/aaa",
    "move_home": false,
    "name": "aaa",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 2000
}

更改用户的属性:那这块还需要自己再进一步的分析:

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=aaa home=/data/haha shell=/bin/false comment=@_@"
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "append": false,
    "changed": true,
    "comment": "@_@",
    "group": 1000,
    "home": "/data/haha",
    "move_home": false,
    "name": "aaa",
    "shell": "/bin/false",
    "state": "present",
    "uid": 2000
}
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "append": false,
    "changed": true,
    "comment": "@_@",
    "group": 1000,
    "home": "/data/haha",
    "move_home": false,
    "name": "aaa",
    "shell": "/bin/false",
    "state": "present",
    "uid": 2000
}

然后到agent机器上检查:

bash 复制代码
[root@agent1 tmp]# cat /etc/passwd |grep aaa
aaa:x:2000:1000:@_@:/data/haha:/bin/false

删除用户:

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=aaa state=absent"                              192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "aaa",
    "remove": false,
    "state": "absent"
}
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "aaa",
    "remove": false,
    "state": "absent"
}

然后再到agent的机器上检查,可见用户已删除:

bash 复制代码
[root@agent1 tmp]# id aaa
id: aaa: no such user
[root@agent1 tmp]# grep aaa /etc/passwd
[root@agent1 tmp]#

删除用户的时候需要注意删除家目录的问题:

删除家目录的场景:

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=bbb state=absent remove=yes"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "bbb",
    "remove": true,
    "state": "absent"
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "bbb",
    "remove": true,
    "state": "absent"
}
bash 复制代码
[root@agent1 tmp]# cd /home/bbb
-bash: cd: /home/bbb: No such file or directory

这两个,如果工作中需要可以试下。

相关推荐
筑梦之路5 天前
centos 9 Kickstart + Ansible自动化部署 —— 筑梦之路
centos·自动化·ansible
遇见火星5 天前
Ansible模块——通过 URL 下载文件
ansible
遇见火星5 天前
Ansible模块——管理100台Linux的最佳实践
linux·运维·ansible
Ashmcracker5 天前
在Ubuntu使用 Ansible 配置 Azure 资源的动态清单
ubuntu·microsoft·云计算·ansible·azure·devops
遇见火星6 天前
Ansible模块——文件内容修改
数据库·mysql·ansible
Johny_Zhao7 天前
AI+自动化测试系统方案:网络设备与网络应用智能测试
linux·网络·人工智能·python·网络安全·docker·ai·信息安全·云计算·ansible·shell·cisco·huawei·系统运维·itsm·华三·deepseek
IT成长日记7 天前
【Ansible基础】Ansible设计理念与无代理架构深度解析
架构·自动化·ansible·无代理
IT成长日记8 天前
【Ansible基础】Ansible执行流程详解:从Playbook到实际任务
ansible·playbook·执行流程
麦a~M了M9 天前
ansible
linux·运维·ansible
IT成长日记10 天前
【Ansible基础】Ansible 核心组件深度解析:控制节点、受管节点、Inventory与Playbook
ansible·playbook·控制节点·受管节点·inventory