# Concatenate variable and string in looping with\_items

**URL:** <https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112>\
**Category:** Ansible Project\
**Created:** [January 31, 2020, 5:17pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112 "2020-01-31T17:17:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [January 31, 2020, 5:17pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/1 "2020-01-31T17:17:00Z")

</div>

**Is it possible to concatenate like below?**

- name: Retaining latest backup directory  
shell: find $(pwd)/_backup_ -type d -prune -exec ls -d {} ; |tail -1  
register: import\_back\_up\_dir

- name: Import dashboard  
grafana\_dashboard:  
state: present

message: Updated by ansible  
overwrite: no  
path: “{{ item }}”  
with\_items:

- “{{ import\_back\_up\_dir }} + SonarQube\_Dashboard.json” \>\>\>\>\>\> I am trying to concatenate like this however this is not working - could some one please suggest how can we achieve this concatenation.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 31, 2020, 5:37pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/2 "2020-01-31T17:37:03Z")

</div>

> \*\_Is it possible to concatenate like below?\_\*
> 
> ```
> \- name: Retaining latest backup directory 
> shell: find $\(pwd\)/\*backup\* \-type d \-prune \-exec ls \-d \{\} \\; |tail \-1
> register: import\_back\_up\_dir
> 
> \- name: Import dashboard
> grafana\_dashboard:
> state: present
> message: Updated by ansible
> overwrite: no
> path: &quot;\{\{ item \}\}&quot;
> with\_items:
> \- &quot;\{\{ import\_back\_up\_dir \}\} \+ SonarQube\_Dashboard\.json&quot; \_&gt;&gt;&gt;&gt;&gt;&gt; I am trying to concatenate like this however
> 
> ```
> 
> this is not working - could some one please suggest how can we achieve this concatenation.

Maybe you give us the objective of your task first ...

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [January 31, 2020, 6:04pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/3 "2020-01-31T18:04:45Z")

</div>

Thanks Stefan for looking into this. actually I want to import some grafana dashboards and I need to map this import\_back\_up\_dir (which I cannot hardcode) to appropriate json files. so I want to find a way to concatenate these.

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [January 31, 2020, 6:26pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/4 "2020-01-31T18:26:45Z")

</div>

Replied

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [January 31, 2020, 6:30pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/5 "2020-01-31T18:30:23Z")

</div>

Thanks Stefan for looking into this. actually I want to import some grafana dashboards and I need to map this import\_back\_up\_dir (which I cannot hardcode) to appropriate json files. so I want to find a way to concatenate these.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 31, 2020, 7:27pm UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/6 "2020-01-31T19:27:42Z")

</div>

Concatenate need to be done in inside of {{ }} and use tilde

"{{ import\_back\_up\_dir ~ 'SonarQube\_Dashboard.json' }}"

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [February 1, 2020, 5:54am UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/7 "2020-02-01T05:54:04Z")

</div>

HI kai,

thanks for the response. however I am trying to use inside looping with\_items it isn’t working.

“msg”: "error : Can’t load json file [Errno 2] No such file or directory: '{\‘stderr\_lines\’:

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [February 1, 2020, 5:55am UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/8 "2020-02-01T05:55:44Z")

</div>

trying like this

with\_items:

- “{{ dashboards\_imported\_from\_latest\_backup ~ ‘/Artifactory\_Monitoring.json’ }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 1, 2020, 8:12am UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/9 "2020-02-01T08:12:27Z")

</div>

This will only be one item so you don't need to use with\_items.  
Anyway, since you only need the text it should be like this

&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ dashboards\_imported\_from\_latest\_backup}}/Artifactory\_Monitoring.json"

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [February 3, 2020, 4:52am UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/10 "2020-02-03T04:52:41Z")

</div>

Thanks Kai. No actually this is not going to be one item only as it should be done for almost 7-8 json files. so i think the below trick you shown I am going to try that right now and see if that works forme. Thanks again , cheers!

---

<div class="post-metadata">

**Author:** ![Vishal\_Bobade](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vishal\_Bobade](https://forum.ansible.com/u/Vishal_Bobade)\
**Post date:** [February 3, 2020, 5:24am UTC](https://forum.ansible.com/t/concatenate-variable-and-string-in-looping-with-items/32112/11 "2020-02-03T05:24:52Z")

</div>

I tried the above what Kai suggested however that is not working and unable to get the particular json for me from the specific backup dir. However I could find a way to get this done as shown below - this way we can get the output from the variable line by line.

with\_items:

- “{{ dashboards\_imported\_from\_latest\_backup.stdout\_lines[2] }}”
- “{{ dashboards\_imported\_from\_latest\_backup.stdout\_lines[3] }}”
- “{{ dashboards\_imported\_from\_latest\_backup.stdout\_lines[4] }}”
