Ansible:维护 sudoers 列表的最佳实践
Ansible: best practice for maintaining list of sudoers
在documentation中有一个使用lineinfile
模块编辑/etc/sudoers
的例子。
- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'"
感觉有点老套。
我以为 user
模块中会有一些东西来处理这个,但似乎没有任何选项。
向 /etc/sudoers
添加和删除用户的最佳做法是什么?
该行实际上并没有将用户添加到 sudoers,只是确保 wheel
组可以对所有命令使用无密码 sudo。
至于将用户添加到 /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 }}"
如果可能的话,我更愿意为此使用 /etc/sudoers.d/
(这风险更小,更模块化和自我描述),所以这种方法看起来像:
$ cat files/*
%admins ALL=(ALL) NOPASSWD: ALL
$ cat tasks/*
- name: sudoers | Create sudoers.d files
copy:
src: ./
dest: /etc/sudoers.d
owner: root
group: root
mode: ug+rwX,o=
force: yes
文件已使用 visudo -cf file_name
预先检查。
在documentation中有一个使用lineinfile
模块编辑/etc/sudoers
的例子。
- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'"
感觉有点老套。
我以为 user
模块中会有一些东西来处理这个,但似乎没有任何选项。
向 /etc/sudoers
添加和删除用户的最佳做法是什么?
该行实际上并没有将用户添加到 sudoers,只是确保 wheel
组可以对所有命令使用无密码 sudo。
至于将用户添加到 /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 }}"
如果可能的话,我更愿意为此使用 /etc/sudoers.d/
(这风险更小,更模块化和自我描述),所以这种方法看起来像:
$ cat files/*
%admins ALL=(ALL) NOPASSWD: ALL
$ cat tasks/*
- name: sudoers | Create sudoers.d files
copy:
src: ./
dest: /etc/sudoers.d
owner: root
group: root
mode: ug+rwX,o=
force: yes
文件已使用 visudo -cf file_name
预先检查。