{"action":"created","issue":{"url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/106","repository_url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql","labels_url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/106/labels{/name}","comments_url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/106/comments","events_url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/106/events","html_url":"https://github.com/geerlingguy/ansible-role-mysql/issues/106","id":149671568,"number":106,"title":"Use loop to define databases (and users)","user":{"login":"cirdan","id":424306,"avatar_url":"https://avatars.githubusercontent.com/u/424306?v=3","gravatar_id":"","url":"https://api.github.com/users/cirdan","html_url":"https://github.com/cirdan","followers_url":"https://api.github.com/users/cirdan/followers","following_url":"https://api.github.com/users/cirdan/following{/other_user}","gists_url":"https://api.github.com/users/cirdan/gists{/gist_id}","starred_url":"https://api.github.com/users/cirdan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cirdan/subscriptions","organizations_url":"https://api.github.com/users/cirdan/orgs","repos_url":"https://api.github.com/users/cirdan/repos","events_url":"https://api.github.com/users/cirdan/events{/privacy}","received_events_url":"https://api.github.com/users/cirdan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-04-20T06:22:04Z","updated_at":"2016-10-04T21:16:26Z","closed_at":null,"body":"Hi,\r\nThank you for your huge work on Ansible roles.\r\n(This is a question, not a bug, and maybe related to my new and partial knowledge of Ansible)\r\nI'm trying to define the databases to be created using facts defined in the target node, and a loop (with_items).\r\nBut I can't find where to do this : I can't use a loop inside the vars definition.\r\nYou give an example but with one database : how can I dynamically define more than one database ?\r\n\r\nThanks for any help."},"comment":{"url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/comments/251516231","html_url":"https://github.com/geerlingguy/ansible-role-mysql/issues/106#issuecomment-251516231","issue_url":"https://api.github.com/repos/geerlingguy/ansible-role-mysql/issues/106","id":251516231,"user":{"login":"bennyt2","id":5905154,"avatar_url":"https://avatars.githubusercontent.com/u/5905154?v=3","gravatar_id":"","url":"https://api.github.com/users/bennyt2","html_url":"https://github.com/bennyt2","followers_url":"https://api.github.com/users/bennyt2/followers","following_url":"https://api.github.com/users/bennyt2/following{/other_user}","gists_url":"https://api.github.com/users/bennyt2/gists{/gist_id}","starred_url":"https://api.github.com/users/bennyt2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bennyt2/subscriptions","organizations_url":"https://api.github.com/users/bennyt2/orgs","repos_url":"https://api.github.com/users/bennyt2/repos","events_url":"https://api.github.com/users/bennyt2/events{/privacy}","received_events_url":"https://api.github.com/users/bennyt2/received_events","type":"User","site_admin":false},"created_at":"2016-10-04T21:16:26Z","updated_at":"2016-10-04T21:16:26Z","body":"I was able to figure this out with help from [this stackoverflow post](http://stackoverflow.com/a/35608380/6839737).\r\n\r\nAssume I have the following variables and facts:\r\n\r\nVariables\r\n```\r\nusername: test_user\r\npassword: test_pass\r\npriv: test_priv\r\n```\r\nFacts\r\n```\r\n\"groups.apiservers\": [\r\n    \"192.168.4.30\", \r\n    \"192.168.4.31\"\r\n],\r\n\"groups.webservers\": [\r\n    \"192.168.4.32\", \r\n    \"192.168.4.33\"\r\n]\r\n```\r\n\r\nThis task loops through the facts and adds an object with user information to the `mysql_users` array. You'll need to run this task before you run the `geerlingguy.mysql` role.\r\n```\r\n- name: \"Define mysql users\"\r\n  set_fact:\r\n    mysql_users: \"{{ mysql_users|default({}) + [{'name': username, 'host': item, 'password': password, 'priv': priv }] }}\"\r\n  with_items:\r\n    - \"{{groups.apiservers}}\"\r\n    - \"{{groups.webservers}}\"\r\n```\r\n\r\nThe resulting `mysql_users` array looks like this:\r\n\r\n```\r\n{\r\n    \"host\": \"192.168.4.30\", \r\n    \"name\": \"test_user\", \r\n    \"password\": \"test_pass\", \r\n    \"priv\": \"test_priv\"\r\n}, \r\n{\r\n    \"host\": \"192.168.4.31\", \r\n    \"name\": \"test_user\", \r\n    \"password\": \"test_pass\", \r\n    \"priv\": \"test_priv\"\r\n}, \r\n{\r\n    \"host\": \"192.168.4.32\", \r\n    \"name\": \"test_user\", \r\n    \"password\": \"test_pass\", \r\n    \"priv\": \"test_priv\"\r\n},\r\n{\r\n    \"host\": \"192.168.4.33\", \r\n    \"name\": \"test_user\", \r\n    \"password\": \"test_pass\", \r\n    \"priv\": \"test_priv\"\r\n}\r\n```"}}
{"actor":{"display_login":"bennyt2"}}