Ansible取消注释文件中的行

Zum*_*rio 5 linux ansible

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

- name: Uncomment line from /etc/ssh/sshd_config
    lineinfile:
      dest: /etc/ssh/sshd_config
      regexp: '^#AuthorizedKeysFile'
      line: 'AuthorizedKeysFile    .ssh/authorized_keys'
Run Code Online (Sandbox Code Playgroud)

但是,此配置仅在行开始时有效#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'
Run Code Online (Sandbox Code Playgroud)

Wil*_*ich 8

如果您在'#'字符后需要零个或多个空格,则满足以下条件:

- name: Uncomment line from /etc/ssh/sshd_config
    lineinfile:
      dest: /etc/ssh/sshd_config
      regexp: '^#\s*AuthorizedKeysFile.*$'
      line: 'AuthorizedKeysFile    .ssh/authorized_keys'
Run Code Online (Sandbox Code Playgroud)

对原始代码的修改是在regex中添加\s*.*$

说明:

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

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

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

$ -匹配行尾


KCD*_*KCD 8

我应该警告@techraf 的观点,即 99% 的情况下,配置文件的完整模板几乎总是更好。

我所做的lineinfile包括由其他进程管理的奇怪而美妙的配置文件,或者我尚未完全理解的配置的惰性,并且可能因发行版/版本而异,我不想维护所有变体......然而。

继续学习更多 Ansible...它很棒,因为您可以不断迭代它,从原始 bash shell 命令一直到最佳实践。

文件行模块

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

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
Run Code Online (Sandbox Code Playgroud)
  • validate如果更改无效,请勿更改
  • notify/handlers最后只重启一次的正确方法
  • with_items(很快就会成为loop)如果您有多个设置
  • ^#?该设置可能会被注释掉 - 请参阅其他答案
  • \s*{{ item.key }}\s不会匹配其他设置(即SettingA无法匹配NotSettingASettingAThisIsNot

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

模板模块

- 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
Run Code Online (Sandbox Code Playgroud)

多发行版支持

如果您不懒于支持所有发行版,请参阅此提示

- 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"
Run Code Online (Sandbox Code Playgroud)

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

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


tec*_*raf 7

首先,您使用的是错误的语言.使用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'
Run Code Online (Sandbox Code Playgroud)

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

AuthorizedKeysFile .ssh/authorized_keys

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

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


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

  • 模板最适合完整文件。在修改文件的某些部分时, lineinfile 是完全可以接受的,实际上这就是它的目的。 (2认同)
  • `# AuthorizedKeysFile - 用于显示正则表达式替换合法评论(这个)的示例` (2认同)