Note
This module is part of the community.mysql collection (version 3.15.0).
You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.mysql. You need further requirements to be able to use this module, see Requirements for details.
To use it in a playbook, specify: community.mysql.mysql_replication.
The below requirements are needed on the host that executes this module.
Parameter | Comments |
|---|---|
ca_cert aliases: ssl_ca path | The path to a Certificate Authority (CA) certificate. This option, if used, must specify the same certificate as used by the server. |
channel string added in community.mysql 0.1.0 | Name of replication channel. Multi-source replication is supported from MySQL 5.7. Mutually exclusive with connection_name. For more information see https://dev.mysql.com/doc/refman/8.0/en/replication-multi-source.html. |
check_hostname boolean added in community.mysql 1.1.0 | Whether to validate the server host name when an SSL connection is required. Corresponds to MySQL CLIs Setting this to Requires pymysql >= 0.7.11. Choices:
|
client_cert aliases: ssl_cert path | The path to a client public key certificate. |
client_key aliases: ssl_key path | The path to the client private key. |
config_file path | Specify a config file from which user and password are to be read. The default config file, The default config file, To prevent the default config file from being read, set config_file to be an empty string. Default: |
connect_timeout integer | The connection timeout when connecting to the MySQL server. Default: |
connection_name string added in community.mysql 0.1.0 | Name of the primary connection. Supported from MariaDB 10.0.1. Mutually exclusive with channel. For more information see https://mariadb.com/kb/en/library/multi-source-replication/. |
fail_on_error boolean added in community.mysql 0.1.0 | Fails on error when calling mysql. Choices:
|
login_host string | Host running the database. In some cases for local connections the login_unix_socket=/path/to/mysqld/socket, that is usually Default: |
login_password string | The password used to authenticate with. |
login_port integer | Port of the MySQL server. Requires login_host be defined as other than localhost if login_port is used. Default: |
login_unix_socket string | The path to a Unix domain socket for local connections. Use this parameter to avoid the |
login_user string | The username used to authenticate with. |
mode string | Module operating mode. Could be Choices:
|
primary_auto_position aliases: master_auto_position boolean | Whether the host uses GTID based replication or not. Same as the Choices:
|
primary_connect_retry aliases: master_connect_retry integer | Same as the |
primary_delay aliases: master_delay integer added in community.mysql 0.1.0 | Time lag behind the primary’s state (in seconds). Same as the Available from MySQL 5.6. For more information see https://dev.mysql.com/doc/refman/8.0/en/replication-delayed.html. |
primary_host aliases: master_host string | Same as the |
primary_log_file aliases: master_log_file string | Same as the |
primary_log_pos aliases: master_log_pos integer | Same as the |
primary_password aliases: master_password string | Same as the |
primary_port aliases: master_port integer | Same as the |
primary_ssl aliases: master_ssl boolean | Same as the When setting it to For details, refer to MySQL encrypted replication documentation. The default is Choices:
|
primary_ssl_ca aliases: master_ssl_ca string | Same as the For details, refer to MySQL encrypted replication documentation. |
primary_ssl_capath aliases: master_ssl_capath string | Same as the For details, refer to MySQL encrypted replication documentation. |
primary_ssl_cert aliases: master_ssl_cert string | Same as the For details, refer to MySQL encrypted replication documentation. |
primary_ssl_cipher aliases: master_ssl_cipher string | Same as the Specifies a colon-separated list of one or more ciphers permitted by the replica for the replication connection. For details, refer to MySQL encrypted replication documentation. |
primary_ssl_key aliases: master_ssl_key string | Same as the For details, refer to MySQL encrypted replication documentation. |
primary_ssl_verify_server_cert boolean added in community.mysql 3.5.0 | Same as The module switch automatically to Prior to community.mysql 3.14.0 Choices:
|
primary_use_gtid aliases: master_use_gtid string added in community.mysql 0.1.0 | Configures the replica to use the MariaDB Global Transaction ID.
To find information about available values see https://mariadb.com/kb/en/library/change-master-to/#master_use_gtid. Available since MariaDB 10.0.2. Choices:
|
primary_user aliases: master_user string | Same as the |
relay_log_file string | Same as mysql variable. |
relay_log_pos integer | Same as mysql variable. |
Attribute | Support | Description |
|---|---|---|
check_mode | Support: none | Can run in check_mode and return changed status prediction without modifying target. |
Note
ansible_python_interpreter. For more information, see https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html.login_password and login_user are required when you are passing credentials. If none are present, the module will attempt to read the credentials from ~/.my.cnf, and finally fall back to using the MySQL default login of ‘root’ with no password./root/.my.cnf) are not needed to connect to a database server, but the file exists and does not contain a [client] section, before any other valid directives, it will be read and this will cause the connection to fail, to prevent this set it to an empty string, (for example config_file: '').Please explicitly state intended protocol error, use the login_unix_socket argument, for example, login_unix_socket: /run/mysqld/mysqld.sock.~/.my.cnf) on the destination host, for example socket=/var/lib/mysql/mysql.sock.See also
Gather information about MySQL or MariaDB servers.
Complete reference of the MySQL replication documentation.
Setting up MySQL replication to use encrypted connection.
Complete reference of the MariaDB replication documentation.
# If you encounter the "Please explicitly state intended protocol" error,
# use the login_unix_socket argument
- name: Stop mysql replica thread
community.mysql.mysql_replication:
mode: stopreplica
login_unix_socket: /run/mysqld/mysqld.sock
- name: Get primary binlog file name and binlog position
community.mysql.mysql_replication:
mode: getprimary
- name: Change primary to primary server 192.0.2.1 and use binary log 'mysql-bin.000009' with position 4578
community.mysql.mysql_replication:
mode: changeprimary
primary_host: 192.0.2.1
primary_log_file: mysql-bin.000009
primary_log_pos: 4578
- name: Change replication source to replica server 192.0.2.1 and use binary log 'mysql-bin.000009' with position 4578
community.mysql.mysql_replication:
mode: changereplication
primary_host: 192.0.2.1
primary_log_file: mysql-bin.000009
primary_log_pos: 4578
- name: Check replica status using port 3308
community.mysql.mysql_replication:
mode: getreplica
login_host: ansible.example.com
login_port: 3308
- name: On MariaDB change primary to use GTID current_pos
community.mysql.mysql_replication:
mode: changeprimary
primary_use_gtid: current_pos
- name: Change primary to use replication delay 3600 seconds
community.mysql.mysql_replication:
mode: changeprimary
primary_host: 192.0.2.1
primary_delay: 3600
- name: Start MariaDB replica with connection name primary-1
community.mysql.mysql_replication:
mode: startreplica
connection_name: primary-1
- name: Stop replication in channel primary-1
community.mysql.mysql_replication:
mode: stopreplica
channel: primary-1
- name: >
Run RESET MASTER command which will delete all existing binary log files
and reset the binary log index file on the primary
community.mysql.mysql_replication:
mode: resetprimary
- name: Run start replica and fail the task on errors
community.mysql.mysql_replication:
mode: startreplica
connection_name: primary-1
fail_on_error: true
- name: Change primary and fail on error (like when replica thread is running)
community.mysql.mysql_replication:
mode: changeprimary
fail_on_error: true
Common return values are documented here, the following are the fields unique to this module:
Key | Description |
|---|---|
queries list / elements=string added in community.mysql 0.1.0 | List of executed queries which modified DB’s state. Returned: always Sample: |
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/community/mysql/mysql_replication_module.html