# Pre-create list of volumes for docker\_container command?

**URL:** <https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155>\
**Category:** Ansible Project\
**Created:** [August 28, 2019, 8:08pm UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155 "2019-08-28T20:08:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koen\_De\_Groote](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Koen\_De\_Groote](https://forum.ansible.com/u/Koen_De_Groote)\
**Post date:** [August 28, 2019, 8:08pm UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155/1 "2019-08-28T20:08:10Z")

</div>

I’m looking for some help or guidance in trying to create a list of volumes to pass along to docker container, before that actual command.

So, the format for this is:

```auto
    volumes:
    - '{{ dir1 }}:{{ dir1 }}'
    - '/tmp:/tmp'

```

And so on…

But I’m looking at a case where the volumes are dynamic. And I’d like to put them in a variable before this step and then pass that variable along.

The alternative right now is a bunch of separate `docker_container` steps with a `when` condition, which is not very efficient.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 29, 2019, 4:22am UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155/2 "2019-08-29T04:22:56Z")

</div>

Hi

> I’m looking for some help or guidance in trying to create a list of volumes to pass along to docker container, before that actual command.
> 
> So, the format for this is:
> 
> ```auto
> volumes:
> - '{{ dir1 }}:{{ dir1 }}'
> - '/tmp:/tmp'
> 
> ```
> 
> And so on…
> 
> But I’m looking at a case where the volumes are dynamic. And I’d like to put them in a variable before this step and then pass that variable along.

Yes that is possible.  
But it’s not clear what the problem is, or what is the question you have with this?  
I.E. what exactly are you looking for?

---

<div class="post-metadata">

**Author:** ![Koen\_De\_Groote](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Koen\_De\_Groote](https://forum.ansible.com/u/Koen_De_Groote)\
**Post date:** [August 29, 2019, 8:19pm UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155/3 "2019-08-29T20:19:26Z")

</div>

The question is: how do you do it? I’m looking for an example.

Nothing I’ve found so far in the documentation says outright “this is how it’s done”.

I’m uncertain what kind of variable I need to produce, in what way, so that it is the correct one for this field to accept it.

This may be obvious to a lot of people, but not to me.

---

<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:** [August 29, 2019, 10:26pm UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155/4 "2019-08-29T22:26:58Z")

</div>

You have this

&nbsp;&nbsp;volumes:  
&nbsp;&nbsp;- '{{ dir1 }}:{{ dir1 }}'  
&nbsp;&nbsp;- '/tmp:/tmp'

The dashes indicates it's a list, and pretty sure if you check the documentation it says that volumes takes a list.  
So create a variable with a list in it and provide that variable to volumes.

Define a variable  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;myvar:  
&nbsp;&nbsp;&nbsp;&nbsp;- '{{ dir1 }}:{{ dir1 }}'  
&nbsp;&nbsp;&nbsp;&nbsp;- '/tmp:/tmp'

and then use it

&nbsp;&nbsp;volumes: '{{ myvar }}'

---

<div class="post-metadata">

**Author:** ![Koen\_De\_Groote](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Koen\_De\_Groote](https://forum.ansible.com/u/Koen_De_Groote)\
**Post date:** [September 9, 2019, 1:50pm UTC](https://forum.ansible.com/t/pre-create-list-of-volumes-for-docker-container-command/31155/5 "2019-09-09T13:50:26Z")

</div>

Apologies for the late reply.

What I’m looking for is more something for use inside a role, and also additive.  
So, if I already have a list, to be able to specificy a “when” condition and if the condition is met, add something to the list.
