Ansible - grab a key from a dictionary (but not in a loop)
<p>Another question regarding dictionaries in Ansible!</p>

<p>For convenience, I have certain values for mysql databases held in dictionaries, which works fine to loop over using <code>with_dict</code> to create the DBs and DB users.</p>

<pre><code>mysql_dbs:
  db1:
    user: db1user
    pass: "jdhfksjdf"
    accessible_from: localhost
  db2:
    user: db2user
    pass: "npoaivrpon"
    accessible_from: localhost
</code></pre>

<p>task:</p>

<pre><code>- name: Configure mysql users
  mysql_user: name={{ item.value.user }} password={{ item.value.pass }} host={{ item.value.accessible_from }} priv={{ item.key }}.*:ALL  state=present
  with_dict: "{{ mysql_dbs }}"
</code></pre>

<p>However, I would like to use the key from one of the dictionaries in another task, but I don't want to loop over the dictionaries, I would only like to use one at a time. How would I grab the key that describes the dictionary (sorry, not sure about terminology)?</p>

<p>problem task:</p>

<pre><code>- name: Add the db1 schema
  shell: mysql {{ item }} &lt; /path/to/db1.sql 
  with_items: '{{ mysql_dbs[db1] }}'
</code></pre>

<p>Error in ansible run:</p>

<pre><code>fatal: [myhost]: FAILED! =&gt; {"failed": true, "msg": "'item' is undefined"}
</code></pre>

<p>I'm willing to believe <code>with_items</code> isn't the best strategy here, but does anyone have any ideas what is the right one?</p>

<p>Thanks in advance, been stuck on this for a while now...</p>