# Nested Loop Variables

**URL:** <https://forum.ansible.com/t/nested-loop-variables/15242>\
**Category:** Ansible Project\
**Created:** [May 15, 2014, 7:51am UTC](https://forum.ansible.com/t/nested-loop-variables/15242 "2014-05-15T07:51:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wynne\_Pirini](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@Wynne\_Pirini](https://forum.ansible.com/u/Wynne_Pirini)\
**Post date:** [May 15, 2014, 7:51am UTC](https://forum.ansible.com/t/nested-loop-variables/15242/1 "2014-05-15T07:51:01Z")

</div>

Hi there

I’ve searched for days for a clear and straight forward answer to this question.

In the following nested loop I want to use variables instead of the static values I have there.

- name: Create/assign database users to db and grant permissions

mysql\_user: name={{ item[0] }} password={{ item[1] }} priv={{ item[2] }}.\*:ALL state=present login\_host={{ item[3] }}  
with\_nested:

- ‘john’
- ‘john1234’
- ‘sample.sql’
- [‘localhost’,‘%’]

Actually, that doesn’t even work. So I’m already stuck.

Then in my group\_vars I have defined variables:

env:  
db\_import: sample.sql  
db\_name: sample\_db  
db\_user: john  
db\_password: john1234

I’ve tried all sorts of ways of using variables instead of the static values.

Can anyone help me with this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 11:36am UTC](https://forum.ansible.com/t/nested-loop-variables/15242/2 "2014-05-15T11:36:44Z")

</div>

" with\_nested:

- ‘john’
- ‘john1234’
- ‘sample.sql’
- [‘localhost’,‘%’]

It looks like the first should be a list of usernames, and the second should be a list of files, but you are specifying strings instead – which is what you would do if those were variable names.

with\_nested:

- [‘john’, ‘john1234’]
- [‘sampel.sql’]
- [‘localhost’, ‘%’]

OR:

with\_nested:

- db\_users
- sql\_files
- hosts

etc

---

<div class="post-metadata">

**Author:** ![Wynne\_Pirini](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@Wynne\_Pirini](https://forum.ansible.com/u/Wynne_Pirini)\
**Post date:** [May 15, 2014, 8:19pm UTC](https://forum.ansible.com/t/nested-loop-variables/15242/3 "2014-05-15T20:19:22Z")

</div>

Thanks for your help, I’ll give this a go.

---

<div class="post-metadata">

**Author:** ![Wynne\_Pirini](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@Wynne\_Pirini](https://forum.ansible.com/u/Wynne_Pirini)\
**Post date:** [May 15, 2014, 9:42pm UTC](https://forum.ansible.com/t/nested-loop-variables/15242/4 "2014-05-15T21:42:07Z")

</div>

This is the yaml I’m running and it’s giving an error:

- name: Create/assign database users to db and grant permissions  
mysql\_user: name={{ item[0] }} password={{ item[1] }} priv={{ item[2] }}.\*:ALL state=present login\_host={{ item[3] }}  
with\_nested:
- [‘john’]
- [‘john1234’]
- [‘sample.sql’]
- [‘localhost’, ‘%’]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 9:57pm UTC](https://forum.ansible.com/t/nested-loop-variables/15242/5 "2014-05-15T21:57:59Z")

</div>

It’s a database error, so nothing with “with\_nested”.

Key part:

“You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘.\* TO ‘john’@‘localhost’’ at line 1”

---

<div class="post-metadata">

**Author:** ![Wynne\_Pirini](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@Wynne\_Pirini](https://forum.ansible.com/u/Wynne_Pirini)\
**Post date:** [May 15, 2014, 10:12pm UTC](https://forum.ansible.com/t/nested-loop-variables/15242/6 "2014-05-15T22:12:26Z")

</div>

If I run this…

- name: Create/assign database user to db and grant permissions  
mysql\_user: name={{ item.env.db\_user }}  
password={{ item.env.db\_password }}  
priv={{ item.env.db\_name | default(item.site\_name) }}.\*:ALL,GRANT  
state=present  
login\_host={{ item.env.db\_host | default(‘localhost’) }}  
with\_items: web\_site

If item:env.db\_host == ‘localhost’ it works.

If item:env.db\_host == ‘%’ it fails.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 10:31pm UTC](https://forum.ansible.com/t/nested-loop-variables/15242/7 "2014-05-15T22:31:34Z")

</div>

Seems reasonable to me since that’s not a valid hostname?
