Ansible idempotent MySQL installation Playbook

后端 未结 11 1605
后悔当初
后悔当初 2021-01-30 04:29

I want to setup a MySQL server on AWS, using Ansible for the configuration management. I am using the default AMI from Amazon (ami-3275ee5b), which uses yum

11条回答
  •  误落风尘
    2021-01-30 04:32

    Ansible version for a secure MySQL installation.

    mysql_secure_installation.yml

    - hosts: staging_mysql
      user: ec2-user
      sudo: yes
    
      tasks:
        - name: Install MySQL
          action: yum name={{ item }}
          with_items:
            - MySQL-python
            - mysql
            - mysql-server
    
        - name: Start the MySQL service
          action: service name=mysqld state=started
    
        # '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 }}
          with_items:
            - "{{ ansible_hostname }}"
            - 127.0.0.1
            - ::1
            - localhost
    
        - name: copy .my.cnf file with root password credentials
          template: src=templates/root/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
    
        - name: delete anonymous MySQL server user for $server_hostname
          action: mysql_user user="" host="{{ server_hostname }}" state="absent"
    
        - name: delete anonymous MySQL server user for localhost
          action: mysql_user user="" state="absent"
    
        - name: remove the MySQL test database
          action: mysql_db db=test state=absent
    

    templates/root/my.cnf.j2

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

    References

    • The original answer by Lorin Hochstein
    • https://github.com/gaspaio/ansible-devbox/blob/master/roles/mysql/tasks/server.yml

提交回复
热议问题