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.
vbotka
(Vladimir Botka)
December 7, 2020, 2:20pm
2
The task below does the job
- debug:
msg: "{{ lookup('file',
'files/branches.txt').split()|
product([version])|
map('join')|
join(' ') }}"
gives
msg: mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT
vbotka
(Vladimir Botka)
December 7, 2020, 2:34pm
3
In addition to this, you've said "each line add {{version}} to it".
If there are more lines, e.g.
cat files/branches.txt
mhr- mtr- tsr-
xhr- xtr- xsr-
yhr- ytr- ysr-
the task below iterates the lines
- debug:
msg: "{{ item.split()|
product([version])|
map('join')|
join(' ') }}"
loop: "{{ lookup('file',
'files/branches.txt').splitlines() }}"
gives
msg: mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT
msg: xhr-1.1-SNAPSHOT xtr-1.1-SNAPSHOT xsr-1.1-SNAPSHOT
msg: yhr-1.1-SNAPSHOT ytr-1.1-SNAPSHOT ysr-1.1-SNAPSHOT
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.
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
username: user
password: @123
verify_checksum: never
keep_name : yes
dest: /home/test/slurp/
loop: ‘{{ files_version[“results”] }}’
vbotka
(Vladimir Botka)
December 8, 2020, 7:56am
5
Try this
- set_fact:
files: "{{ lookup('file','releasevars.yml').split()|
product([version])|
map('join')|
list }}"
- slurp:
src: "{{ item }}"
register: input_file_content
loop: "{{ files }}"
Thanks for your help Vladimir Botka . I was able to sucessfully append and download my file in a loop.