简体   繁体   中英

Ansible playbook to create database on mariadb via maxscale

I have a 3 node mariadb cluster (PSS) which is managed by an maxscale. The below playbook I created can create database by connecting directly to my mariadb Primary (I pass my mariadb Primary ip in my inventory). But I want to connect to my maxscale and create database on my mariadb primary node. I tried to pass maxscale ip in my inventory hoping it would connect to maxscale and create database on my Primary node but it keeps failing.

---

- hosts: mysql
  become: yes

  vars:
    mysql_root_password: test_password

  tasks:
    - name: create new database
      mysql_db:
        name: ansible_db
        state: present
        login_user: test_user
        login_password: "{{ mysql_root_password }}"
        login_unix_socket: /var/lib/mysql/mysql.sock

My inventory:

[mysql]
10.XXX.XX.XXX ansible_user=ssh_user ansible_pass=ssh_passwd ansible_become=yes ansible_become_method=su

Error when I try to create via maxscale:

Exception message: (2003, \"Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)\")

The connection string we must use in the Maxscale server to connect to Mariadb is:

mysql -u<db_user> -p<db_password> -h<ip of maxscale server> -P<port no of readwrite split listener>

This can be replicated in ansible using the mysql_db module:

---

- hosts: mysql
     
  vars:
    mysql_root_password: <db_password>

  tasks:
   - name: create new database
      mysql_db:
        login_user: <db_user_name>
        login_password: "{{ mysql_root_password }}"
        login_host: <ip of maxscale server>
        login_port: <port no of readwrite split listener>
        name: <database name>
        state: present
      delegate_to: <ip of maxscale server>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM