2

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 ?

Piduna
  • 501
  • 3
  • 10
  • 23

1 Answers1

4

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.

Henrik Pingel
  • 8,676
  • 2
  • 24
  • 38
  • I think this is the right answer. The official doco indicates `dest` is an alias for `path` in both modules, but I am not surprised it is not working in this case. – 0xSheepdog Nov 14 '19 at 01:06
  • Ansible changed this behaviour with version `2.3` (released 2017) This answer might be out of date by now. – Henrik Pingel Nov 14 '19 at 06:38