Skip to content

Commit d85cb46

Browse files
committed
Role to install postgresql
0 parents  commit d85cb46

25 files changed

+1156
-0
lines changed

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
ANSIBLE Role for postgreSQL
2+
===========================
3+
4+
This role can be used to install postgreSQL on LINUX Server including Ubuntu, centOS etc., This role is customizable, it support basic functionalities like creating database and users. Users can modify the role as per there requirements.
5+
6+
Requirements
7+
------------
8+
9+
There is no such requirements for the role.
10+
11+
Role Variables
12+
--------------
13+
14+
For now these are the varibales which we are using in our role, which are pretty much basic and are self defining,
15+
# use this option when want to create database (true/ false)
16+
postgresql_database:
17+
# define the version here (11, 10, 9.4, 9.5, 9.6)
18+
version:
19+
# use this option when want to create user (true/ false)
20+
postgresql_users:
21+
# when creating user, provide list of users here,
22+
user_list:
23+
# when creating database, provide list of databases here,
24+
database_list:
25+
# user & group of the postgreSQL
26+
user:
27+
group:
28+
29+
Dependencies
30+
------------
31+
32+
There are no such dependencies,
33+
34+
Example Playbook
35+
----------------
36+
37+
Sample playbook will look something like this,
38+
39+
- hosts: servers
40+
roles:
41+
- osm_postgresql
42+
43+
Testing
44+
------------
45+
46+
To test using molecule, define package to install and user list and database list in test_default.py
47+
48+
Author Information
49+
------------------
50+
51+
Ishan Ji Gupta (ishan.gupta@opstree.com)

defaults/main.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
postgresql_database: true
3+
version: 9.5
4+
postgresql_users: true
5+
user_list_attr: {attr: CREATEROLE, INHERIT, BYPASSRLS, CREATEDB}
6+
user_list:
7+
- {user: demo1, password: password1, -user_list_attr}
8+
- {user: demo2, password: password2, attr: CREATEROLE, INHERIT}
9+
database_list:
10+
- {db: test1}
11+
- {db: test2}
12+
user: postgres
13+
group: postgres
14+
...

handlers/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: restart postgresql
3+
service:
4+
name: postgresql-{{ version }}
5+
state: restarted
6+
...

meta/main.yml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
galaxy_info:
3+
author: Abhishek Vishwakarma
4+
description: Install nginx and configure Virtual Hosting
5+
company: Opstree Solutions
6+
7+
# If the issue tracker for your role is not on github, uncomment the
8+
# next line and provide a value
9+
# issue_tracker_url: http://example.com/issue/tracker
10+
11+
# Some suggested licenses:
12+
# - BSD (default)
13+
# - MIT
14+
# - GPLv2
15+
# - GPLv3
16+
# - Apache
17+
# - CC-BY
18+
license: license (GPLv2)
19+
20+
min_ansible_version: 2.3
21+
22+
# If this a Container Enabled role,
23+
# provide the minimum Ansible Container version.
24+
# min_ansible_container_version:
25+
26+
# Optionally specify the branch Galaxy will use when accessing the GitHub
27+
# repo for this role. During role install, if no tags are available,
28+
# Galaxy will use this branch. During import Galaxy will access files on
29+
# this branch.If Travis integration is configured, only notifications for this
30+
# branch will be accepted. Otherwise, in all cases, the repo's default branch
31+
# (usually master) will be used.
32+
# github_branch:
33+
34+
#
35+
# platforms is a list of platforms,
36+
# and each platform has a name and a list of versions.
37+
#
38+
platforms:
39+
- name: Centos
40+
versions:
41+
- 6
42+
- name: Ubuntu
43+
versions:
44+
- 16
45+
46+
galaxy_tags: []
47+
# List tags for your role here, one per line.
48+
# A tag is a keyword that describes
49+
# and categorizes the role. Users find roles by searching for tags.
50+
# Be sure to remove the '[]' above, if you add tags to this list.
51+
#
52+
# NOTE: A tag is limited to a single word
53+
# comprised of alphanumeric characters.
54+
# Maximum 20 tags per role.
55+
56+
dependencies: []
57+
# List your role dependencies here, one per line.
58+
# Be sure to remove the '[]' above,
59+
# if you add dependencies to this list.

molecule/default/Dockerfile.j2

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Molecule managed
2+
3+
{% if item.registry is defined %}
4+
FROM {{ item.registry.url }}/{{ item.image }}
5+
{% else %}
6+
FROM {{ item.image }}
7+
{% endif %}
8+
9+
{% if item.env is defined %}
10+
{% for var, value in item.env.items() %}
11+
{% if value %}
12+
ENV {{ var }} {{ value }}
13+
{% endif %}
14+
{% endfor %}
15+
{% endif %}
16+
17+
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \
18+
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \
19+
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
20+
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \
21+
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
22+
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi

molecule/default/INSTALL.rst

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*******
2+
Docker driver installation guide
3+
*******
4+
5+
Requirements
6+
============
7+
8+
* Docker Engine
9+
10+
Install
11+
=======
12+
13+
Please refer to the `Virtual environment`_ documentation for installation best
14+
practices. If not using a virtual environment, please consider passing the
15+
widely recommended `'--user' flag`_ when invoking ``pip``.
16+
17+
.. _Virtual environment: https://virtualenv.pypa.io/en/latest/
18+
.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site
19+
20+
.. code-block:: bash
21+
22+
$ pip install 'molecule[docker]'

