Nested Loop Variables

115 views
Skip to first unread message

Wynne Pirini

unread,
May 15, 2014, 3:51:01 AM5/15/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
May 15, 2014, 7:36:44 AM5/15/14
to ansible...@googlegroups.com
"  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




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4e82e4ab-168a-4672-91e5-114cb268de1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wynne Pirini

unread,
May 15, 2014, 4:19:22 PM5/15/14
to ansible...@googlegroups.com
Thanks for your help, I'll give this a go.

Wynne Pirini

unread,
May 15, 2014, 5:42:07 PM5/15/14
to ansible...@googlegroups.com
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', '%' ]


TASK: [mariadb | Create/assign database users to db and grant permissions] ****
failed: [192.168.50.12] => (item=['john', 'john1234', 'sample.sql', 'localhost']) => {"failed": true, "item": ["john", "john1234", "sample.sql", "localhost"], "parsed": false}
invalid output was: Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1400187475.47-8611387391407/mysql_user", line 1651, in <module>
    main()
  File "/root/.ansible/tmp/ansible-tmp-1400187475.47-8611387391407/mysql_user", line 459, in main
    changed = user_add(cursor, user, host, password, priv)
  File "/root/.ansible/tmp/ansible-tmp-1400187475.47-8611387391407/mysql_user", line 161, in user_add
    privileges_grant(cursor, user,host,db_table,priv)
  File "/root/.ansible/tmp/ansible-tmp-1400187475.47-8611387391407/mysql_user", line 289, in privileges_grant
    cursor.execute(query)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "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")

failed: [192.168.50.12] => (item=['john', 'john1234', 'sample.sql', '%']) => {"failed": true, "item": ["john", "john1234", "sample.sql", "%"]}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials

FATAL: all hosts have already failed -- aborting

Michael DeHaan

unread,
May 15, 2014, 5:57:59 PM5/15/14
to ansible...@googlegroups.com
It's a database error, so nothing with "with_nested".

Key part:

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Wynne Pirini

unread,
May 15, 2014, 6:12:26 PM5/15/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
May 15, 2014, 6:31:34 PM5/15/14
to ansible...@googlegroups.com
Seems reasonable to me since that's not a valid hostname?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages