Ansible readfile and concatenate a variable to each line

28 views
Skip to first unread message

Manoj Chander

unread,
Dec 7, 2020, 6:28:35 AM12/7/20
to Ansible Project
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.

---
- name: get file names
  shell: echo {{item}}{{version}}
  register: result
  with_lines: cat files/branches.txt

- include_tasks: 2main.yml
  with_items: "{{result.results}}"
  

Vladimir Botka

unread,
Dec 7, 2020, 9:20:43 AM12/7/20
to Manoj Chander, ansible...@googlegroups.com
On Mon, 7 Dec 2020 03:28:34 -0800 (PST)
Manoj Chander <cmanoj....@gmail.com> wrote:

> ... a file with list of filenames
> ... read each line
> ... add a variable {{ version }} to the end
> ... pass it to another task
> ...
> cat file/branches.txt mhr- mtr- tsr
>
> ... each line add {{version}} to it
> mhr-1.1-SNAPSHOT mtr-1.1-SNAPSHOT tsr-1.1-SNAPSHOT

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

--
Vladimir Botka

Vladimir Botka

unread,
Dec 7, 2020, 9:35:24 AM12/7/20
to Manoj Chander, ansible...@googlegroups.com
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

--
Vladimir Botka

Manoj Chander

unread,
Dec 7, 2020, 9:50:13 PM12/7/20
to Vladimir Botka, ansible...@googlegroups.com
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
      username: user
      password: @123
      verify_checksum: never
      keep_name : yes
      dest: /home/test/slurp/
  loop: '{{ files_version["results"] }}'



Vladimir Botka

unread,
Dec 8, 2020, 2:56:54 AM12/8/20
to Manoj Chander, ansible...@googlegroups.com
On Tue, 8 Dec 2020 08:19:36 +0530
Manoj Chander <cmanoj....@gmail.com> wrote:

> ... save it to a variable ... loop it and download each file.

Try this

- set_fact:
files: "{{ lookup('file','releasevars.yml').split()|
product([version])|
map('join')|
list }}"

- slurp:
src: "{{ item }}"
register: input_file_content
loop: "{{ files }}"

--
Vladimir Botka

Manoj Chander

unread,
Dec 8, 2020, 9:10:56 AM12/8/20
to Ansible Project
Thanks for your help  Vladimir Botka . I was able to sucessfully append and download my file in a loop. 
Reply all
Reply to author
Forward
0 new messages