6

I want to grant privilege on a db(test_db) to a user(test_user) using Ansible. My command is as shown below.

grant all PRIVILEGES on <test_db>.* to <test_user>@'localhost';

How will I execute the command using Ansible.

Pattu
  • 3,481
  • 8
  • 32
  • 41

2 Answers2

9

You could do it like this:

- name: Set mysql user privileges
  mysql_user:
    name=user_name
    priv="dbname.*:ALL"
    state=present

Of course you can interpolate variables, like the username, db name, etc...

Palantir
  • 23,820
  • 10
  • 76
  • 86
2
- name: "Create user {{ user }}"
  mysql_user:
    name: "{{ user }}"
    password: "{{ password }}"
    host: localhost
    state: present
    update_password: on_create
    priv: "{{ dbname }}.*:ALL"
    login_unix_socket: /var/run/mysqld/mysqld.sock

Solutions works

  • 1
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Jul 23 '19 at 15:49