j'aimerais plutot que tu utilises les modules d'ansible pour postgresql et mysql (si…
- User
j'aimerais plutot que tu utilises les modules d'ansible pour postgresql et mysql (si cela est possible) :Voici un playbook Ansible qui automatise l’exportation de données MySQL en fichiers CSV et l’importation de ces fichiers CSV dans une base de données PostgreSQL. Ce playbook suppose que vous avez des accès SSH configurés entre vos serveurs et que les configurations requises pour MySQL et PostgreSQL sont en place.
—
-
name: Automate MySQL to PostgreSQL Data Migration hosts: localhost vars: mysql_host: “mysql-server” mysql_user: “your_mysql_user” mysql_password: “your_mysql_password” mysql_database: “your_mysql_database”
postgresql_host: “postgresql-server” postgresql_user: “your_postgresql_user” postgresql_password: “your_postgresql_password” postgresql_database: “your_postgresql_database”
tables_to_migrate: - “table1” - “table2” # Add more tables as needed
tasks:
-
name: Export MySQL tables to CSV become: yes shell: > mysql --host={{ mysql_host }} --user={{ mysql_user }} --password={{ mysql_password }} {{ mysql_database }} -e “SELECT * INTO OUTFILE ‘/tmp/{{ item }}.csv’ FIELDS TERMINATED BY ‘,’ ENCLOSED BY '”’ LINES TERMINATED BY ‘\n’ FROM {{ item }}"> with_items: “{{ tables_to_migrate }}” delegate_to: “{{ mysql_host }}”
-
name: Copy CSV files to PostgreSQL server become: yes copy: src: “/tmp/{{ item }}.csv” dest: “/tmp/{{ item }}.csv” with_items: “{{ tables_to_migrate }}” delegate_to: “{{ postgresql_host }}”
-
name: Import CSV files into PostgreSQL become: yes shell: > PGPASSWORD={{ postgresql_password }} psql --host={{ postgresql_host }} --username={{ postgresql_user }} --dbname={{ postgresql_database }} -c “\COPY {{ item }} FROM ‘/tmp/{{ item }}.csv’ DELIMITER ‘,’ CSV HEADER” with_items: “{{ tables_to_migrate }}” delegate_to: “{{ postgresql_host }}”
-
name: Clean up temporary CSV files on MySQL server become: yes file: path: “/tmp/{{ item }}.csv” state: absent with_items: “{{ tables_to_migrate }}” delegate_to: “{{ mysql_host }}”
-
name: Clean up temporary CSV files on PostgreSQL server become: yes file: path: “/tmp/{{ item }}.csv” state: absent with_items: “{{ tables_to_migrate }}” delegate_to: “{{ postgresql_host }}”
-
Explication des étapes
- Exporter les tables MySQL en CSV
- Utilise la commande mysql pour exécuter une requête SELECT INTO OUTFILE afin d’exporter les tables spécifiées en fichiers CSV.
- Copier les fichiers CSV vers le serveur PostgreSQL
- Utilise le module copy d’Ansible pour transférer les fichiers CSV générés sur le serveur PostgreSQL.
- Importer les fichiers CSV dans PostgreSQL
-
- Assistant