2017-04-14 5 views
2

あなたはmysqlのrootパスワードの更新と権限の付与のためのymlを構築するアイデアがありますか?私は自分の作ったプレイブックを新鮮にインストールしても、期待通りに動作し、全く問題はありません。しかし、私が不規則なプロビジョニングをやり直すと、ルートパスワードを設定することができなくなり、エラーが発生します。以下は、私は全力を尽くすことができるよ私のコード可能な設定mysqlのルートのパスワード

mysql.yml

--- 
- name: Install the MySQL packages 
    apt: name={{ item }} state=installed update_cache=yes 
    with_items: 
    - mysql-server 
    - mysql-client 
    - python-mysqldb 
    - libmysqlclient-dev 

- name: drop database {{ dbname }} 
    mysql_db: 
    name: "{{ dbname }}" 
    login_user: "{{ dbuser }}" 
    login_password: "{{ dbpass }}" 
    state: absent 
    delegate_to: "{{ dbhost }}" 
    run_once: true 

- name: create database {{ dbname }} 
    mysql_db: 
    name: "{{ dbname }}" 
    login_user: "{{ dbuser }}" 
    login_password: "{{ dbpass }}" 
    state: present 
    delegate_to: "{{ dbhost }}" 
    run_once: true 

- name: ensure mysql is running and starts on boot 
    service: name=mysql state=started enabled=true 

- name: copy .my.cnf file with root password credentials 
    template: src=my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600 

- name: update mysql root password for all root accounts "{{ dbpass }}" 
    mysql_user: name={{ dbuser }} host={{ item }} password="{{ dbpass }}" priv="{{ dbname }}.*:ALL,GRANT" 
    with_items: 
    - localhost 
    - 127.0.0.1 

- name: grant privilege on "{{ dbname }}" to "{{ dbuser }}" 
    mysql_user: 
    name: "{{ item.user }}" 
    host: "{{ item.host }}" 
    password: "{{ dbpass }}" 
    login_user: "{{ dbuser }}" 
    login_password: "{{ dbpass }}" 
    priv: "{{ dbname }}.*:ALL" 
    state: present 
    with_items: 
    - { user: "{{ dbuser }}" , host: localhost } 
    - { user: "{{ dbuser }}" , host: 127.0.0.1 } 
    delegate_to: "{{ dbhost }}" 
    run_once: true 

- name: ensure anonymous users are not in the database 
    mysql_user: name='' host={{ item }} state=absent 
    with_items: 
    - 127.0.0.1 
    - localhost 

- name: remove the test database 
    mysql_db: name=test state=absent 

my.cnf.j2

[client] 
user=root 
password={{ dbpass }} 

デフォルト/ main.yml

--- 
dbhost: localhost 
dbname: mydb 
dbuser: root 
dbpass: root 

ですその新鮮なインストールがうまくいけば、2回目に実行すると、以下のエラーが表示されます

enter image description here

答えて

3

あなたは次のタスクでそれを変更しようとする直前に新しいパスワードで.my.cnfを更新するように思えます。

with_itemsはモジュールを複数回実行するので、パスワードを更新するときにhost_allオプションを使用し、同じエラーが発生する可能性があります。最初のアイテムのパスワードを変更し、2番目のアイテムを接続できません。

+0

実際のルートアカウントを更新する前に、まず.my.cnfを更新することができます。 –

2

これについて正しい答えを既に見つけてください。だから私は、私と同じ悩みを持つもの

のためだけに参照==========================私の答えを追加することがあります===============================

--- 
# Install the needed package of mysql 
- name: Install MySQL packages 
    apt: pkg={{ item }} state=installed 
    with_items: 
    - bundler 
    - mysql-server 
    - mysql-client 
    - libmysqlclient-dev 
    - python-mysqldb 
    - build-essential 

# Update the root password immediately. This should come first as ordering 
# is very important 
- name: Update mysql root password for all root accounts "{{ dbpass }}" 
    mysql_user: name=root password="{{ dbpass }}" priv="*.*:ALL,GRANT" 

# After we update the root password we are going to use this everytime 
# we do an update or create something on mysql 
# we will create a copy in /root/.my.cnf as this will be use to check 
# the login or root credential. Meaning this should contain the latest 
# password of the root (right after we update the root password) 
- name: copy .my.cnf file with root password credentials 
    template: src=my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600 
    notify: Restart the MySQL service 

# Remove the unnecessary db for now 
- name: Remove the test database 
    mysql_db: name=test state=absent 

# Make sure no anonymous user in the db 
- name: ensure anonymous users are not in the database 
    mysql_user: name='' host={{ item }} state=absent 
    with_items: 
    - 127.0.0.1 
    - localhost 

# Delete the user if its existing so that we can create the user again 
- name: Delete deploy DB user 
    mysql_user: name={{ dbuser }} password={{ dbpass }} state=absent 
    notify: Restart the MySQL service 

# Create our own user aside from the root password 
# here our root password and new user created will have the same password 
- name: Add deploy DB user 
    mysql_user: name={{ dbuser }} password={{ dbpass }} priv=*.*:ALL,GRANT state=present 
    notify: Restart the MySQL service 

# Delete databases. This should not be included in production. 
# this is only on local so its fine. 
- name: Drop databases 
    mysql_db: 
    name: "{{ item }}" 
    login_user: "{{ dbuser }}" 
    login_password: "{{ dbpass }}" 
    state: absent 
    with_items: 
    - db1 
    - db2 
    - "{{ dbname }}" 
    run_once: true 

# Recreate the databases 
- name: Create databases 
    mysql_db: 
    name: "{{ item }}" 
    login_user: "{{ dbuser }}" 
    login_password: "{{ dbpass }}" 
    state: present 
    with_items: 
    - db1 
    - db2 
    - "{{ dbname }}" 
    run_once: true 

# Grant the privilege for the newly created user 
- name: grant privilege on "{{ dbname }}" to "{{ dbuser }}" 
    mysql_user: 
    name: "{{ item.user }}" 
    host: "{{ item.host }}" 
    password: "{{ dbpass }}" 
    priv: "*.*:ALL" 
    state: present 
    with_items: 
    - { user: "{{ dbuser }}" , host: localhost } 
    - { user: "{{ dbuser }}" , host: 127.0.0.1 } 
+0

これは私の答えとどう違うのですか?その順序は重要ですか? –

+1

@ ConstantinSuvorov実際には違いはありません。私はちょうどこれのための完全なymlファイルを追加しました。誰かを入れて、ファイルの実際の構造がどのように見えるのか不思議です –

関連する問題