LoginSignup
51
54

More than 5 years have passed since last update.

AnsibleでMySQL導入 (CentOS 7)

Posted at

Ansible には MySQLのユーザーを追加削除できる mysql_user がある.
しかし,使うためには少し準備が必要.具体的には,下図のようになる.

ansible_mysql_user

(例)ユーザー wordpress と データベース wordpress を生成する

変数として
- mysql_root_password
- mysql_db_password
が必要

---
- name: MariaDB のクライアント&サーバーをインストール
  yum:
    name: "{{ item }}"
    state: present
    enablerepo: remi
  with_items:
    - mariadb
    - mariadb-server
    - MySQL-python
  sudo: yes

- name: MariaDB を常時起動にする (サービスを有効化)
  service:
    name: mariadb
    state: started
    enabled: yes
  sudo: yes

- name: root アカウントのパスワードを設定
  mysql_user:
    name: root
    host: localhost
    password: "{{ mysql_root_password }}"

- name: ~/.my.cnf を配置
  template:
    src: my.cnf.j2
    dest: "{{ lookup('env', 'HOME') }}/.my.cnf"
    mode: 0600

- name: データベース wordpress を作成
  mysql_db:
    name: wordpress
    state: present

- name: MySQLユーザー wordpress を作成し,wordpress.* にすべての権限を与える
  mysql_user:
    name: wordpress
    password: "{{ mysql_db_password }}"
    priv: "wordpress.*:ALL"
    host: localhost
    state: present
51
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
54