首页 文章

Ansible取消注释文件中的行

提问于
浏览
3

我想通过使用Ansible取消注释文件 sshd_config 中的一行,我有以下工作配置:

- name: Uncomment line from /etc/ssh/sshd_config
    lineinfile:
      dest: /etc/ssh/sshd_config
      regexp: '^#AuthorizedKeysFile'
      line: 'AuthorizedKeysFile    .ssh/authorized_keys'

但是,此配置仅在行以 #AuthorizedKeysFile 开头时才有效,但如果该行以 # AuthorizedKeysFile# AuthorizedKeysFile# 与单词之间的空格)开头,则该配置无效 .

如何配置正则表达式,以便在“#”之后不考虑任何数量的空格?


我试图在'#'之后添加另一个带有空格的lineinfile选项,但这不是一个好的解决方案:

- name: Uncomment line from /etc/ssh/sshd_config
    lineinfile:
      dest: /etc/ssh/sshd_config
      regexp: '# AuthorizedKeysFile'
      line: 'AuthorizedKeysFile    .ssh/authorized_keys'

3 回答

  • 4

    如果在'#'字符后需要零个或多个空格,则以下内容应足够:

    - name: Uncomment line from /etc/ssh/sshd_config
        lineinfile:
          dest: /etc/ssh/sshd_config
          regexp: '^#\s*AuthorizedKeysFile.*$'
          line: 'AuthorizedKeysFile    .ssh/authorized_keys'
    

    对原始代码的修改是在正则表达式中添加 \s*.*$ .

    说明:

    \s - 匹配空格(空格,制表符,换行符和换页符)

    * - 指定左边的表达式( \s )在匹配中可以有零个或多个实例

    .* - 匹配任何字符的零个或多个

    $ - 匹配行尾

  • 6

    首先,您使用的是错误的语言 . 使用Ansible,你不能 Uncomment line form /etc/ssh/sshd_config ,而是 Ensure AuthorizedKeysFile is set to .ssh/authorized_keys .

    其次,初始状态是什么并不重要(如果该行被评论,或不是) . 您必须指定标识该行的唯一唯一字符串 .

    使用 sshd_config 这是可能的,因为 AuthorizedKeysFile 指令在文件中只出现一次 . 使用其他配置文件可能会更困难 .

    - name: Ensure AuthorizedKeysFile is set to .ssh/authorized_keys
      lineinfile:
        dest: /etc/ssh/sshd_config
        regexp: AuthorizedKeysFile
        line: 'AuthorizedKeysFile    .ssh/authorized_keys'
    

    它将匹配包含 AuthorizedKeysFile 字符串的任何行(无论是否注释,或者有多少空格)并确保整行是:

    AuthorizedKeysFile .ssh/authorized_keys

    如果该行不同,Ansible将报告“已更改”状态 .

    在第二次运行时,Ansible将再次找到 AuthorizedKeysFile 并发现该行已处于所需状态,因此它将以"ok"状态结束任务 .


    上述任务的一个警告是,如果任何行包含注释,例如真实的,有意的注释(例如,包含字符串 AuthorizedKeysFile 的英语说明),Ansible将使用 line 中指定的值替换该行 .

  • 1

    我应该用@ techraf的观点来警告这一点,99%的时间配置文件的完整 template 几乎总是更好 .

    我完成的时间 lineinfile 包括由其他一些进程管理的奇怪和精彩的配置文件,或者配置的懒惰我还没有想要维护所有变体......

    前往学习更多Ansible ...它很棒,因为你可以继续从原始的bash shell命令迭代到最佳实践 .

    lineinfile模块

    仍然很高兴看到如何最好地配置管理一个或两个设置只是更好用这个:

    tasks:
    - name: Apply sshd_config settings
      lineinfile:
        path: /etc/ssh/sshd_config
        # might be commented out, whitespace between key and value
        regexp: '^#?\s*{{ item.key }}\s'
        line: "{{ item.key }} {{ item.value }}"
        validate: '/usr/sbin/sshd -T -f %s'
      with_items:
      - key: MaxSessions
        value: 30
      - key: AuthorizedKeysFile    
        value: .ssh/authorized_keys
      notify: restart sshd
    
    handlers:
    - name: restart sshd
      service: 
        name: sshd
        state: restarted
    
    • validate 如果更改无效,请勿进行更改

    • notify / handlers 只在结束时重启一次的正确方法
      如果您有多个设置,

    • with_items (即将成为 loop

    • ^#? 该设置可能已被注释掉 - 请参阅其他答案

    • \s*{{ item.key }}\s 将与其他设置不匹配(即 SettingA 无法匹配 NotSettingASettingAThisIsNot

    仍然可能会破坏像我们必须忍受的 # AuthorizedKeysFile - is a setting 这样的评论,因为可能有像_655385这样的设置......重新阅读警告 .

    模板模块

    - name: Configure sshd
      template:
        src: sshd_config.j2
        dest: /etc/ssh/sshd_config
        owner: root
        group: root
        mode: "0644"
        validate: '/usr/sbin/sshd -T -f %s'
      notify: restart sshd
    handlers:
    - name: restart sshd
      service: 
        name: sshd
        state: restarted
    

    多个发行版支持

    如果你不是懒得支持你所有的发行版,请看这个提示

    - name: configure ssh
      template: src={{ item }} dest={{ SSH_CONFIG }} backup=yes
      with_first_found:
        - "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.sshd_config.j2"
        - "{{ ansible_distribution }}.sshd_config.j2"
    

    https://ansible-tips-and-tricks.readthedocs.io/en/latest/modifying-files/modifying-files/

    (需要使用 first_found 查找更新为 loop

相关问题