-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathcreate-database.yml
More file actions
60 lines (60 loc) · 1.92 KB
/
Copy pathcreate-database.yml
File metadata and controls
60 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
- name: Install Postgresql for AWX
hosts: local
tasks:
- name: Start postgresql latest version
docker_container:
name: 'db'
image: systemdevformations/docker-postgres12
state: 'started'
env:
POSTGRES_PASSWORD: 'password' # Beware: in some version of postgres official release is POSTGRESQL_PASSWORD
ports:
- "5432:5432"
volumes:
- "/opt/postgres:/var/lib/postgresql/data"
#- "./sql:/tmp"
register: db_cont_metadata
- name: Get infos on container
docker_container_info:
name: 'db'
register: result
- name: Does container exist?
debug:
msg: "The container {{ 'exist' if result.exists else 'does not exist' }}"
- name: IP address
debug:
msg: "{{result.container.NetworkSettings.IPAddress}}"
- name: wait for postgres to accept connections
wait_for:
host: "{{ result.container.NetworkSettings.IPAddress }}"
port: 5432
state: 'drained' # will check for active connection
connect_timeout: 1
timeout: 30 # maximum number of seconds to wait for
register: postgresql_running
until: postgresql_running is success
retries: 10
- name: Add container db to in-memory inventory
add_host:
name: db
ansible_connection: docker
changed_when: false
- name: run command in container db
delegate_to: db
remote_user: postgres
raw: psql -l | grep tododb | wc -l
register: result
- name: Return value
debug:
msg: "{{ result.stdout | trim }}"
- name: Create database
delegate_to: db
remote_user: postgres
raw: psql -c 'create database tododb;'
when: result.stdout | trim | int == 0
- name: Insert data
delegate_to: db
remote_user: postgres
raw: psql tododb -f /tmp/todos.sql
when: result.stdout | trim | int == 0