From 181aef980669dbf316236c85c29886b76f99744c Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 8 Nov 2014 15:10:49 -0600 Subject: [PATCH] Stylistic cleanup and reorganization. --- handlers/main.yml | 4 +- meta/main.yml | 1 + tasks/configure.yml | 12 ++++++ tasks/databases-users.yml | 17 ++++++++ tasks/main.yml | 73 ++--------------------------------- tasks/secure-installation.yml | 32 +++++++++++++++ tasks/setup-Debian.yml | 4 +- tasks/setup-RedHat.yml | 9 +++-- 8 files changed, 72 insertions(+), 80 deletions(-) create mode 100644 tasks/configure.yml create mode 100644 tasks/databases-users.yml create mode 100644 tasks/secure-installation.yml diff --git a/handlers/main.yml b/handlers/main.yml index 8622c20..24b351d 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,5 +1,3 @@ --- - name: restart mysql - service: > - name={{ mysql_daemon }} - state=restarted + service: "name={{ mysql_daemon }} state=restarted" diff --git a/meta/main.yml b/meta/main.yml index c66da60..6436e07 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -11,6 +11,7 @@ galaxy_info: - name: EL versions: - 6 + - 7 - name: Ubuntu versions: - all diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..008a354 --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,12 @@ +--- +- name: Copy my.cnf global MySQL configuration. + template: + src: my.cnf.j2 + dest: /etc/my.cnf + owner: root + group: root + mode: 0644 + notify: restart mysql + +- name: Ensure MySQL is started and enabled on boot. + service: "name={{ mysql_daemon }} state=started enabled=yes" diff --git a/tasks/databases-users.yml b/tasks/databases-users.yml new file mode 100644 index 0000000..23b013a --- /dev/null +++ b/tasks/databases-users.yml @@ -0,0 +1,17 @@ +--- +- name: Ensure MySQL databases are present. + mysql_db: + name: "{{ item.name }}" + collation: "{{ item.collation | default('utf8_general_ci') }}" + encoding: "{{ item.encoding | default('utf8') }}" + state: present + with_items: mysql_databases + +- name: Ensure MySQL users are present. + mysql_user: + name: "{{ item.name }}" + host: "{{ item.host | default('localhost') }}" + password: "{{ item.password }}" + priv: "{{ item.priv | default('*.*:USAGE') }}" + state: present + with_items: mysql_users diff --git a/tasks/main.yml b/tasks/main.yml index 1624aa0..5dc2614 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,79 +2,12 @@ - name: Include OS-specific variables. include_vars: "{{ ansible_os_family }}.yml" -- name: Update postfix to the latest version (if extra repositories enabled). - yum: > - name=postfix - state=latest - enablerepo={{ mysql_enablerepo }} - when: mysql_enablerepo != "" - - include: setup-RedHat.yml when: ansible_os_family == 'RedHat' - include: setup-Debian.yml when: ansible_os_family == 'Debian' -- name: Copy my.cnf global MySQL configuration. - template: > - src=my.cnf.j2 - dest=/etc/my.cnf - owner=root group=root mode=644 - notify: restart mysql - -- name: Ensure MySQL is started and enabled on boot. - service: > - name={{ mysql_daemon }} - state=started - enabled=yes - -- name: Check if .my.cnf file already exists. - stat: "path={{ mysql_user_home }}/.my.cnf" - register: mycnf_file - -# '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: - - 127.0.0.1 - - ::1 - - localhost - when: mycnf_file.stat.exists == false - -# Has to be after the root password assignment, for idempotency. -- name: Copy .my.cnf file with root password credentials. - template: > - src=python-my.cnf.j2 - dest={{ mysql_user_home }}/.my.cnf - owner=root group=root mode=600 - -- name: Delete anonymous MySQL user for localhost. - mysql_user: > - name="" - state=absent - -- name: Remove the MySQL test database. - mysql_db: > - name="test" - state=absent - -- name: Ensure MySQL databases are present. - mysql_db: > - name="{{ item.name }}" - collation="{{ item.collation | default('utf8_general_ci') }}" - encoding="{{ item.encoding | default('utf8') }}" - state=present - with_items: mysql_databases - -- name: Ensure MySQL users are present. - mysql_user: > - name="{{ item.name }}" - host="{{ item.host | default('localhost') }}" - password="{{ item.password }}" - priv="{{ item.priv | default('*.*:USAGE') }}" - state=present - with_items: mysql_users +- include: configure.yml +- include: secure-installation.yml +- include: databases-users.yml diff --git a/tasks/secure-installation.yml b/tasks/secure-installation.yml new file mode 100644 index 0000000..db44d50 --- /dev/null +++ b/tasks/secure-installation.yml @@ -0,0 +1,32 @@ +--- +- name: Check if .my.cnf file already exists. + stat: "path={{ mysql_user_home }}/.my.cnf" + register: mycnf_file + +# '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: + - 127.0.0.1 + - ::1 + - localhost + when: mycnf_file.stat.exists == false + +# Has to be after the root password assignment, for idempotency. +- name: Copy .my.cnf file with root password credentials. + template: + src: "python-my.cnf.j2" + dest: "{{ mysql_user_home }}/.my.cnf" + owner: root + group: root + mode: 0600 + +- name: Remove anonymous MySQL user. + mysql_user: "name='' state=absent" + +- name: Remove MySQL test database. + mysql_db: "name='test' state=absent" \ No newline at end of file diff --git a/tasks/setup-Debian.yml b/tasks/setup-Debian.yml index 425bc17..3cfe518 100644 --- a/tasks/setup-Debian.yml +++ b/tasks/setup-Debian.yml @@ -8,7 +8,5 @@ when: mysql_installed.stat.exists == false - name: Ensure MySQL packages are installed. - apt: > - name={{ item }} - state=installed + apt: "name={{ item }} state=installed" with_items: mysql_packages diff --git a/tasks/setup-RedHat.yml b/tasks/setup-RedHat.yml index 6265326..dc71e2f 100644 --- a/tasks/setup-RedHat.yml +++ b/tasks/setup-RedHat.yml @@ -1,7 +1,8 @@ --- +- name: Update postfix to the latest version (if extra repositories enabled). + yum: "name=postfix state=latest enablerepo={{ mysql_enablerepo }}" + when: mysql_enablerepo != "" + - name: Ensure MySQL packages are installed. - yum: > - name={{ item }} - state=installed - enablerepo={{ mysql_enablerepo }} + yum: "name={{ item }} state=installed enablerepo={{ mysql_enablerepo }}" with_items: mysql_packages