Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ansible.builtin.command with command module #528

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: restart mysql
ansible.builtin.service:
service:
name: "{{ mysql_daemon }}"
state: restarted
sleep: 5
4 changes: 2 additions & 2 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

post_tasks:
- name: Make sure we can connect to MySQL via Unix socket.
ansible.builtin.command: "mysql -u root -proot -e 'show databases;'"
command: "mysql -u root -proot -e 'show databases;'"
changed_when: false

- name: Make sure we can connect to MySQL via TCP.
ansible.builtin.command: "mysql -u root -proot -h 127.0.0.1 -e 'show databases;'"
command: "mysql -u root -proot -h 127.0.0.1 -e 'show databases;'"
changed_when: false
20 changes: 10 additions & 10 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: Get MySQL version.
ansible.builtin.command: 'mysql --version'
command: 'mysql --version'
register: mysql_cli_version
changed_when: false
check_mode: false

- name: Copy my.cnf global MySQL configuration.
ansible.builtin.template:
template:
src: my.cnf.j2
dest: "{{ mysql_config_file }}"
owner: root
Expand All @@ -16,7 +16,7 @@
notify: restart mysql

- name: Verify mysql include directory exists.
ansible.builtin.file:
file:
path: "{{ mysql_config_include_dir }}"
state: directory
owner: root
Expand All @@ -25,7 +25,7 @@
when: mysql_config_include_files | length

- name: Copy my.cnf override files into include directory.
ansible.builtin.template:
template:
src: "{{ item.src }}"
dest: "{{ mysql_config_include_dir }}/{{ item.src | basename }}"
owner: root
Expand All @@ -36,13 +36,13 @@
notify: restart mysql

- name: Create slow query log file (if configured).
ansible.builtin.command: "touch {{ mysql_slow_query_log_file }}"
command: "touch {{ mysql_slow_query_log_file }}"
args:
creates: "{{ mysql_slow_query_log_file }}"
when: mysql_slow_query_log_enabled

- name: Create datadir if it does not exist
ansible.builtin.file:
file:
path: "{{ mysql_datadir }}"
state: directory
owner: mysql
Expand All @@ -51,7 +51,7 @@
setype: mysqld_db_t

- name: Set ownership on slow query log file (if configured).
ansible.builtin.file:
file:
path: "{{ mysql_slow_query_log_file }}"
state: file
owner: mysql
Expand All @@ -60,7 +60,7 @@
when: mysql_slow_query_log_enabled

- name: Create error log file (if configured).
ansible.builtin.command: "touch {{ mysql_log_error }}"
command: "touch {{ mysql_log_error }}"
args:
creates: "{{ mysql_log_error }}"
when:
Expand All @@ -69,7 +69,7 @@
tags: ['skip_ansible_galaxy']

- name: Set ownership on error log file (if configured).
ansible.builtin.file:
file:
path: "{{ mysql_log_error }}"
state: file
owner: mysql
Expand All @@ -81,7 +81,7 @@
tags: ['skip_ansible_galaxy']

- name: Ensure MySQL is started and enabled on boot.
ansible.builtin.service:
service:
name: "{{ mysql_daemon }}"
state: started
enabled: "{{ mysql_enabled_on_startup }}"
Expand Down
20 changes: 10 additions & 10 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
---
# Variable configuration.
- ansible.builtin.include_tasks: variables.yml
- include_tasks: variables.yml

# Setup/install tasks.
- ansible.builtin.include_tasks: setup-RedHat.yml
- include_tasks: setup-RedHat.yml
when: ansible_os_family == 'RedHat'

- ansible.builtin.include_tasks: setup-Debian.yml
- include_tasks: setup-Debian.yml
when: ansible_os_family == 'Debian'

- ansible.builtin.include_tasks: setup-Archlinux.yml
- include_tasks: setup-Archlinux.yml
when: ansible_os_family == 'Archlinux'

- name: Check if MySQL packages were installed.
ansible.builtin.set_fact:
set_fact:
mysql_install_packages: "{{ (rh_mysql_install_packages is defined and rh_mysql_install_packages.changed)
or (deb_mysql_install_packages is defined and deb_mysql_install_packages.changed)
or (arch_mysql_install_packages is defined and arch_mysql_install_packages.changed) }}"

# Configure MySQL.
- ansible.builtin.include_tasks: configure.yml
- ansible.builtin.include_tasks: secure-installation.yml
- ansible.builtin.include_tasks: databases.yml
- ansible.builtin.include_tasks: users.yml
- ansible.builtin.include_tasks: replication.yml
- include_tasks: configure.yml
- include_tasks: secure-installation.yml
- include_tasks: databases.yml
- include_tasks: users.yml
- include_tasks: replication.yml
14 changes: 7 additions & 7 deletions tasks/secure-installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Has to be after the password assignment, for idempotency.
- name: Copy user-my.cnf file with password credentials.
ansible.builtin.template:
template:
src: "user-my.cnf.j2"
dest: "{{ mysql_user_home }}/.my.cnf"
owner: "{{ mysql_user_name }}"
Expand All @@ -20,13 +20,13 @@
and (mysql_install_packages | bool or mysql_user_password_update)

- name: Disallow root login remotely
ansible.builtin.command: 'mysql -NBe "{{ item }}"'
command: 'mysql -NBe "{{ item }}"'
with_items:
- DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
changed_when: false

