Skip to content

Commit 3c24e28

Browse files
committed
init ansible role for mysql db and user creation
1 parent 20677a5 commit 3c24e28

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
webspace_id: 1
2+
dbtype: "mysql"

meta/main.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
dependencies: []
3+
4+
galaxy_info:
5+
author: susgo, kukshaus
6+
description: Add mysql db and mysql user for Plesk
7+
company: "ITEXIA GmbH"
8+
license: "license (MIT)"
9+
min_ansible_version: 2.3
10+
platforms:
11+
- name: Linux (Ubuntu, Debian) running Plesk
12+
versions:
13+
- all
14+
galaxy_tags:
15+
- plesk
16+
- cloud
17+
- setup
18+
- hosting
19+
- sysadmin
20+
- mysql
21+
- database
22+
- dtabase user

tasks/main.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
- name: "Copy and fill template for creating new db"
2+
template:
3+
src: "templates/create-db.xml.j2"
4+
dest: "/tmp/create-db.xml"
5+
mode: 0777
6+
delegate_to: localhost
7+
8+
- name: "Create new db {{ dbname }} on strato server with plesk"
9+
uri:
10+
url: "https://{{ host_url }}:{{ host_port }}/enterprise/control/agent.php"
11+
method: POST
12+
user: "{{ plesk_admin_user }}"
13+
password: "{{ plesk_admin_pw }}"
14+
body: "{{ lookup('file','/tmp/create-db.xml') }}"
15+
return_content: yes
16+
follow_redirects: all
17+
force_basic_auth: yes
18+
body_format: raw
19+
validate_certs: no
20+
headers:
21+
Content-Type: "text/xml"
22+
HTTP_AUTH_LOGIN: "{{ plesk_admin_user }}"
23+
HTTP_AUTH_PASSWD: "{{ plesk_admin_pw }}"
24+
HTTP_PRETTY_PRINT: TRUE
25+
timeout: 120
26+
register: dbcreate_response
27+
dest: "/tmp/log_create_database_{{ dbname }}.txt"
28+
failed_when: '("error" in dbcreate_response.content)'
29+
register: create_new_db_response
30+
31+
- name: "create new db response file"
32+
copy: content="{{ create_new_db_response.content }}" dest="/tmp/create_db_for_{{ dbname }}.txt"
33+
34+
- name: Read attribute value from create new db response file
35+
xml:
36+
path: /tmp/create_db_for_{{ dbname }}.txt
37+
xpath: /packet/database/add-db/result/id
38+
content: text
39+
register: new_db_id
40+
41+
- name: "Delete template for instance post"
42+
file:
43+
path: "/tmp/create-db.xml"
44+
state: absent
45+
delegate_to: localhost
46+
47+
- name: "Copy and fill template for creating new db user"
48+
template:
49+
src: "templates/create-db-user.xml.j2"
50+
dest: "/tmp/create-db-user.xml"
51+
mode: 0777
52+
delegate_to: localhost
53+
54+
- name: "Create new db user {{ dbuser }} for {{ dbname }} on strato server with plesk"
55+
uri:
56+
url: "https://{{ host_url }}:{{ host_port }}/enterprise/control/agent.php"
57+
method: POST
58+
user: "{{ plesk_admin_user }}"
59+
password: "{{ plesk_admin_pw }}"
60+
body: "{{ lookup('file','/tmp/create-db-user.xml') }}"
61+
return_content: yes
62+
follow_redirects: all
63+
force_basic_auth: yes
64+
body_format: raw
65+
validate_certs: no
66+
headers:
67+
Content-Type: "text/xml"
68+
HTTP_AUTH_LOGIN: "{{ plesk_admin_user }}"
69+
HTTP_AUTH_PASSWD: "{{ plesk_admin_pw }}"
70+
HTTP_PRETTY_PRINT: TRUE
71+
timeout: 120
72+
register: create_dbuser_response
73+
dest: "/tmp/log_create_dbuser_{{ dbuser }}.txt"
74+
failed_when: '("error" in create_dbuser_response.content)'
75+
76+
- name: "Delete template for instance post"
77+
file:
78+
path: "/tmp/create-db-user.xml"
79+
state: absent
80+
delegate_to: localhost

templates/create-db-user.xml.j2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<packet>
3+
<database>
4+
<add-db-user>
5+
<db-id>{{new_db_id.matches.0.id}}</db-id>
6+
<login>{{ dbuser }}</login>
7+
<password>{{ dbpw }}</password>
8+
</add-db-user>
9+
</database>
10+
</packet>

templates/create-db.xml.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<packet>
2+
<database>
3+
<add-db>
4+
<webspace-id>{{ webspace_id }}</webspace-id>
5+
<name>{{ dbname | regex_replace("\.", "") }}</name>
6+
<type>{{ dbtype }}</type>
7+
</add-db>
8+
</database>
9+
</packet>

vars/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
host_url: <your-strato-server-id>.stratoserver.net
2+
host_port: 8443
3+
4+
dbname: "dbmydomain"
5+
dbuser: "dbuser"
6+
dbpw: "secure-db-pw"
7+
8+
plesk_admin_user: "admin"
9+
plesk_admin_pw: "secure-admin-pw"

0 commit comments

Comments
 (0)