2014-10-27 6 views
23

mam problem z instalacją MySQL z ansibla na Vagrant ubuntu,zainstalować MySQL z ansibla na ubuntu

To moja część MySQL

--- 
- name: Install MySQL 
    apt: 
    name: "{{ item }}" 
    with_items: 
    - python-mysqldb 
    - mysql-server 

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

- name: Start the MySQL service 
    service: 
    name: mysql 
    state: started 
    enabled: true 

    # 'localhost' needs to be the last item for idempotency, see 
    # http://ansible.cc/docs/modules.html#mysql-user 
- name: update mysql root password for all root accounts 
    mysql_user: 
    name: root 
    host: "{{ item }}" 
    password: "{{ mysql_root_password }}" 
    priv: "*.*:ALL,GRANT" 
    with_items: 
    - "{{ ansible_hostname }}" 
    - 127.0.0.1 
    - ::1 
    - localhost 

I mam ten błąd

failed: [default] => (item=vagrant-ubuntu-trusty-64) => {"failed": true, "item": "vagrant-ubuntu-trusty-64"} 
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials 
failed: [default] => (item=127.0.0.1) => {"failed": true, "item": "127.0.0.1"} 
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials 
failed: [default] => (item=::1) => {"failed": true, "item": "::1"} 
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials 
failed: [default] => (item=localhost) => {"failed": true, "item": "localhost"} 
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials 

mój .my.cnf jest

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

a gdy skopiowane na serwer

[client] 
user=root 
password=root 

nie rozumiem dlaczego, ~/.my.cnf jest tworzony

projektu Github

Dzięki

+0

proszę udostępnić szablon '.my.cnf'. – tedder42

+0

@ tedder42 done;) – Ajouve

+0

Świetnie, mam złożoną odpowiedź, po prostu podwójnie sprawdzam ją w stosunku do świeżego instancji testowej. – tedder42

Odpowiedz

32

Kiedy mysql-server jest zainstalowany headlessly, nie ma hasła. Dlatego aby działał .my.cnf, powinien mieć pustą linię hasła. Oto co testowane na .my.cnf:

[client] 
user=root 
password= 

Jest to także nieco dziwne umieścić .my.cnf w katalogu vagrant użytkownika jako root i czytelne tylko jako root.

Po upewnieniu się, że hasło jest puste w .my.cnf, udało mi się poprawnie ustawić hasło root w tych czterech kontekstach. Zauważ, że nie działa po tym, ponieważ .my.cnf musiałaby zostać zaktualizowana, więc nie przejdzie testu idempotency.

Na stronie modułu ansys mysql_user znajduje się notatka, która sugeruje zapisanie hasła i , a następnie zapisanie pliku .my.cnf. Jeśli to zrobisz, będziesz potrzebował klauzuli where do działania mysql_user (prawdopodobnie z wcześniejszą statystyką pliku).

Jeszcze bardziej elegancka jest funkcja check_implicit_admin wraz z login_user i login_password. To jest piękne idempotent.

Jako trzeci sposób, być może check_implicit_admin czyni to jeszcze łatwiejszym.

Oto mój udany poradnik pokazujący powyższe, przetestowany przy użyciu kilku świeżych serwerów. Trochę z tego dumni. Uwaga: .my.cnf nie jest konieczne.

--- 
- hosts: mysql 
    vars: 
    mysql_root_password: fart 
    tasks: 
    - name: Install MySQL 
    apt: name={{ item }} update_cache=yes cache_valid_time=3600 state=present 
    sudo: yes 
    with_items: 
    - python-mysqldb 
    - mysql-server 
    #- name: copy cnf 
    # copy: src=.my.cnf dest=~/.my.cnf owner=ubuntu mode=0644 
    # sudo: yes 
    - name: Start the MySQL service 
    sudo: yes 
    service: 
     name: mysql 
     state: started 
     enabled: true 
    - name: update mysql root password for all root accounts 
    sudo: yes 
    mysql_user: 
     name: root 
     host: "{{ item }}" 
     password: "{{ mysql_root_password }}" 
     login_user: root 
     login_password: "{{ mysql_root_password }}" 
     check_implicit_admin: yes 
     priv: "*.*:ALL,GRANT" 
    with_items: 
     - "{{ ansible_hostname }}" 
     - 127.0.0.1 
     - ::1 
     - localhost 

(Edit- usunięte my.cnf)

+1

Dzięki, ale mam ten sam błąd: – Ajouve

+1

@ant próbujesz na nowym serwerze lub na tym, który miał częściową instalację zrobić? Jeśli to drugie, upewnij się, że '.my.cnf' nie istnieje, a następnie ręcznie sprawdź hasło roota. – tedder42

+0

Ja robię rezerwę dla włóczęgi – Ajouve

10

Oto mój pełny rola MySQL pracy, które mogą pomóc.

vars/main.yml

mysql_root_pass: mypassword #MySQL Root Password 

tasks/main.yml

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

- name: Update MySQL root password for all root accounts 
    mysql_user: name=root host={{ item }} password={{ mysql_root_pass }} state=present 
    with_items: 
    - "{{ ansible_hostname }}" 
    - 127.0.0.1 
    - ::1 
    - localhost 

- name: Copy the root credentials as .my.cnf file 
    template: src=root.cnf.j2 dest=~/.my.cnf mode=0600 

- name: Ensure Anonymous user(s) are not in the database 
    mysql_user: name='' host={{ item }} state=absent 
    with_items: 
    - localhost 
    - "{{ ansible_hostname }}" 

- name: Remove the test database 
    mysql_db: name=test state=absent 
    notify: 
    - Restart MySQL 

templates/root.cnf.j2

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

handlers/main.yml

--- 
- name: Restart MySQL 
    service: name=mysql state=restarted 

Moja site.yml wyglądać następująco:

--- 
- hosts: all 
    become: yes 
    gather_facts: yes 
    roles: 
    - mysql 

Jeśli potrzebujesz pomocy, prosimy o zaznaczenie tego GitHub link. Dziękujemy