# Pull of List in a Variable File as Extra-Vars

**URL:** <https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943>\
**Category:** Ansible Project\
**Created:** [August 3, 2022, 9:02pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943 "2022-08-03T21:02:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 3, 2022, 9:02pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/1 "2022-08-03T21:02:16Z")

</div>

Hello Team,

Can I ask some help on how to pull items as Extra-Vars?  
So, here is the scenario.

**dirname.yml**  
dir\_list:

- hello
- goodbye
- take-care

**patch\_file.yml**  
patch\_lib:

- lib\_1
- lib\_2

**general\_var.yml**  
main\_dir: “{{ app\_name }}-{{ ansible\_hostname }}-{{ ver }}”

**main\_playbook.yml**

- name: Test Playbook Script for Debug  
hosts: all

vars\_files:

- general\_var.yml

- dirname.yml

- patch\_file.yml

tasks:

- name: Copy | All Mule v392 Patch to LIB-User Directory for “{{Application\_Runtime | upper}}” Runtime  
copy:  
src: “/user/lib/{{ item .0 }}”  
dest: “/home/{{ item.1 }}/lib/user/”  
mode: 0644  
with\_nested:
- “{{ patch\_lib }}”
- “{{ main\_dir }}”

And here’s my playbook script that doesn’t work.  
**ansible-playbook main\_playbook.yml --limit [awssandbox001.us.com](http://awssandbox001.us.com) -e “app\_name= dir\_list”**

The idea, it should be able to copy the list of ' **patch\_lib**" into each “ **main\_dir** ”.

dest:  
/home/hello-awssandbox001.us.com-123/lib/user  
/home/goodbye-awssandbox001.us.com-123/lib/user

/home/take-care-awssandbox001.us.com-123/lib/user

Look forward for anybody that can able to help.

Thanks… WP

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 4, 2022, 1:11pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/2 "2022-08-04T13:11:49Z")

</div>

Are you getting an error? Can we see it?

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 4, 2022, 2:09pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/3 "2022-08-04T14:09:44Z")

</div>

Yeah, I forgot to mention the result.  
Basically, there was no error but instead it creates a new directory from the value of ‘app\_name’ rather than the item list of the dir\_list.

**Result:** /home/ **dir\_list** -awssandbox001.us.com-123/lib/user  
**Expected:**

- /home/ **hello** -awssandbox001.us.com-123/lib/user

- /home/ **goodbye** -awssandbox001.us.com-123/lib/user

- /home/ **take-care** -awssandbox001.us.com-123/lib/user

Appreciated.

Thanks,  
WP

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 4, 2022, 3:53pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/4 "2022-08-04T15:53:41Z")

</div>

See if this does what you want …

- name: Test Playbook Script for Debug  
hosts: localhost

vars\_files:

- general\_var.yml
- dirname.yml
- patch\_file.yml

tasks:

- name: Copy | All Mule v392 Patch to LIB-User Directory for “{{Application\_Runtime | upper}}” Runtime  
copy:  
src: “/user/lib/{{ item .0 }}”  
**dest: “/home/{{ item.1 }}-{{ ansible\_hostname }}-{{ ver }}/lib/user/”**

mode: 0644  
with\_nested:

- “{{ patch\_lib }}”  
**- “{{ dir\_list }}”**

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 4, 2022, 4:52pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/5 "2022-08-04T16:52:07Z")

</div>

Thank You, Walter.  
It does work as expected however, I do have couple of variables inside the dirname.yml and different directories inside of the home path.  
The idea, I don’t want to run the copy module (etc) in one execution but instead putting them in a group or set. Another purpose, I don’t want them to restart them at the same time wherein it will be on schedule on when it will happen.

**dirname.yml**  
dir\_list:

- hello
- goodbye
- take-care

**another\_list:**

- say
- tell
- make

**exam\_list:**

- etc
- other

**Home Directory**

- /home/hello-awssandbox001.us.com-123

- /home/goodbye-awssandbox001.us.com-123

- /home/takecare-awssandbox001.us.com-123

- /home/say-awssandbox001.us.com-123

- /home/tell-awssandbox001.us.com-123

- /home/make-awssandbox001.us.com-123

- /home/etc-awssandbox001.us.com-123

- /home/other-awssandbox001.us.com-123

Appreciated and thanks… WP

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 4, 2022, 5:06pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/6 "2022-08-04T17:06:16Z")

</div>

You can group your inventory and provide the group name to the execution and it will only run on that group.

$ ansible-playbook --inventory _my\_inventory\_file_ --limit _group\_name_ … rest of your command …

The “ **hosts: all** ” in your playbook will be limited to the group from your inventory that you specify on the command line.

