# Ansible readfile and concatenate a variable to each line

**URL:** <https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293>\
**Category:** Ansible Project\
**Created:** [December 7, 2020, 11:28am UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293 "2020-12-07T11:28:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_Chander](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Manoj\_Chander](https://forum.ansible.com/u/Manoj_Chander)\
**Post date:** [December 7, 2020, 11:28am UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/1 "2020-12-07T11:28:34Z")

</div>

Hi I have a file with list of filenames .I want to read each line and add a variable {{version}} to the end and pass it to another task which will download the artifact

**MY\_File**

cat file/branches.txt mhr- mtr- tsr

I want to get each line add {{version}} to it

mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT

I couldn’t get the file names and append them. below is a failed attempt by me.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 7, 2020, 2:20pm UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/2 "2020-12-07T14:20:14Z")

</div>

The task below does the job

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ lookup('file',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'files/branches.txt').split()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([version])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join(' ') }}"

gives

&nbsp;&nbsp;msg: mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 7, 2020, 2:34pm UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/3 "2020-12-07T14:34:56Z")

</div>

In addition to this, you've said "each line add {{version}} to it".  
If there are more lines, e.g.

&nbsp;&nbsp;cat files/branches.txt  
&nbsp;&nbsp;mhr- mtr- tsr-  
&nbsp;&nbsp;xhr- xtr- xsr-  
&nbsp;&nbsp;yhr- ytr- ysr-

the task below iterates the lines

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.split()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([version])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join(' ') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('file',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'files/branches.txt').splitlines() }}"

gives

&nbsp;&nbsp;msg: mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT  
&nbsp;&nbsp;msg: xhr-1.1-SNAPSHOT xtr-1.1-SNAPSHOT xsr-1.1-SNAPSHOT  
&nbsp;&nbsp;msg: yhr-1.1-SNAPSHOT ytr-1.1-SNAPSHOT ysr-1.1-SNAPSHOT

---

<div class="post-metadata">

**Author:** ![Manoj\_Chander](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Manoj\_Chander](https://forum.ansible.com/u/Manoj_Chander)\
**Post date:** [December 8, 2020, 2:49am UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/4 "2020-12-08T02:49:36Z")

</div>

Hi Vladimir Botka ,  
thanks you so much for taking time to help , above works at debug level but I have to save it to a variable and . Loop it and download each file.

The file is in my host and Download happens in remote host .i tried the below task im still  
not able to achieve desired result.

- include\_vars:

file: releasevars.yml  
when: env == “dev”

- slurp:  
src: ‘{{item}}’  
register: input\_file\_content  
loop:

- “{{file}}”  
delegate\_to: localhost

- debug:  
msg: “{{ item[‘content’] | b64decode }}”  
loop: ‘{{ input\_file\_content[“results”] }}’

- set\_fact:  
files\_version: “{{ (input\_file\_content[‘content’] | b64decode).split(‘\n’) | map(‘regex\_replace’, ‘^(.+)$’, ‘\g\<1\>’ + version ) }}”

- name: Download artifact  
maven\_artifact:  
group\_id: com.devops.services  
artifact\_id: “{{ item[‘content’]| b64decode }}”  
version: 6.0.0-39-SNAPSHOT  
repository\_url: [https://nexus.com/repository/maven-snapshots](https://nexus.com/repository/maven-snapshots)  
username: user  
password: @123  
verify\_checksum: never  
keep\_name : yes  
dest: /home/test/slurp/  
loop: ‘{{ files\_version[“results”] }}’

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 8, 2020, 7:56am UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/5 "2020-12-08T07:56:28Z")

</div>

Try this

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files: "{{ lookup('file','releasevars.yml').split()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([version])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"

&nbsp;&nbsp;&nbsp;&nbsp;- slurp:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: input\_file\_content  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ files }}"

---

<div class="post-metadata">

**Author:** ![Manoj\_Chander](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Manoj\_Chander](https://forum.ansible.com/u/Manoj_Chander)\
**Post date:** [December 8, 2020, 2:10pm UTC](https://forum.ansible.com/t/ansible-readfile-and-concatenate-a-variable-to-each-line/34293/6 "2020-12-08T14:10:56Z")

</div>

Thanks for your help Vladimir Botka . I was able to sucessfully append and download my file in a loop.
