create_db_and_users.yml 2.49 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
# If running ansible from a python virtualenv you will need a command like the following
#
# ansible-playbook -c local -i 'localhost,' create_dbs_and_users.yml -e@./db.yml -e "ansible_python_interpreter=$(which python)"
#
11
# where the content of db.yml contains the following dictionaries
e0d committed
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 45 46 47 48
#
# 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
49 50 51
  hosts: all
  gather_facts: False
  tasks:
e0d committed
52 53
    # Install required library, currently this needs to be available
    # to system python.
e0d committed
54 55 56 57
    - name: install python mysqldb module
      pip: name={{item}} state=present
      with_items:
        - MySQL-python
58

e0d committed
59
    - name: create mysql databases
60 61 62 63 64 65 66
      mysql_db:
        db: "{{ item.name}}"
        state: "{{ item.state }}"
        encoding: "{{ item.encoding }}"
        login_host: "{{ item.login_host }}"
        login_user: "{{ item.login_user }}"
        login_password: "{{ item.login_password }}"
67
      with_items: "{{ databases }}"
Edward Zarecor committed
68 69
      tags:
        - dbs
70 71

    - name: create mysql users and assign privileges
72 73
      mysql_user:
        name: "{{ item.name }}"
74
        state: "{{ item.state | default('present') }}"
75 76 77 78 79 80
        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 }}"
John Eskew committed
81
        append_privs: yes
82
      with_items: "{{ database_users }}"
Edward Zarecor committed
83
      tags:
84
        - users