Running a SELECT Query with an Ansible Task

2020-06-07 07:03发布

问题:

In this list of mysql db modules for Ansbile, there's one for creating a db, or creating a user, etc.

I would like to run a query against a pre-existing table and use the results of that query to populate an Ansible variable (list of IP addresses, and node type) upon which I would run different tasks, depending on node type.

How can that be done in Ansible?

回答1:

This is approximately how to do it (but it is untested):

- 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
  check_mode: no
  changed_when: False

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

Documented here.