Ansible:维护sudoers列表的最佳实践

2024-06-24 13:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

documentation中,有一个使用lineinfile模块编辑/etc/sudoers的示例。

- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'"

感觉有点老土。

我以为user模块中会有一些东西来处理这个问题,但似乎没有任何选项。

/etc/sudoers中添加和删除用户的最佳实践是什么?


Tags: 模块编辑示例documentationlineetcalldest
1条回答
网友
1楼 · 发布于 2024-06-24 13:52:54

这一行实际上并没有向sudoers添加用户,只是确保wheel组可以使用password sudo for all命令。

至于将用户添加到/etc/sudoers中,最好将用户添加到必要的组中,然后为这些组提供sudo的相关访问权限。当你不使用Ansible的时候,这也是正确的。

user module允许您指定组的独占列表,或者只需将指定的组附加到用户已经拥有的当前组。这自然是等幂的,因为用户不能多次定义为在一个组中。

一个例子可能是这样的:

- hosts: all
  vars:
    sudoers:
      - user1
      - user2
      - user3
  tasks:
    - name: Make sure we have a 'wheel' group
      group:
        name: wheel
        state: present

    - name: Allow 'wheel' group to have passwordless sudo
      lineinfile:
        dest: /etc/sudoers
        state: present
        regexp: '^%wheel'
        line: '%wheel ALL=(ALL) NOPASSWD: ALL'
        validate: visudo -cf %s

    - name: Add sudoers users to wheel group
      user:
        name: "{{ item }}"
        groups: wheel
        append: yes
      with_items: "{{ sudoers }}"

相关问题 更多 >