Install MySQL with ansible on ubuntu

前端 未结 2 1536
迷失自我
迷失自我 2020-12-13 02:23

I have a problem installing MySQL with ansible on a vagrant ubuntu,

This is my MySQL part

---
- name: Install MySQL
  apt:
    name: \"{{ item }}\"
          


        
2条回答
  •  暖寄归人
    2020-12-13 02:51

    Here is my complete working MySQL role, that might help you.

    vars/main.yml :

    mysql_root_pass: mypassword #MySQL Root Password
    

    asks/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
    

    site.yml

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

    If you need any help, please check this github link. Thanks

提交回复
热议问题