create_db_and_users.yml 2.18 KB
Newer Older
e0d committed
1
#
e0d committed
2 3
# This play will create databases and user for an application.
# It can be run like so:
e0d committed
4
#
Max Rothman committed
5
# ansible-playbook -c local -i 'localhost,' create_dbs_and_users.yml -e@./db.yml
e0d committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#
# where the content of dbs.yml contains the following dictionaries
#
# database_connection: &default_connection
#   login_host: "mysql.example.org"
#   login_user: "root"
#   login_password: "super-secure-password"

# DEFAULT_ENCODING: "utf8"

# databases:
#   reports:
#     state: "present"
#     encoding: "{{ DEFAULT_ENCODING }}"
#     <<: *default_connection
#   application:
#     state: "present"
#     encoding: "{{ DEFAULT_ENCODING }}"
#     <<: *default_connection

# database_users:
#   migrate:
#     state: "present"
#     password: "user-with-ddl-privs"
#     host: "%"
#     privileges:
#       - "reports.*:SELECT,INSERT,UPDATE,DELETE,ALTER,CREATE,DROP,INDEX"
#       - "wwc.*:SELECT,INSERT,UPDATE,DELETE,ALTER,CREATE,DROP,INDEX"
#     <<: *default_connection
#   runtime:
#     state: "present"
#     password: "user-with-dml-privs"
#     host: "%"
#     privileges:
#       - "reports.*:SELECT"
#       - "wwc.*:SELECT,INSERT,UPDATE,DELETE"
#     <<: *default_connection

- name: Create databases and users
45 46 47
  hosts: all
  gather_facts: False
  tasks:
e0d committed
48 49
    # Install required library, currently this needs to be available
    # to system python.
e0d committed
50 51 52 53
    - name: install python mysqldb module
      pip: name={{item}} state=present
      with_items:
        - MySQL-python
54

e0d committed
55
    - name: create mysql databases
56
      mysql_db: >
57 58 59 60 61 62
        db={{ item.name}}
        state={{ item.state }}
        encoding={{ item.encoding }}
        login_host={{ item.login_host }}
        login_user={{ item.login_user }}
        login_password={{ item.login_password }}
63
      with_items: databases
Edward Zarecor committed
64 65
      tags:
        - dbs
66 67 68

    - name: create mysql users and assign privileges
      mysql_user: >
69 70 71 72 73 74 75
        name="{{ item.name }}"
        priv="{{ '/'.join(item.privileges) }}"
        password="{{ item.password }}"
        host={{ item.host }}
        login_host={{ item.login_host }}
        login_user={{ item.login_user }}
        login_password={{ item.login_password }}
e0d committed
76
        append_privs=yes
77
      with_items: database_users
Edward Zarecor committed
78
      tags:
79
        - users