# Create a variable that contains other variable values with labels

**URL:** <https://forum.ansible.com/t/create-a-variable-that-contains-other-variable-values-with-labels/30250>\
**Category:** Ansible Project\
**Created:** [May 2, 2019, 5:00pm UTC](https://forum.ansible.com/t/create-a-variable-that-contains-other-variable-values-with-labels/30250 "2019-05-02T17:00:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark.tovey](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mark.tovey](https://forum.ansible.com/u/mark.tovey)\
**Post date:** [May 2, 2019, 5:00pm UTC](https://forum.ansible.com/t/create-a-variable-that-contains-other-variable-values-with-labels/30250/1 "2019-05-02T17:00:44Z")

</div>

I have a list:

> list1

> - value1

> - value2

and I want to use set\_fact create a new variable that contains these variables with a label in front of them, so that this debug task:

> - debug:

> > msg: “{{var1}}”

would result in output that looks like this:

> label value1

> label value2

This gives me the expected results:

> - set\_fact:

> > var1: |+

> > > “{{'label ’ ~ list1[0]}}”

> > > “{{'label ’ ~ list1[1]}}”

But I need more flexibility than that. I need to be able to add or remove values to or from the list and not have to modify the set\_fact task each time to accommodate the modification. I tried using the join filter:

> - set\_fact:

> > var1:

> > > “{{‘label ’ ~ list1 | join(’\n’)}}”

And that got me:

> label value1

> value2

Close, but not quite. So what is the best method of achieving what I want?

---

<div class="post-metadata">

**Author:** ![Matthew\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Matthew\_Davis](https://forum.ansible.com/u/Matthew_Davis)\
**Post date:** [May 8, 2019, 12:26am UTC](https://forum.ansible.com/t/create-a-variable-that-contains-other-variable-values-with-labels/30250/2 "2019-05-08T00:26:28Z")

</div>

Here is a solution using regex and map. It’s not the most computationally efficient, but it works.

`

---

<div class="post-metadata">

**Author:** ![mark.tovey](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mark.tovey](https://forum.ansible.com/u/mark.tovey)\
**Post date:** [May 8, 2019, 2:50pm UTC](https://forum.ansible.com/t/create-a-variable-that-contains-other-variable-values-with-labels/30250/3 "2019-05-08T14:50:30Z")

</div>

Whatever works is what counts.

Thanks,

-Mark
