Ansible 2.3:用于替换文件中多行的单个正则表达式
Ansible 2.3: single regular expression for replacing multi lines in a file
正在尝试找出正则表达式来替换文件中的以下多行
我要更新的目标文件
# enable the encryption , to be used .
ssh_encryption_options:
enabled: false
optional: false
algorithm: tls
我的任务定义
- replace:
path: /etc/config/
regexp: '^(ssh_encryption_options:
enabled: false)$'
replace: '^(ssh_encryption_options:
enabled: true)$'
become: true
文件中的预期输出
ssh_encryption_options:
enabled: true
optional: false
algorithm: tls
但是 ansible 无法在该文件中找到要替换的多行,在 regex.I 中给出,我确信有人之前已经尝试过这种情况。
将/etc/config/
替换为您要更改的文件的路径;
注意空格(显式添加或使用\s
进行匹配);
使用\n
匹配一个换行符;
使用捕获组来简化替换值。
- replace:
path: /etc/config/_file_
regexp: '^( ssh_encryption_options:\n\s*enabled: )false$'
replace: 'true'
become: true
正在尝试找出正则表达式来替换文件中的以下多行
我要更新的目标文件
# enable the encryption , to be used .
ssh_encryption_options:
enabled: false
optional: false
algorithm: tls
我的任务定义
- replace:
path: /etc/config/
regexp: '^(ssh_encryption_options:
enabled: false)$'
replace: '^(ssh_encryption_options:
enabled: true)$'
become: true
文件中的预期输出
ssh_encryption_options:
enabled: true
optional: false
algorithm: tls
但是 ansible 无法在该文件中找到要替换的多行,在 regex.I 中给出,我确信有人之前已经尝试过这种情况。
将
/etc/config/
替换为您要更改的文件的路径;注意空格(显式添加或使用
\s
进行匹配);使用
\n
匹配一个换行符;使用捕获组来简化替换值。
- replace:
path: /etc/config/_file_
regexp: '^( ssh_encryption_options:\n\s*enabled: )false$'
replace: 'true'
become: true