【问题标题】:User Management with Ansible使用 Ansible 进行用户管理
【发布时间】:2016-01-19 16:09:39
【问题描述】:

我想通过 Ansible 实现以下用户管理: 我有名为 user[1-3] 的用户,名为 admin 和 developer 的用户组。我有一堆服务器,根据他们的用户组,应该添加或删除用户。

目前的工作是将一个用户组添加到一个用户,并将该用户组中的所有用户添加到一个特定的服务器组。

这是我的文件:

users.yml:

---
os_groups:
  - name: sudo
    system: yes

os_users:
  - name: userone
    comment: 'Test User'
    usergroups:
      - developer
      - admin
    authorized_keys:
      - key: 'ssh-rsa ...'

  - name: usertwo
    comment: 'Test User 2'
    usergroups:
      - developer
    state: present
    authorized_keys:
      - key: 'ssh-rsa ...'

  - name: userthree
    usergroups:
      - admin
    comment: 'Test User 3'
    authorized_keys:
      - key: 'ssh-rsa ...'

剧本

---
- hosts: testhosts1
  sudo: yes
  vars_files:
    - users.yml

  roles:
    - role: configure-os_users
      usergroup:
        - admin

- hosts: testhosts2
  sudo: yes
  vars_files:
    - users.yml

  roles:
    - role: configure-os_users
      usergroup:
        - admin
        - usergroup1

任务

- name: Ensure users present
  sudo: yes
  user: >-
    'name={{ item.name }}'
    {% if item.append is defined %}
    'append={{ item.append }}'
    {% endif %}
    {% if item.comment is defined %}
    'comment={{ item.comment }}'
    {% endif %}
    {% if item.createhome is defined %}
    'createhome={{ item.createhome }}'
    {% endif %}
    {% if item.force is defined %}
    'force={{ item.force }}'
    {% endif %}
    {% if item.generate_ssh_key is defined %}
    'generate_ssh_key={{ item.generate_ssh_key }}'
    {% endif %}
    {% if item.group is defined %}
    'group={{ item.group }}'
    {% endif %}
    {% if item.groups is defined %}
    'groups={{ item.groups }}'
    {% else %}
    'groups=sudo'
    {% endif %}
    {% if item.home is defined %}
    'home={{ item.home }}'
    {% endif %}
    {% if item.login_class is defined %}
    'login_class={{ item.login_class }}'
    {% endif %}
    {% if item.move_home is defined %}
    'move_home={{ item.move_home }}'
    {% endif %}
    {% if item.non_unique is defined %}
    'non_unique={{ item.non_unique }}'
    {% endif %}
    {% if item.password is defined %}
    'password={{ item.password }}'
    {% else %}
    'password=XXXXX'
    {% endif %}
    {% if item.remove is defined %}
    'remove={{ item.remove }}'
    {% endif %}
    {% if item.shell is defined %}
    'shell={{ item.shell }}'
    {% else %}
    'shell=/bin/bash'
    {% endif %}
    {% if item.ssh_key_bits is defined %}
    'ssh_key_bits={{ item.ssh_key_bits }}'
    {% endif %}
    {% if item.ssh_key_comment is defined %}
    'ssh_key_comment={{ item.ssh_key_comment }}'
    {% endif %}
    {% if item.ssh_key_file is defined %}
    'ssh_key_file={{ item.ssh_key_file }}'
    {% endif %}
    {% if item.ssh_key_passphrase is defined %}
    'ssh_key_passphrase={{ item.ssh_key_passphrase }}'
    {% endif %}
    {% if item.ssh_key_type is defined %}
    'ssh_key_type={{ item.ssh_key_type }}'
    {% endif %}
    {% if item.state is defined %}
    'state={{ item.state }}'
    {% endif %}
    {% if item.system is defined %}
    'system={{ item.system }}'
    {% endif %}
    {% if item.uid is defined %}
    'uid={{ item.uid }}'
    {% endif %}
    {% if item.update_password is defined %}
    'update_password={{ item.update_password }}'
    {% else %}
    'update_password=on_create'
    {% endif %}
  with_items: os_users
  when: usergroup in item.usergroups

我希望每个用户能够使用多个用户组,当我从该用户中删除一个用户组时,该用户也应该从服务器中删除。

我来回搜索了网络和 Ansible Galaxy,但没有找到任何适合的内容。

您知道我该如何继续吗?特别是关于从用户组中删除用户时删除用户?

如果有一些不是 Ansible 但可以完成工作的东西,我也愿意接受想法。

谢谢,

【问题讨论】:

    标签: ubuntu ubuntu-14.04 ansible ansible-playbook user-management


    【解决方案1】:

    嗯,大多数地方都会告诉你使用 LDAP,但如果你想通过 Ansible 来做 - 你最好的办法是将变量构建为带有条件标志的子元素列表。因此,当您将“John Doe”与“Group:Team 1”和“Group:Development”和“Group:Special Permissions”添加时 - 它会在服务器上关联适当的组。

    http://docs.ansible.com/ansible/playbooks_loops.html

    问题是从剧本的角度来看,它不是很容易维护。如果您还没有域帐户,您可能想看看 LinOTP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 2016-09-23
      • 1970-01-01
      • 2018-08-07
      • 2011-06-10
      • 2018-05-01
      • 1970-01-01
      相关资源
      最近更新 更多