Ansible idempotent MySQL installation Playbook

后端 未结 11 1382
后悔当初
后悔当初 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:43

    It is important to start/re-start the mysql server prior to setting the root password. Also, I had tried everything posted up to this post [date] and discovered it is imperative to pass login_password and login_user.

    (i.e.) Any Plays after setting the mysql_user user:root and password= {{ SOMEPASSWORD }}, you must connect using login_password and login_user for any subsequent play.

    Note: The with_items below is based on what Ansible &/ MariaDB default hosts created.

    Example for Securing a MariaDB Server:

    ---
    # 'secure_mariadb.yml'
    
    - name: 'Ensure MariaDB server is started and enabled on boot'
      service: name={{ mariadb_service_name }} state=started enabled=yes
    
    # localhost needs to be the last item for idempotency, see
    # http://ansible.cc/docs/modules.html#mysql-user
    - name: 'Update Mysql Root Password'
      mysql_user: name=root
                  host={{ item }}
                  password={{ root_db_password }}
                  priv=*.*:ALL,GRANT
                  state=present
      with_items:
        - 127.0.0.1
        - ::1
        - instance-1 # Created by MariaDB to prevent conflicts between port and sockets if multi-instances running on the same computer.
        - localhost
    
    - name: 'Create MariaDB main configuration file'
      template: >
        src=my.cnf.j2
        dest=/etc/mysql/my.cnf
        owner=root
        group=root
        mode=0600
    
    - name: 'Ensure anonymous users are not in the database'
      mysql_user: login_user=root 
                  login_password={{ root_db_password }}
                  name=''
                  host={{ item }}
                  state=absent
      with_items:
        - 127.0.0.1
        - localhost
    
    - name: 'Remove the test database'
      mysql_db: login_user=root 
                login_password={{ root_db_password }}
                name=test
                state=absent
    
    - name: 'Reload privilege tables'
      command: 'mysql -ne "{{ item }}"'
      with_items:
        - FLUSH PRIVILEGES
      changed_when: False
    
    - name: 'Ensure MariaDB server is started and enabled on boot'
      service: name={{ mariadb_service_name }} state=started enabled=yes
    
    
    # 'End Of File'
    

提交回复
热议问题