Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
4
Indexable
Never
/home/control/
|_main_call.yml
|_hosts.txt
|_roles/
    |_mysql-serv/
        |_handlers/
        |    |_main.yml
        |_files/
        |    |_test_db.sql
        |_tasks/
        |    |_main.yml
        |_vars/
             |_main.yml
----------------------------------------------------
### hosts.txt

[mysql]

server-1 ansible_host=192.168.56.21
server-2 ansible_host=192.168.56.22

---------------------------------------------------------
### main_call.yml
---
- hosts: mysql
  name: Installation of mysql servers
  become: yes

  roles:
    - mysql-serv
----------------------------------------------------------
### roles/mysql-serv/vars/main.yml
---
# vars file for mysql-serv
mysql_apt:
  - mysql-server
  - python3-mysqldb
  - libmysqlclient-dev
  - python-dev
  - python3-dev
  - python-pip
mysql_pip:
  - MySQL-python
mysql_user: cimsi_user
mysql_passwd: sudo
db_name: mysqlDB
------------------------------------------------------------
### roles/mysql-serv/tasks/main.yml
---
---
# tasks file for mysql-serv
- name: Installing mySQL packages with apt
  apt:
    name: "{{ mysql_apt }}"
    state: present
    update_cache: yes
- name: Installing mySQL packages with pip
  pip:
    name: "{{ mysql_pip }}"
    state: present

- name: Start mySQL service
  service:
    name: mysql
    state: started
    enabled: yes

- name: Create mySQL user
  mysql_user:
    name: "{{ mysql_user }}"
    password: "{{ mysql_passwd }}"
    priv: '*.*:ALL'
    host: '%'
    state: present

- name: Create test database
  mysql_db:
    name: "{{ db_name }}"
    state: present

- name: Add data to test database
  copy: src=../files/test_db.sql dest=/tmp/test_db.sql

- name: Insert data into test database
  mysql_db: name="{{ db_name }}" state=import target=/tmp/test_db.sql login_user="{{ mysql_user }}" login_password="{{ mysql_passwd }}"
  notify: restart mySQL
-----------------------------------------------------
### roles/mysql-serv/handlers/main.yml
---
# handlers file for mysql-serv
- name: restart mySQL 
  service:
    name: mysql
    state: restarted
    enabled: yes
Leave a Comment