ToB企服应用市场:ToB评测及商务社交产业平台

标题: Ansible——user模块 [打印本页]

作者: 饭宝    时间: 2024-6-11 12:44
标题: Ansible——user模块
 
目次
参数总结
语法示例 
1. 创建用户
2. 删除用户
3. 修改用户属性
4. 锁定用户账户
5. 解锁用户账户
6. 添加用户到多个组
7. 修改或创建用户,并设置暗码
访问下令行资助
Playbook示例
1. 创建用户
2. 删除用户
3. 修改用户属性
4. 锁定息争锁用户账户
5. 添加用户到多个组
6. 设置 SSH 公钥
综合示例


user 模块用于管理系统用户,可以创建、删除、修改用户属性。它提供了一个同一的方法来包管用户账户的存在或不存在,以及其他相关的设置,好比暗码、组、home 目次等等。以下是一些常见操作和语法总结: 
参数总结


语法示例 


1. 创建用户

创建一个名为 john 的用户:
  1. ansible all -m user -a "name=john state=present"
复制代码

2. 删除用户

删除名为 john 的用户:
  1. ansible all -m user -a "name=john state=absent"
复制代码

3. 修改用户属性

创建用户 jane 并为其设置特定属性:
  1. ansible all -m user -a "name=jane comment='Jane Doe' uid=1042 group=admin home=/home/jane shell=/bin/bash password='{{ ''password'' | password_hash(''sha512'') }}'"
复制代码
(注意:此下令中暗码部分须要特定的处置惩罚,最好在 Playbook 中使用而不是在下令行直接敲)

4. 锁定用户账户

锁定名为 john 的用户账户:
  1. ansible all -m user -a "name=john password_lock=yes"
复制代码

5. 解锁用户账户

解锁名为 john 的用户账户:
  1. ansible all -m user -a "name=john password_lock=no"
复制代码

6. 添加用户到多个组

将用户 jane 添加到多个组:
  1. ansible all -m user -a "name=jane groups=wheel,staff append=yes"
复制代码

7. 修改或创建用户,并设置暗码

注意暗码应当进行加密处置惩罚,通常通过变量转达:
  1. ansible all -m user -a "name=jane password='<encrypted_password>'"
复制代码

访问下令行资助

查看 user 模块的全部选项和参数,可以使用以下下令:
  1. ansible-doc -s user
复制代码
 
Playbook示例

 
1. 创建用户

创建一个名为 john 的用户:
  1. ---
  2. - name: Ensure user 'john' is created
  3.   hosts: all
  4.   tasks:
  5.     - name: Create a user 'john'
  6.       user:
  7.         name: john
  8.         state: present
复制代码

2. 删除用户

删除名为 john 的用户及其 home 目次:
  1. ---
  2. - name: Ensure user 'john' is removed
  3.   hosts: all
  4.   tasks:
  5.     - name: Delete user 'john'
  6.       user:
  7.         name: john
  8.         state: absent
  9.         remove: yes
复制代码

3. 修改用户属性

创建用户 jane 并设置其属性:
  1. ---
  2. - name: Ensure user 'jane' is created with specific attributes
  3.   hosts: all
  4.   tasks:
  5.     - name: Create user 'jane' with specific attributes
  6.       user:
  7.         name: jane
  8.         comment: "Jane Doe"
  9.         uid: 1042
  10.         group: admin
  11.         groups: "wheel,staff"
  12.         home: /home/jane
  13.         shell: /bin/bash
  14.         password: "{{ 'password' | password_hash('sha512') }}"
复制代码

4. 锁定息争锁用户账户

锁定用户 john 的账户:
  1. ---
  2. - name: Ensure user 'john' is locked
  3.   hosts: all
  4.   tasks:
  5.     - name: Lock user 'john' account
  6.       user:
  7.         name: john
  8.         password_lock: yes
复制代码
解锁用户 john 的账户:
  1. ---
  2. - name: Ensure user 'john' is unlocked
  3.   hosts: all
  4.   tasks:
  5.     - name: Unlock user 'john' account
  6.       user:
  7.         name: john
  8.         password_lock: no
复制代码

5. 添加用户到多个组

将用户 jane 添加到多个组:
  1. ---
  2. - name: Ensure user 'jane' is a member of multiple groups
  3.   hosts: all
  4.   tasks:
  5.     - name: Add user 'jane' to multiple groups
  6.       user:
  7.         name: jane
  8.         groups: "wheel,staff"
  9.         append: yes
复制代码

6. 设置 SSH 公钥

为用户 jane 设置 SSH 公钥:
  1. ---
  2. - name: Set SSH public key for user 'jane'
  3.   hosts: all
  4.   tasks:
  5.     - name: Ensure user 'jane' has an SSH key
  6.       authorized_key:
  7.         user: jane
  8.         state: present
  9.         key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr..."
复制代码
 
综合示例

综合示例,展示了如安在同一 Playbook 中执行多种用户管理操作:
  1. ---
  2. - name: Manage multiple users
  3.   hosts: all
  4.   tasks:
  5.     - name: Ensure user 'john' exists with specific attributes
  6.       user:
  7.         name: john
  8.         comment: "John Doe"
  9.         uid: 1001
  10.         group: developers
  11.         groups: "devops"
  12.         home: /home/john
  13.         shell: /bin/bash
  14.         state: present
  15.     - name: Ensure user 'jane' exists with specific attributes
  16.       user:
  17.         name: jane
  18.         comment: "Jane Doe"
  19.         uid: 1002
  20.         group: developers
  21.         groups: "devops,sysadmins"
  22.         home: /home/jane
  23.         shell: /bin/zsh
  24.         state: present
  25.     - name: Ensure user 'jim' is removed
  26.       user:
  27.         name: jim
  28.         state: absent
  29.     - name: Lock user 'bob' account
  30.       user:
  31.         name: bob
  32.         password_lock: yes
  33.     - name: Configure authorized key for user 'john'
  34.       authorized_key:
  35.         user: john
  36.         state: present
  37.         key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr4..."
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4