- name: Get list of hosts for the root user.
ansible.builtin.command: mysql -NBe
command: mysql -NBe
"SELECT Host
FROM mysql.user
WHERE User = '{{ mysql_root_username }}'
Expand All @@ -40,7 +40,7 @@
# the root password correctly. See: https://goo.gl/MSOejW
# Set root password for MySQL >= 5.7.x.
- name: Update MySQL root password for localhost root account (5.7.x).
ansible.builtin.shell: >
shell: >
mysql -u root -NBe
"ALTER USER '{{ mysql_root_username }}'@'{{ item }}'
IDENTIFIED WITH mysql_native_password BY '{{ mysql_root_password }}'; FLUSH PRIVILEGES;"
Expand All @@ -51,7 +51,7 @@

# Set root password for MySQL < 5.7.x.
- name: Update MySQL root password for localhost root account (< 5.7.x).
ansible.builtin.shell: >
shell: >
mysql -NBe
'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}"); FLUSH PRIVILEGES;'
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
Expand All @@ -61,7 +61,7 @@

# Has to be after the root password assignment, for idempotency.
- name: Copy .my.cnf file with root password credentials.
ansible.builtin.template:
template:
src: "root-my.cnf.j2"
dest: "{{ mysql_root_home }}/.my.cnf"
owner: root
Expand All @@ -70,7 +70,7 @@
when: mysql_install_packages | bool or mysql_root_password_update

- name: Get list of hosts for the anonymous user.
ansible.builtin.command: mysql -NBe "SELECT Host FROM mysql.user WHERE User = ''"
command: mysql -NBe "SELECT Host FROM mysql.user WHERE User = ''"
register: mysql_anonymous_hosts
changed_when: false
check_mode: false
Expand Down
2 changes: 1 addition & 1 deletion tasks/setup-Archlinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
register: arch_mysql_install_packages

- name: Run mysql_install_db if MySQL packages were changed.
ansible.builtin.command: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
command: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
when: arch_mysql_install_packages.changed
tags: ['skip_ansible_lint']
12 changes: 6 additions & 6 deletions tasks/setup-Debian.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
- name: Check if MySQL is already installed.
ansible.builtin.stat:
stat:
path: "{{ mysql_config_file }}"
register: mysql_installed

- name: Update apt cache if MySQL is not yet installed.
ansible.builtin.apt:
apt:
update_cache: yes
changed_when: False
when: not mysql_installed.stat.exists

- name: Ensure MySQL Python libraries are installed.
ansible.builtin.apt:
apt:
name: "{{ mysql_python_package_debian }}"
state: present

- name: Ensure MySQL packages are installed.
ansible.builtin.apt:
apt:
name: "{{ mysql_packages }}"
state: present
policy_rc_d: 101
Expand All @@ -25,13 +25,13 @@
# Because Ubuntu starts MySQL as part of the install process, we need to stop
# mysql and remove the logfiles in case the user set a custom log file size.
- name: Ensure MySQL is stopped after initial install.
ansible.builtin.service:
service:
name: "{{ mysql_daemon }}"
state: stopped
when: not mysql_installed.stat.exists

- name: Delete innodb log files created by apt package after initial install.
ansible.builtin.file:
file:
path: "{{ mysql_datadir }}/{{ item }}"
state: absent
with_items:
Expand Down
2 changes: 1 addition & 1 deletion tasks/setup-RedHat.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Ensure MySQL packages are installed.
ansible.builtin.yum:
yum:
name: "{{ mysql_packages }}"
state: present
enablerepo: "{{ mysql_enablerepo | default(omit, true) }}"
Expand Down
22 changes: 11 additions & 11 deletions tasks/variables.yml
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
---
# Variable configuration.
- name: Include OS-specific variables.
ansible.builtin.include_vars: "{{ item }}"
include_vars: "{{ item }}"
with_first_found:
- files:
- "vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
- "vars/{{ ansible_os_family }}.yml"
skip: true

- name: Define mysql_packages.
ansible.builtin.set_fact:
set_fact:
mysql_packages: "{{ __mysql_packages | list }}"
when: mysql_packages is not defined

- name: Define mysql_daemon.
ansible.builtin.set_fact:
set_fact:
mysql_daemon: "{{ __mysql_daemon }}"
when: mysql_daemon is not defined

- name: Define mysql_slow_query_log_file.
ansible.builtin.set_fact:
set_fact:
mysql_slow_query_log_file: "{{ __mysql_slow_query_log_file }}"
when: mysql_slow_query_log_file is not defined

- name: Define mysql_log_error.
ansible.builtin.set_fact:
set_fact:
mysql_log_error: "{{ __mysql_log_error }}"
when: mysql_log_error is not defined

- name: Define mysql_syslog_tag.
ansible.builtin.set_fact:
set_fact:
mysql_syslog_tag: "{{ __mysql_syslog_tag }}"
when: mysql_syslog_tag is not defined

- name: Define mysql_pid_file.
ansible.builtin.set_fact:
set_fact:
mysql_pid_file: "{{ __mysql_pid_file }}"
when: mysql_pid_file is not defined

- name: Define mysql_config_file.
ansible.builtin.set_fact:
set_fact:
mysql_config_file: "{{ __mysql_config_file }}"
when: mysql_config_file is not defined

- name: Define mysql_config_include_dir.
ansible.builtin.set_fact:
set_fact:
mysql_config_include_dir: "{{ __mysql_config_include_dir }}"
when: mysql_config_include_dir is not defined

- name: Define mysql_socket.
ansible.builtin.set_fact:
set_fact:
mysql_socket: "{{ __mysql_socket }}"
when: mysql_socket is not defined

- name: Define mysql_supports_innodb_large_prefix.
ansible.builtin.set_fact:
set_fact:
mysql_supports_innodb_large_prefix: "{{ __mysql_supports_innodb_large_prefix }}"
when: mysql_supports_innodb_large_prefix is not defined