我有一个包含字符串的文件:
MYAPP.db.username.DEV=MYUSERNAME
在哪里:
MYAPP mean name of applications
DEV means environment
MYUSERNAME means name of user for connection to db
我需要根据某些脚本中的变量替换这些变量。我正在使用这个:
- name: Replace line
shell: sed -i "/{{ name_of_app }}.db.username.{{ name_of_environment }}/c\\{{ name_of_app }}.db.username.{{ name_of_environment }}={{ name_of_user_to_db }}" /path/to/my/apps/{{ name_of_app }}/config/{{ name_of_app }}.config
它工作正常,但我在 ansible 的输出中看到警告
[WARNING]: Consider using the replace, lineinfile or template module rather
than running sed. If you need to use command because replace, lineinfile or
template is insufficient you can add warn=False to this command task or set
command_warnings=False in ansible.cfg to get rid of this message.
changed:
我试过
- name: Replace line via replace method
replace:
dest: "/path/to/my/apps/{{ name_of_app }}/config/{{ name_of_app }}.config"
regexp: "{{ name_of_app }}.db.username.{{ name_of_environment }}"
replace: "{{ name_of_app }}.db.username.{{ name_of_environment }}={{ name_of_user_to_db }}"
和这个
- name: Replace line via lineinfile method
lineinfile:
dest: "/path/to/my/apps/{{ name_of_app }}/config/{{ name_of_app }}.config"
regexp: "{{ name_of_app }}.db.username.{{ name_of_environment }}"
line: "{{ name_of_app }}.db.username.{{ name_of_environment }}={{ name_of_user_to_db }}"
backrefs: yes
每次,我都失败了:
FAILED! => {"changed": false, "msg": "Unsupported parameters for (replace) module: when Supported parameters include: after, attributes, backup, before, content, delimiter, directory_mode, encoding, follow, force, group, mode, owner, path, regexp, remote_src, replace, selevel, serole, setype, seuser, src, unsafe_writes, validate"}
你能帮助我吗?我的剧本哪里有错误?
答案1
这replace
和lineinfile
使用path
参数来标记
要修改的文件。
lineinfile
模块用于确保文件中存在某一行,或者使用反向引用的正则表达式替换现有行。
使用
如果你想更改多个类似的行,可以使用 replace 模块
我相信该dest
参数用于模块创建新文件,如template
或copy
。
只需替换dest
,path
提供的两个示例都应该按预期工作。