【问题标题】:Ansible to update sshd config fileAnsible 更新 sshd 配置文件
【发布时间】:2020-07-10 15:39:08
【问题描述】:

我正在编写一个 Ansible 脚本,以在 100 多个 Unix 服务器中自动创建新用户。我已经掌握了创建用户并分配密码的部分。但我们的组织强化政策要求,每当添加新用户时,必须在 sshd_config 文件的“AllowUsers”参数中更新用户名。我是 Ansible 的新手,不知道如何完成这项工作。

这是 sshd_config 文件的“AllowUsers”部分。

AllowUsers root user1 user2 user2

这是添加新用户“testuser”后的样子

AllowUsers root user1 user2 testuser

【问题讨论】:

    标签: unix ssh ansible


    【解决方案1】:

    如果用户已经在列表中,我会搜索不执行任何操作的解决方案。这就是它在 Ansible 中的工作方式。我的解决方案首先搜索用户,只有当用户不在列表中时才会添加。

    tasks:
    - name: Check if bamboo user already is in SSHD AllowUsers list
      command: grep -P '^[ \t]*AllowUsers[ \t]+([-\w ]+[ \t]+)*bamboo([ \t]+.+)*$' /etc/ssh/sshd_config
      register: allow_users_exists
      changed_when: no
      ignore_errors: yes
    
    - name: Allow bamboo user SSH login
      lineinfile:
        regexp: ^[ \t]*AllowUsers([ \t]+.*)$
        line: AllowUsers bamboo\1
        dest: /etc/ssh/sshd_config
        backrefs: yes
        validate: sshd -t -f %s
      when: allow_users_exists.rc != 0
      notify:
        - reload sshd
    
    handlers:
    - name: reload sshd
      service:
        name: sshd
        state: reloaded
    

    在这种特殊情况下,我正在搜索静态用户“bamboo”。您可以像这样使用变量:

    command: grep -P '^[ \t]*AllowUsers[ \t]+([-\w ]+[ \t]+)*{{ username | regex_escape() }}([ \t]+.+)*$' /etc/ssh/sshd_config
    

    line: AllowUsers {{ username }}\1
    

    结果

    在:

    AllowUsers ubuntu #sdfd
    

    输出:

    AllowUsers bamboo ubuntu #sdfd
    

    在:

    AllowUsers ubuntu
    

    输出:

    AllowUsers bamboo ubuntu
    

    在:

    AllowUsers ubuntu bamboo
    

    输出:

    AllowUsers ubuntu bamboo
    

    【讨论】:

      【解决方案2】:

      与 lineinfile 模块匹配行的正则表达式说“^AllowUsers .+”并使用新用户名构造行。一些示例

      - command: grep "^AllowUsers " /etc/ssh/sshd_config
        register: old_user_list
      - lineinfile:
           regexp: "^AllowUsers .+"
           line: "{{ old_user_list.stdout }} {{new-user-name}}"
        when: old_user_list.rc == 0
      

      【讨论】:

      • 感谢您的及时回复。我会对此进行测试并告诉你。我只是想知道为什么使用“when: old_user_list.rc = 0”这一行?
      • 它的用途是只有在 grep 发现旧配置允许特定用户在 sshd_config 文件中使用 ssh 时才更新文件。因此,该任务只是按预期进行,如果尚未为 ssh 服务器启用,则不会将代码放入文件中。接受这个答案,让我知道这个方法是否对你有用。
      • 抛出错误The conditional check 'old_user_list.rc= 0' failed. The error was: template error while templating string: expected token 'end of statement block', got '='
      • 已更新代码。似乎需要 == 进行比较而不是 =
      • - name: Collect AllowUsers list from sshd config file command: bash -c "grep '^AllowUsers' /etc/ssh/sshd_config" ignore_errors: yes changed_when: no register: old_user_list - name: Append new username in the AllowUsers list lineinfile: regexp: "^AllowUsers" line: "{{ old_user_list.stdout }} {{newusername}}" dest: /etc/ssh/sshd_config when: - old_user_list is succeeded notify: - restart sshd handlers: - name: restart sshd service: name: sshd state: restarted
      猜你喜欢
      • 2019-09-06
      • 1970-01-01
      • 2010-11-23
      • 2018-03-17
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多