Ansible idempotent MySQL installation Playbook

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

    I'm adding my own take on the various approaches (centos 7).

    The variable mysql_root_password should be stored in an ansible-vault (better) or passed on the command-line (worse)

    - name: "Ensure mariadb packages are installed"
      yum: name={{ item }} state="present"
      with_items:
        - mariadb
        - mariadb-server
    
    - name: "Ensure mariadb is running and configured to start at boot"
      service: name=mariadb state=started enabled=yes
    
    # idempotently ensure secure mariadb installation --
    # - attempts to connect as root user with no password and then set the root@ mysql password for each mysql root user mode.
    # - ignore_errors is true because this task will always fail on subsequent runs (as the root user password has been changed from "")
    - name: Change root user password on first run, this will only succeed (and only needs to succeed) on first playbook run
      mysql_user: login_user=root
                  login_password=''
                  name=root
                  password={{ mysql_root_password }}
                  priv=*.*:ALL,GRANT
                  host={{ item }}
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost
      ignore_errors: true
    
    - name: Ensure the anonymous mysql user ""@{{ansible_hostname}} is deleted
      action: mysql_user user="" host="{{ ansible_hostname }}" state="absent" login_user=root login_password={{ mysql_root_password }}
    
    - name: Ensure the anonymous mysql user ""@localhost is deleted
      action: mysql_user user="" state="absent" login_user=root login_password={{ sts_ad_password }}
    
    - name: Ensure the mysql test database is deleted
      action: mysql_db db=test state=absent login_user=root login_password={{ mysql_root_password }}
    

提交回复
热议问题