# can we not use 2 var\_files in a playbook

**URL:** <https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011>\
**Category:** Ansible Project\
**Created:** [May 19, 2023, 5:21am UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011 "2023-05-19T05:21:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [May 19, 2023, 5:21am UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/1 "2023-05-19T05:21:36Z")

</div>

I am getting error saying I am using 2 var\_files.  
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes:

I also tried using include\_vars which gave me same error, the values in these 2 files need to be replace at diff locations at play book  
some one please suggest what is the best way for this

name: post call to get servergroup  
hosts: localhost  
become: true  
gather\_facts: no  
tasks:

- name: set the facts per host  
set\_fact:  
access\_token: “{{ hostvars.localhost.output.stdout }}” # this is the output of the above play
- name: Make a post call to get server details  
shell: |  
cd /var/output/  
curl --location --request GET “{{ serverlist }}” --header 'Authorization: Bearer “{{ access\_token }}” \> /var/output/“{{ item }}”.json  
jq -r ‘.|.serverData.servers’ “{{ item }}”.json \> /home/deploy-user/ansible/group\_vars/“{{ item }}”.yml

# item and server list from one file and

register: data

- debug: msg=“data , {{ repo }}” # this need to replaced from one var file  
loop:
- dev
- uat
- stg2

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 19, 2023, 7:49am UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/2 "2023-05-19T07:49:48Z")

</div>

> I am getting error saying I am using 2 var\_files.

What is that error exactly?

> ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes:

This is another error then?

> I also tried using include\_vars which gave me same error, the values in these 2 files need to be replace at diff locations at play book  
> some one please suggest what is the best way for this  
> name: post call to get servergroup  
> &nbsp;&nbsp;hosts: localhost  
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;gather\_facts: no  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: set the facts per host  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;access\_token: "{{ hostvars.localhost.output.stdout }}" # this is the output of the above play  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Make a post call to get server details  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cd /var/output/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl --location --request GET "{{ serverlist }}" \--header 'Authorization: Bearer "{{ access\_token }}" \> /var/output/"{{ item }}".json  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jq -r '.|.serverData.servers' "{{ item }}".json \> /home/deploy-user/ansible/group\_vars/"{{ item }}".yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# item and server list from one file and

Instead of having ansible execute several shell commands piped  
together, you should use native modules, like  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html),  
register the result, and then operate/make decisions based on that.

> register: data  
> &nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="data , {{ repo }}" # this need to replaced from one var file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- dev  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- uat  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- stg2

"replaced from" is unclear - you usually replace something with  
something, or by something.  
Rather than guessing what you might mean, please try to be more clear.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 19, 2023, 10:59am UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/3 "2023-05-19T10:59:08Z")

</div>

Your problem is overquoting. The in-line shell script is already in a kind of quotes, so you don’t have to quote again your jinja expressions’ mustaches. All those double-quotes remain in the fully templated shell script.

Or it might be that the opening single quote of your --header value is never closed.

Or both of those things.

---

<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:** [May 19, 2023, 12:07pm UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/4 "2023-05-19T12:07:27Z")

</div>

Your curl command places the output in /var/output/{{ item }}.json.  
Your jq command reads the output from {{ item }}.json which is not /var/output/{{ item }}.json.

Walter

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [May 19, 2023, 7:19pm UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/5 "2023-05-19T19:19:16Z")

</div>

thank you all for your findings and recommendations

In the same playbook I would like to explain one more scenario , I am using set\_facts to use above playbook output as input in the playbook, and I have variables.yml

It has to go through loop and get the server details from each environment, will my below playbook work  
my variables.yml file

# Environment and os as input to pull all servers out of this

serverlist: “[https://test-dev.test.com/testapi/test/server?environment={{](https://test-dev.test.com/testapi/test/server?environment=%7B%7B) item }}&os=linux” +#

# item should be replaced with the item from the loop and pass it to below playbook

vars\_files:

- “variables.yml”  
tasks:
- name: set the facts per host  
set\_fact:  
access\_token: “{{ hostvars.localhost.output.stdout }}” # this is the output of the above play
- name: Make a post call to get server details  
shell: |  
cd /var/output/  
curl --location --request GET {{ serverlist }} --header Authorization: Bearer {{ access\_token }} \> /var/output/servers1.json

jq -r ‘.|.serverData.servers’ servers1.json \> /home/deploy-user/ansible/group\_vars/servers1.yml  
register: data

- debug: msg=“data , {{ serverlist }}” # this need to replaced from one var file  
loop:
- dev
- uat
- stg2

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 19, 2023, 8:37pm UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/6 "2023-05-19T20:37:37Z")

</div>

> will my below playbook work  
> I don’t think so.

1. Your serverlist is a string with “{{ item }}” in it, but you’re using it in a shell module invocation with no loops, i.e. where “item” isn’t defined.

2. In your shell script, your jq output is being directed in group\_vars/server1.yml _with replacement_. Once you fix issue #1 and start looping, you’ll find servers1.yml contains the data only for the last iteration.

I’m puzzled by your placement of the serverlist definition in a separate file rather than in a vars: section in the playbook, or even hard-coded directly in the embedded shell script. Nor is it clear to me what you expect the final debug step to show you. I mean, we know it will look like this:

> TASK [unnamed debug loop] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> (item=dev) =\> {  
> “msg”: “data , [https://test-dev.test.com/testapi/test/server?environment=dev&os=linux](https://test-dev.test.com/testapi/test/server?environment=dev&os=linux)”  
> }  
> ok: [localhost] =\> (item=uat) =\> {  
> “msg”: “data , [https://test-dev.test.com/testapi/test/server?environment=uat&os=linux](https://test-dev.test.com/testapi/test/server?environment=uat&os=linux)”  
> }  
> ok: [localhost] =\> (item=stg2) =\> {  
> “msg”: “data , [https://test-dev.test.com/testapi/test/server?environment=stg2&os=linux](https://test-dev.test.com/testapi/test/server?environment=stg2&os=linux)”  
> }

but what’s the point?

---

<div class="post-metadata">

**Author:** ![Narmada\_Karthika](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Narmada\_Karthika](https://forum.ansible.com/u/Narmada_Karthika)\
**Post date:** [May 19, 2023, 8:58pm UTC](https://forum.ansible.com/t/can-we-not-use-2-var-files-in-a-playbook/38011/7 "2023-05-19T20:58:03Z")

</div>

Thankyou for taking time and looking into it…  
you made a valid questions,

1. I am trying this approach so that I can pass {item} as jenkins parameters. when dev fro item group is given it should print all servers from that env.
2. group\_vars/server1.yml I am trying to get the first part working so I did not specify the output file here. ( will make a note next time not to miss it)

also can you please elaborate more here “Your serverlist is a string with “{{ item }}” in it, but you’re using it in a shell module invocation with no loops, i.e. where “item” isn’t defined.”

I mean best possibilities to fulfill my need.
