Ansible idempotent MySQL installation Playbook

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

    I posted about this on coderwall, but I'll reproduce dennisjac's improvement in the comments of my original post.

    The trick to doing it idempotently is knowing that the mysql_user module will load a ~/.my.cnf file if it finds one.

    I first change the password, then copy a .my.cnf file with the password credentials. When you try to run it a second time, the myqsl_user ansible module will find the .my.cnf and use the new password.

    - 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 }} priv=*.*:ALL,GRANT
          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 dest=/root/.my.cnf owner=root mode=0600
    

    The .my.cnf template looks like this:

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

    Edit: Added privileges as recommended by Dhananjay Nene in the comments, and changed variable interpolation to use braces instead of dollar sign.

提交回复
热议问题