| 215 |  | - name: installing mysql and dependencies | 
          
            | 216 |  | package: | 
          
            | 217 |  | name: "{{item}}" | 
          
            | 218 |  | state: present | 
          
            | 219 |  | update_cache: yes | 
          
            | 220 |  | loop: | 
          
            | 221 |  | - mysql-server | 
          
            | 222 |  | - mysql-client | 
          
            | 223 |  | - python3-mysqldb | 
          
            | 224 |  | - libmysqlclient-dev | 
          
            | 225 |  | become: yes | 
          
            | 226 |  | - name: start and enable mysql service | 
          
            | 227 |  | service: | 
          
            | 228 |  | name: mysql | 
          
            | 229 |  | state: started | 
          
            | 230 |  | enabled: yes | 
          
            | 231 |  | - name: creating mysql user | 
          
            |  | 217 | - name: Update | 
          
            |  | 218 | shell:  apt update | 
          
            |  | 219 |  | 
          
            |  | 220 | - name: install python, pip etc | 
          
            |  | 221 | shell: apt-get -y install "{{ item }}" | 
          
            |  | 222 | with_items: | 
          
            |  | 223 | - pip | 
          
            |  | 224 | - python3-dev | 
          
            |  | 225 | - default-libmysqlclient-dev | 
          
            |  | 226 | - build-essential | 
          
            |  | 227 |  | 
          
            |  | 228 | - name: Install MySQL server | 
          
            |  | 229 | shell: apt-get -y install mysql-server | 
          
            |  | 230 |  | 
          
            |  | 231 | - name: Install MySQL client | 
          
            |  | 232 | shell: apt-get -y install mysql-client | 
          
            |  | 233 |  | 
          
            |  | 234 | - name: pip install mysqlclient | 
          
            |  | 235 | shell: pip install mysqlclient | 
          
            |  | 236 |  | 
          
            |  | 237 | - name: Start the MySQL service | 
          
            |  | 238 | action: service name=mysql state=started | 
          
            |  | 239 |  | 
          
            |  | 240 | - name: copy .my.cnf file with root password credentials | 
          
            |  | 241 | template: src=/home/docker/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600 | 
          
            |  | 242 |  | 
          
            |  | 243 | - name: update mysql root password for all root accounts | 
        
        
          
            | 233 |  | name: "{{user}}" | 
          
            | 234 |  | password: "{{password}}" | 
          
            | 235 |  | priv: '*.*:ALL' | 
          
            | 236 |  | host: '%' | 
          
            | 237 |  | state: present | 
          
            | 238 |  | - name: creating db | 
          
            | 239 |  | mysql_db: | 
          
            | 240 |  | name: "{{db}}" | 
          
            | 241 |  | state: present | 
          
            | 242 |  | handlers: | 
          
            | 243 |  | - name: restart mysql | 
          
            | 244 |  | service: | 
          
            | 245 |  | name: mysql | 
          
            | 246 |  | state: restarted | 
          
            | 247 |  | }}} | 
          
            |  | 245 | name: root | 
          
            |  | 246 | host: localhost | 
          
            |  | 247 | password: "{{ root_password }}" | 
          
            |  | 248 |  | 
          
            |  | 249 | - name: Create database | 
          
            |  | 250 | shell: mysql -u root -p{{ root_password }} -e 'CREATE DATABASE IF NOT EXISTS {{ db_name }};' | 
          
            |  | 251 |  | 
          
            |  | 252 | - name: Create user | 
          
            |  | 253 | shell: mysql -u root -p{{ root_password }} -e "CREATE USER '{{ user_name }}'@'%' IDENTIFIED BY '{{ user_password }}';" | 
          
            |  | 254 |  | 
          
            |  | 255 | - name: Grant permissions | 
          
            |  | 256 | shell: mysql -u root -p{{ root_password }} -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON {{ db_name }}.* TO '{{ user_name }}'@'%';" | 
          
            |  | 257 |  | 
          
            |  | 258 | - name: Reload privileges | 
          
            |  | 259 | shell: mysql -u root -p{{ root_password }} -e "FLUSH PRIVILEGES;" | 
          
            |  | 260 |  | 
          
            |  | 261 | }}} | 
          
            |  | 262 |  | 
          
            |  | 263 | Create template | 
          
            |  | 264 |  | 
          
            |  | 265 |  | 
          
            |  | 266 | {{{ | 
          
            |  | 267 | nano my.cnf.j2.yml | 
          
            |  | 268 | }}} | 
          
            |  | 269 |  | 
          
            |  | 270 |  | 
          
            |  | 271 | {{{ | 
          
            |  | 272 | [client] | 
          
            |  | 273 | user=root | 
          
            |  | 274 | password={{ root_password }} | 
          
            |  | 275 | }}} | 
          
            |  | 276 |  | 
          
            |  | 277 |  |