Hi, First of all I want to thank you all for this thread, it helped me 
solve a problem that I was struggling with. This is my first post to the 
group and want to say hello :)

I have a slightly different solution to this problem as I was running 
several tasks in a row, all using with_items on the same array and could 
not use a handler.

I am building a multi wordpress installation playbook and solved the 
problem like this:

- name: Create wordpress database
  mysql_db: name={{ item.wp_db_name }} state=present
  with_items: wordpress_nginx_sites
  register: create_wordpress_databases

#- name: debug reg
#  debug: var="{{ create_wordpress_databases }}"

- name: Create wordpress database user
  mysql_user: name={{ item.wp_db_user }} password={{ item.wp_db_password }} 
priv={{ item.wp_db_name }}.*:ALL host='localhost' state=present
  with_items: wordpress_nginx_sites

- name: Copy the wordpress mysql db
  copy: src=files/sites/{{ item.item.name }}/{{ item.item.wp_db_name }}.sql 
dest=/tmp
  when: item.changed == True
  with_items: create_wordpress_databases.results

- name: Import the database
  mysql_db: name={{ item.item.wp_db_name }} state=import target=/tmp/{{ 
item.item.wp_db_name }}.sql
  when: item.changed == True
  with_items: create_wordpress_databases.results


As you can see. Because there are multiple databases being created via 
looping a dictionary, I could not see a way to call a handler and know 
which element of the dictionary had triggered the handler. 

The interesting part of this is when looping the result of the create task, 
you must use {{ item.item.name }} to get at the value.

I am fairly new to ansible so feel free to advise on a better way of doing 
this if Its not best practice.

Jae.



On Friday, 7 February 2014 04:06:48 UTC, Jeff Geerling wrote:
>
> Coming into this discussion a little late, but here's an example to 
> illustrate what's being suggested:
>
> Play:
>
> - name: Ensure example database exists.
>   mysql_db: name=exampledb state=present
>   notify: import example database
>
> Handler:
>
> - name: import example database
>   mysql_db: name=example state=import target=/path/to/example_schema.sql
>
> This works great, and avoids errors I was getting when I tried doing the 
> import straightaway; the import only seems to work if the database already 
> exists.
>
> On Tuesday, November 26, 2013 3:12:25 PM UTC-6, Fred Badel wrote:
>>
>> I don’t know why, but in my mind, notifying handlers was only to restart 
>> a service … now I can see the light!! :)
>>
>> Although, this would not help if the db exists but is empty, you just 
>> help me understand how I can use and how useful “notify” can be !  
>>
>> Thank you.
>>
>> fred
>>
>> On Nov 26, 2013, at 19:18 , David Karban <[email protected]> wrote:
>>
>> Hi, 
>>
>> you can use notify in create DB to create schema. That way, it will be 
>> called only on db creation.
>>
>> David
>>  Dne 26.11.2013 17:42 "Fred Badel" <[email protected]> napsal(a):
>>
>>> Hello,
>>>
>>> I have a playbook that create a mysql DB and import an empty schema:
>>>
>>> - name: create DB
>>>   mysql_db: name={{ db_name }} state=present login_host=127.0.0.1 
>>>
>>> - name: create schema
>>>   mysql_db: name={{ db_name }} state=import login_host=127.0.0.1 
>>> target=/create_tables.sql 
>>>
>>> From what i could notice, the sql file is imported each time the 
>>> playbook is run ... (aka, the task is marked as changed each time I run the 
>>> playbook)
>>> is this an expected behavior?
>>> does someone have a recommended way of using the import feature and run 
>>> it only once?
>>>
>>> Thanks.
>>>
>>> Fred
>>>
>>> -- 
>>> 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 [email protected].
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>> -- 
>> 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 [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c622bc6c-d7c9-4440-81bb-1d048b8df7c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to