In the documentation, there is an example of using the lineinfile module to edit /etc/sudoers.

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

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

Feels a bit hackish.

感觉有点hackish。

I assumed there would be something in the user module to handle this but there doesn't appear to be any options.

我假设用户模块中有一些东西可以处理这个问题,但似乎没有任何选择。

What are the best practices for adding and removing users to /etc/sudoers?

向/ etc / sudoers添加和删除用户的最佳做法是什么?

1 个解决方案

#1


43

That line isn't actually adding an users to sudoers, merely making sure that the wheel group can have passwordless sudo for all command.

该行实际上并没有将用户添加到sudoers,只是确保wheel组可以为所有命令提供无密码sudo。

As for adding users to /etc/sudoers this is best done by adding users to necessary groups and then giving these groups the relevant access to sudo. This holds true when you aren't using Ansible too.

至于将用户添加到/ etc / sudoers,最好通过将用户添加到必要的组,然后为这些组提供对sudo的相关访问。当你不使用Ansible时也是如此。

The user module allows you to specify an exclusive list of group or to simply append the specified groups to the current ones that the user already has. This is naturally idempotent as a user cannot be defined to be in a group multiple times.

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

An example play might look something like this:

一个示例游戏可能看起来像这样:

- 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 }}"

更多相关文章

  1. Python 3.x--模块导入
  2. Python脚本如何获取当前环节和用户等信息
  3. Django 1.5多用户身份验证或
  4. Python 安装 pip模块
  5. 基于python的opcode优化和模块按需加载机制研究(学习与个人思路)
  6. Python实战小程序——matplotlib模块画图
  7. python 带正则的search 模块
  8. python接入微博第三方API之2接入用户登录和微博发布
  9. python subprocess模块 监控子进程的2种方式 忙等待和立即返回同

随机推荐

  1. 使用 IntelliJ 调试 Java Streams
  2. 六一儿童节,送你一个F字符串
  3. 每周 10 道 Java 面试题:集合类
  4. 学习linux的第5天
  5. 如何使用maven 轻松重构项目
  6. iPhone要降价?巨头战争很激烈
  7. 【Java】几道让你拿offer的面试题
  8. 基础语法_Haskell笔记1
  9. 骗局!卸载搜狗输入法教程
  10. 【重磅】Google或借道搜狗回归中国,明天见