How to Create Database by Ansible


Create Mysql Database in Remote Machine or Ansible Client  


MySQL-python27.x86_64 (importent)

vim mysql.yml
---
- hosts: web-server
  remote_user: root
  tasks:
      - name: create a test database
        mysql_db: db=testdb state=present
      - name: ensure mysql is running
        service: name=mysqld state=restarted enabled=yes
Past in Playbook  file like  mysql.yml

Install Mysql and Http (Apache)

like :- apache_mysql_install.yml
---
vim apache_mysql_install.yml
- hosts: web-server
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: pkg=httpd state=latest
  - name: ensure mysql is at the latest version
    yum: pkg=mysql-server state=latest
  - name: replace default index.html file
    copy: src=/root/test/index.html dest=/var/www/html/ mode=0644
    notify:
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
    service: name=mysqld state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted
    - name: restart mysql
      service: name=mysqld state=restarted

- hosts: databases
  remote_user: master
  tasks:
    - name: Check if database Exits if so delete
      mysql_db:
               db=testdb state=absent - name: create a test database
              mysql_db:db=testdb state=present

If you have a password of mysql root user then use this script
- hosts: web-server
  remote_user: root
  tasks:
       - name: Create db_name database
         mysql_db:
            name=db_name
            state=present
            login_user=root
            login_password=abc@123
       - name: ensure mysql is running
         service: name=mysqld state=restarted enabled=yes

If you Create Database and User the Privileges
vim permission_mysql.yml
- hosts: web-server
  remote_user: root
  tasks:
       - name: Create db_name database
         mysql_db:
            name=db_name
            state=present
            login_user=root
            login_password=abc@123
       - name: create user
         mysql_user:
            name=test
            login_user=root
            login_password=abc@123
            password=abc@123
            priv=*.*:ALL
            state=present
       - name: ensure mysql is running
         service: name=mysqld state=restarted enabled=yes


How to Create Database by Ansible How to Create Database by Ansible Reviewed by Unknown on October 31, 2017 Rating: 5

No comments:

Scribe

http://feeds.feedburner.com/LinuxAndAws
Powered by Blogger.
X

Get Updates On

Linux Tutorial

AWS Tutorial

Devops Tutorial

We are going to send you our resources for free. To collect your copy at first, join our mailing list. So don't miss any updates, stay connected!