Scripting 将字符串替换为小写或大写
我正在写一个剧本来替换三个文件中的一个字符串。该字符串可以用小写或大写书写。这是我的密码:Scripting 将字符串替换为小写或大写,scripting,ansible,yaml,Scripting,Ansible,Yaml,我正在写一个剧本来替换三个文件中的一个字符串。该字符串可以用小写或大写书写。这是我的密码: --- - name: "Modif string" hosts: myhosts tasks: - name: "Replace line" replace: path: ~/Documents/{{ item }} regexp: 'test' replace: 'new' with_items:
---
- name: "Modif string"
hosts: myhosts
tasks:
- name: "Replace line"
replace:
path: ~/Documents/{{ item }}
regexp: 'test'
replace: 'new'
with_items:
- 'file'
- 'file1'
- 'file2'
我如何使它工作,使字符串“test”修改为小写或大写?如果字符串中随机包含大写字母或小写字母,又该怎么办
谢谢大家。试试下面的方法
- name: "Replace line"
replace:
path: ~/Documents/{{ item }}
regexp: '(?i)test'
replace: 'new'
我终于写了剧本。工作起来很有魅力
- name: "Other test"
hosts: raspi
vars:
text_to_replace:
- {regexp: '(?i)(test)', line: 'new'}
- {regexp: '10', line: '20'}
my_files:
- {file: 'file'}
- {file: 'file1'}
- {file: 'file2'}
tasks:
- name: "Replace"
replace:
path: ~/test/{{item.1.file}}
regexp: "{{item.0.regexp}}"
replace: "{{item.0.line}}"
with_nested:
- "{{text_to_replace}}"
- "{{my_files}}"
如果我想修改这些文件中的另一个字符串怎么办?例如,我想替换一个IP地址加上字符串“test”。它只会发生一次吗?如果是这样,您可以使用lineinfle模块。我想在多个文件中测试这两个字符串,并在它们出现时对其进行修改。请提供一个示例。我希望此答案能为您提供一些见解