command line - Ansible idempotent MySQL installation Playbook -
i want setup mysql server on aws, using ansible configuration management. using default ami amazon (ami-3275ee5b), uses yum
package management.
when playbook below executed, goes well. when run second time, task configure root credentials
fails, because old password of mysql doesn't match anymore, since has been updated last time ran playbook.
this makes playbook non-idempotent, don't like. want able run playbook many times want.
- 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 mysql service action: service name=mysqld state=started - name: configure root credentials action: command mysqladmin -u root -p $mysql_root_password
what best way solve this, means make playbook idempotent? in advance!
i posted on coderwall, i'll reproduce dennisjac's improvement in comments of original post.
the trick doing idempotently knowing mysql_user module load ~/.my.cnf file if finds one.
i first change password, copy .my.cnf file password credentials. when try run second time, myqsl_user ansible module find .my.cnf , use 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 mysql service action: service name=mysqld state=started # 'localhost' needs last item idempotency, see # http://ansible.cc/docs/modules.html#mysql-user - name: update mysql root password 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 root password credentials template: src=templates/root/.my.cnf dest=/root/.my.cnf owner=root mode=0600
the .my.cnf template looks this:
[client] user=root password={{ mysql_root_password }}
edit: added privileges recommended dhananjay nene in comments, , changed variable interpolation use braces instead of dollar sign.
Comments
Post a Comment