molecule/default/molecule.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
dependency:
3+
name: galaxy
4+
driver:
5+
name: docker
6+
lint:
7+
name: yamllint
8+
platforms:
9+
- name: centos
10+
image: centos:6
11+
pull: false
12+
- name: ubuntu
13+
image: ubuntu
14+
pull: false
15+
provisioner:
16+
name: ansible
17+
lint:
18+
name: ansible-lint
19+
verifier:
20+
name: testinfra
21+
lint:
22+
name: flake8

molecule/default/playbook.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
roles:
5+
- role: postgresql
Binary file not shown.

molecule/default/tests/main.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
postgresql_database: true
3+
version: 9.5
4+
postgresql_users: true
5+
user_list:
6+
- { user: ishan, password: ishan}
7+
database_list:
8+
- { db: ishan }
9+
- { db: dev }
10+
user: postgres
11+
group: postgres
12+
data_directory: /home/centos/ishan
13+
...
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import pytest
3+
import testinfra.utils.ansible_runner
4+
5+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
6+
os.environ['MOLECULE_INVENTORY_FILE']
7+
).get_hosts('all')
8+
9+
debian_pkg = []
10+
debian_pkg.append('python-psycopg2')
11+
debian_pkg.append('libpq-dev')
12+
debian_pkg.append('pgadmin4')
13+
debian_pkg.append('postgresql-client-9.5')
14+
debian_pkg.append('postgresql-9.5')
15+
debian_pkg.append('postgresql-contrib-9.5')
16+
debian_pkg.append('postgresql-server-dev-9.5')
17+
18+
19+
def test_debian_pkg(host):
20+
os = host.system_info.distribution
21+
22+
if os == 'debian':
23+
debian_package = host.package(debian_pkg)
24+
25+
assert debian_package.is_installed
26+
27+
28+
redhat_pkg = []
29+
redhat_pkg.append('postgresql-contrib')
30+
redhat_pkg.append('postgresql-devel')
31+
redhat_pkg.append('python-psycopg2')
32+
redhat_pkg.append('postgresql95')
33+
redhat_pkg.append('postgresql95-server')
34+
35+
36+
def test_redhat_pkg(host):
37+
os = host.system_info.distribution
38+
39+
if os == 'redhat':
40+
redhat_package = host.package(redhat_pkg)
41+
42+
assert redhat_package.is_installed
43+
44+
45+
def test_postgresql_service(host):
46+
os = host.system_info.distribution
47+
48+
if os == 'debian':
49+
debian_service = host.service('postgresql')
50+
51+
assert debian_service.is_running
52+
53+
elif os == 'redhat':
54+
redhat_service = host.service('postgresql9.5')
55+
56+
assert redhat_service.is_running
57+
58+
59+
@pytest.mark.parametrize('user', ['demo1', 'demo2'])
60+
def test_user(host, user):
61+
c1 = "sudo -u postgres psql -t -c"
62+
user_status = host.run(c1+"'\\du' | cut -d \\| -f 1 | grep -w "+user)
63+
64+
assert user_status.succeeded
65+
66+
67+
@pytest.mark.parametrize('db', ['test1', 'test2'])
68+
def test_database(host, db):
69+
c1 = "sudo -u postgres psql -t -c"
70+
db_status = host.run(c1+"'\\l' | cut -d \\| -f 1 | grep -w "+db)
71+
72+
assert db_status.succeeded
1023 Bytes
Binary file not shown.

molecule/default/var.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
a: b

molecule/default/verify.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: Verify
3+
hosts: all
4+
vars_files:
5+
- default/main.yml

tasks/a

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Install the client packages
3+
yum:
4+
name: "{{ item }}"
5+
state: present
6+
update_cache: yes
7+
with_items:
8+
- postgresql-contrib
9+
- postgresql-server
10+
11+
- name: Ensure PostgreSQL database is initialized.
12+
command: postgresql-setup initdb
13+
14+
- name: Enable the postgres service
15+
service:
16+
name: postgresql
17+
enabled: yes
18+
notify:
19+
- restart postgresql

tasks/b

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- hosts: postgresql11_hosts
2+
tasks:
3+
- name: setup pg 11
4+
shell: |
5+
rpm -Uvh https://yum.postgresql.org/11/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
6+
yum install -y postgresql11-server postgresql11-contrib
7+
- name: initdb
8+
shell: |
9+
/usr/pgsql-11/bin/postgresql-11-setup initdb
10+
ignore_errors: yes
11+
- name: setup service
12+
shell: |
13+
systemctl enable postgresql-11.service
14+
systemctl start postgresql-11.service

tasks/database.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: create a db
3+
postgresql_db:
4+
name: "{{ item.db }}"
5+
state: present
6+
with_items:
7+
- "{{ database_list }}"
8+
become_user: postgres
9+
become: true
10+
...

tasks/main.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
# Variable setup.
3+
- name: Include OS-Specific variables
4+
include_vars: "{{ ansible_os_family }}.yml"
5+
6+
- include_tasks: setup-RedHat.yml
7+
when: ansible_os_family == 'RedHat'
8+
9+
- include_tasks: setup-Debian.yml
10+
when: ansible_os_family == 'Debian'
11+
12+
- name: This task is included to create a user in postgresql
13+
include: users.yml
14+
when: postgresql_users
15+
16+
- name: This task is included to create a postgresql
17+
include: database.yml
18+
when: postgresql_database

0 commit comments

Comments
 (0)