大网数据广告活动
服务器运维问答

How to use Ansible modules replace or inline instead shell command with SED

· 52CE 我爱测

i have file with string: MYAPP.db.username.DEV=MYUSERNAME Where: MYAPP mean name of applications DEV means environment MYUSERNAME means name of user for connection to db I need to replace these variables according to variables, t…

问题原文

原帖链接 · Piduna · CC BY-SA 4.0

i have file with string:

MYAPP.db.username.DEV=MYUSERNAME

Where:

MYAPP mean name of applications
DEV means environment
MYUSERNAME means name of user for connection to db

I need to replace these variables according to variables, that i have in some script. I am using this:

- 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

It works fine, but i see warnings in output of 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:

I have tried this

- 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 }}"

and this

- 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

each time, i have result with failed:

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"}

Can you help me, where i have error in my playbook ?

已采纳解答

原帖链接 · Henrik Pingel · CC BY-SA 4.0

The replace and the lineinfile use the path parameter to mark

The file to modify. lineinfile module is used to

ensure[s] a particular line is in a file, or [to] replace an existing line using a back-referenced regular expression.

Use

the replace module if you want to change multiple, similar lines

I believe the dest parameter is used for modules creating new files like template or copy.

Just replace dest with path and both provided examples should work as expected.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。