I am getting additional characters added in the lineinfile
module run.
I am trying to add the string content of a file into another file location using the following example:
- name: Read file value
shell: |
cat path_location/filename.txt
register: filename_det
- name: Add string content of filename_det to new file location at point "content"
become_user: root
lineinfile:
path: "new_file_location/filename1.txt"
regexp: '^content.*$'
line: 'content: {{filename_det.stdout_lines}}'
Unfortunately, this is adding some additional unwanted characters at the start and end of the string [' ']
, square brackets and a comma. I just want the string to be displayed.
As pointed in the documentation, stdout_lines
is always a list.
You could use stdout
instead, so,
line: 'content: {{ filename_det.stdout }}'
Still, I wouldn't recommend this approch, as shell
and command
should be kept for cases when there is no purposed module for your use case.
A better approach, here, would be to use the slurp
module to get the content of your file:
- name: Register filename.txt content
ansible.builtin.slurp:
src: path_location/filename.txt
register: filename_det
- name: Add the content of filename.txt to filename1.txt
ansible.builtin.lineinfile:
path: new_file_location/filename1.txt
regexp: ^content.*$
line: "content: {{ filename_det.content | b64decode }}"
become_user: root