3

Suppose you have a sql-file database.sql, which creates the database schema, the table within it and some initial filling. Normally, I can use ansible to make this database by:

---

- name: copy database.sql to server
  template: src=database.sql dest=/tmp/database.sql owner=root group=root

- name: sql the thing into MySQL
  command: mysql "-u root -p{{ mysql_root_password }} < /tmp/database.sql"

- name: remove the sql file from the server
  command: rm -f /tmp/database.sql

and this does exactly what it says. But when database.sql is large (perhaps 2 TByte) you really don't want the copying action first. Are there ways to refer to database.sql as a file on the ansible-master server (where we push it from) such that you can do a mysql -u root@master -p... < "local file" such that the copy action isn't needed anymore ?

4

1 回答 1

8

无论您做什么,数据都必须从它所在的位置流向数据库服务器。请注意,在您的示例中,您应该使用copy而不是template: Jinja 正在解析模板,并且解析大文件的成本非常高。

话虽如此,您可能想要使用local_action并远程提供您的 mysql 服务器:

- name: feed database.sql to server
  local_action: shell mysql -u root -p{{ mysql_root_password }} -h {{ ansible_default_ipv4 }} < /tmp/database.sql

这将在本地运行命令,并且应该在以下情况下工作:

  • 您的数据库转储在/tmp/database.sql本地机器上
  • /tmp/database.sql包含数据库创建语句
  • 你可以远程连接到你的mysql服务器(提示:检查权限+绑定地址)

为了使事情更清洁,您可以使用该mysql_db模块:

- name: feed database.sql to server
  local_action: mysql_db login_user=root login_password={{ mysql_root_password }} login_host={{ ansible_default_ipv4 }} name={{ db_name }} state=import target=/tmp/database.sql
于 2014-02-20T14:07:20.447 回答