Changes between Version 17 and Version 18 of Csle2022/Agenda/Ansible


Ignore:
Timestamp:
Nov 21, 2022, 4:12:44 AM (2 years ago)
Author:
dushmantha
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Csle2022/Agenda/Ansible

    v17 v18  
    205205
    206206{{{
    207 - name: setup mysql
     207- name: setup mysql
     208  hosts: all
    208209  become: yes
    209   hosts: Dbservers
     210  gather_facts: false
    210211  vars:
    211     - user: test
    212     - password: M@#567uers
    213     - db: testdb
     212    root_password: Redact#12
     213    db_name: new
     214    user_name: newuser
     215    user_password: Redact#13
    214216  tasks:
    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
    232244      mysql_user:
    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
     263Create template
     264
     265
     266{{{
     267nano my.cnf.j2.yml
     268}}}
     269
     270
     271{{{
     272[client]
     273user=root
     274password={{ root_password }}
     275}}}
     276
     277
    248278
    249279'''Run the Playbook - In control node - As root'''
    250280
    251281{{{
    252 ansible-playbook <playbook name>.yml -e
    253 }}}
     282ansible-playbook --ask-become-pass -i inventory <playbook name>.yml
     283}}}
     284
     285Give the managed VM password (only one password as the VMs have the same password) when prompted.
    254286
    255287'''Verify results - In managed nodes'''