Walter

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 4, 2022, 5:35pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/7 "2022-08-04T17:35:37Z")

</div>

Hello,

I have tried something like this one… Btw, I’ve been using group\_vars already for my other variables.

**dir tree:**

- inventory

- inv\_test\_01 (file)

- group\_vars (dir)

- awssbx (file)

**inv\_test\_01 (file)**  
[awssbx]  
[awssandbox001.us.com](http://awssandbox001.us.com)

[awsdev]

awsdev001  
awsdev002

**awssbx (file)**

dir\_list:

- hello
- goodbye
- take-care

**Command:**

_$ ansible-playbook --inventory **inv\_test\_01** --limit **awssbx** … rest of the command …_  
**-e “app\_name= dir\_list”**

or even without the -inventory also doesn’t work. It is still creating folder of the ‘ **dir\_list** ’.

Thank you and appreciated… WP

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 4, 2022, 6:11pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/8 "2022-08-04T18:11:35Z")

</div>

Are you thinking there is different “dir\_list” for each group?

–limit awssxb would also need an awssbx vars file that has one list of values for dir\_list  
–limt awssbx2 would also need an awssbx2 vars file that has a different list of values for dir\_list?

In your playbook you can reference vars file by variable names, so you could use the group name

vars\_files:

- general\_var.yml

**- “{{ ansible\_limit }}.yml”**

- patch\_file.yml

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 4, 2022, 6:14pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/9 "2022-08-04T18:14:51Z")

</div>

[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

Look for ansible\_limit …

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 4, 2022, 9:17pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/10 "2022-08-04T21:17:39Z")

</div>

Thank you Walter…  
Honestly, I have no idea and trying to understand and learning it now.  
I’ll get back once I have tried again.

Thank you and appreciated… WP

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 5, 2022, 12:35am UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/11 "2022-08-05T00:35:13Z")

</div>

> \> \*general\_var.yml\*  
> \> main\_dir: "{{ app\_name }}-{{ ansible\_hostname }}-{{ ver }}"

Use \*vars\* lookup plugin to indirectly reference the value of the  
variable stored in \*app\_name\*. Then use filter \*product\* to create  
the combinations. For example, given

\* the extra variable '-e app\_name=dir\_list'  
\* the variable \*dir\_list\*  
&nbsp;&nbsp;\> cat dirname.yml  
&nbsp;&nbsp;dir\_list:  
&nbsp;&nbsp;&nbsp;&nbsp;- hello  
&nbsp;&nbsp;&nbsp;&nbsp;- goodbye  
&nbsp;&nbsp;&nbsp;&nbsp;- take-care  
\* the inventory  
&nbsp;&nbsp;\> cat hosts  
&nbsp;&nbsp;host01 ansible\_hostname=01.us.com  
\* the variable 'ver=123'

The variable \*main\_dir\*

&nbsp;&nbsp;\> cat general\_var.yml  
&nbsp;&nbsp;main\_dir: "{{ lookup('vars', app\_name)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([ansible\_hostname])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([ver])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('flatten')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join','-')|list }}"

gives the list you want (simplified)

&nbsp;&nbsp;main\_dir:  
&nbsp;&nbsp;&nbsp;&nbsp;- hello-01.us.com-123  
&nbsp;&nbsp;&nbsp;&nbsp;- goodbye-01.us.com-123  
&nbsp;&nbsp;&nbsp;&nbsp;- take-care-01.us.com-123

Test the iteration

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "copy {{ item.0 }} to {{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ patch\_lib }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ main\_dir }}"

gives (abridged)

&nbsp;&nbsp;msg: copy lib\_1 to hello-01.us.com-123  
&nbsp;&nbsp;msg: copy lib\_1 to goodbye-01.us.com-123  
&nbsp;&nbsp;msg: copy lib\_1 to take-care-01.us.com-123  
&nbsp;&nbsp;msg: copy lib\_2 to hello-01.us.com-123  
&nbsp;&nbsp;msg: copy lib\_2 to goodbye-01.us.com-123  
&nbsp;&nbsp;msg: copy lib\_2 to take-care-01.us.com-123

Fit the details to your needs.

---

<div class="post-metadata">

**Author:** ![Poseidon\_Olympian](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Poseidon\_Olympian](https://forum.ansible.com/u/Poseidon_Olympian)\
**Post date:** [August 5, 2022, 4:58pm UTC](https://forum.ansible.com/t/pull-of-list-in-a-variable-file-as-extra-vars/36943/12 "2022-08-05T16:58:58Z")

</div>

Hello Vladimir,

It does works successfully. Many thanks and highly appreciated.

This thread is RESOLVED.

Thanks… WP
