linux - Ansible - 禁用 ssh 密码认证

标签 linux shell ansible openssh

我正在尝试制作处理 sshd_config 的 ansible 任务适本地。我从其他问题中找到了类似的正则表达式,但它们什么也没做。

name: Disable SSH password authentication
      become: true
      lineinfile:
        dest: /etc/ssh/sshd_config
        regexp: '^#?\s*PasswordAuthentication\s'
        line: 'PasswordAuthentication no'
        state: present
问题是它应该处理重复的车道以及评论。例如:
  • 可能有:
  • PasswordAuthentication no
    PasswordAuthentication yes
    
    或者
    PasswordAuthentication no
    PasswordAuthentication no
    
    或者
    PasswordAuthentication yes
    PasswordAuthentication yes
    
    或者
    PasswordAuthentication no
    #PasswordAuthentication no
    
    或者
    PasswordAuthentication no
    # PasswordAuthentication no
    
    或者
    # PasswordAuthentication no
    # PasswordAuthentication no
    
    等等这么多组合。但我只想有一个未注释的行 PasswordAuthentication no这可能吗?

    最佳答案

    问: “处理重复的行和评论......有一个未注释的行 PasswordAuthentication no
    A:给定文件列表

        my_files:
          - sshd_config.0
          - sshd_config.1
          - sshd_config.2
          - sshd_config.3
          - sshd_config.4
          - sshd_config.5
    
    和内容
    shell> for f in files-17/*; do printf "\n%s\n" $f; cat $f; done
    
    files-17/sshd_config.0
    PasswordAuthentication no
    PasswordAuthentication yes
    
    files-17/sshd_config.1
    PasswordAuthentication no
    PasswordAuthentication no
    
    files-17/sshd_config.2
    PasswordAuthentication yes
    PasswordAuthentication yes
    
    files-17/sshd_config.3
    PasswordAuthentication no
    #PasswordAuthentication no
    
    files-17/sshd_config.4
    PasswordAuthentication no
    # PasswordAuthentication no
    
    files-17/sshd_config.5
    # PasswordAuthentication no
    # PasswordAuthentication no
    
    下面的任务删除除第一行之外的所有内容,其中包括 PasswordAuthentication
        - replace:
            path: 'files-17/{{ item }}'
            after: 'PasswordAuthentication'
            regexp: '^(.*)PasswordAuthentication(.*)$'
            replace: ''
          loop: "{{ my_files }}"
    

    shell> for f in files-17/*; do printf "\n%s\n" $f; cat $f; done
    
    files-17/sshd_config.0
    PasswordAuthentication no
    
    
    files-17/sshd_config.1
    PasswordAuthentication no
    
    
    files-17/sshd_config.2
    PasswordAuthentication yes
    
    
    files-17/sshd_config.3
    PasswordAuthentication no
    
    
    files-17/sshd_config.4
    PasswordAuthentication no
    
    
    files-17/sshd_config.5
    # PasswordAuthentication no
    
    下一个任务将这些行替换为 PasswordAuthentication no
        - lineinfile:
            path: 'files-17/{{ item }}'
            regexp: '^(.*)PasswordAuthentication(.*)$'
            line: 'PasswordAuthentication no'
          loop: "{{ my_files }}"
    
    shell> for f in files-17/*; do printf "\n%s\n" $f; cat $f; done
    
    files-17/sshd_config.0
    PasswordAuthentication no
    
    
    files-17/sshd_config.1
    PasswordAuthentication no
    
    
    files-17/sshd_config.2
    PasswordAuthentication no
    
    
    files-17/sshd_config.3
    PasswordAuthentication no
    
    
    files-17/sshd_config.4
    PasswordAuthentication no
    
    
    files-17/sshd_config.5
    PasswordAuthentication no
    
    任务的顺序是幂等的。

    关于linux - Ansible - 禁用 ssh 密码认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65228192/

    相关文章:

    linux - 如何连接到另一个VNC桌面?

    linux - 如何查找然后复制找到的文件,linux

    linux - 如何更改 bash 脚本的 `echo` 输出?

    python - 在 Ansible 中将 Jboss (WildFly) cli 输出转换为 json

    file - ansible 查找的替代方法,因为它总是在 localhost 上查找文件

    c++ - 线程在 "in __lll_lock_wait"点卡住了几个线程

    linux - 如何从 bash 中的字符串中获取数值

    python - 自动填充可变大小的命令行输入

    python - 解释 virtualenv activate 命令

    ansible - 列出任务 list 文件中的所有 ansible 主机