使用Ansible任务运行SELECT查询

时间:2015-06-02 20:27:41

标签: mysql ansible ansible-playbook

在Ansbile的this mysql数据库模块列表中,有一个用于创建数据库或创建用户等。

我想针对预先存在的表运行查询,并使用该查询的结果填充Ansible变量(IP地址列表和节点类型),我将根据节点类型运行不同的任务

如何在Ansible中完成?

1 个答案:

答案 0 :(得分:33)

这大约是如何做到的(但未经测试):

- name: Retrieve stuff from mysql
  command: >
    mysql --user=alice --password=topsecret dbname
    --host=147.102.160.1 --batch --skip-column-names
    --execute="SELECT stuff from stuff_table"
  register: stuff
  always_run: True
  changed_when: False

- name: Do something with stuff
  debug: "{{ item }}"
  with_items: stuff.stdout_lines

记录here