From 4ead2f82c9e87e6e0fdca1e1521950f6f616eb27 Mon Sep 17 00:00:00 2001 From: Attilio Greco Date: Tue, 13 Jul 2021 18:22:31 +0200 Subject: [PATCH] publish on github --- .travis.yml | 29 ++++++++++++++++++++++++++ .yamllint | 33 ++++++++++++++++++++++++++++++ README.md | 38 +++++++++++++++++++++++++++++++++++ defaults/main.yml | 26 ++++++++++++++++++++++++ handlers/main.yml | 17 ++++++++++++++++ meta/main.yml | 31 ++++++++++++++++++++++++++++ molecule/default/INSTALL.rst | 11 ++++++++++ molecule/default/converge.yml | 8 ++++++++ molecule/default/molecule.yml | 13 ++++++++++++ molecule/default/playbook.yml | 6 ++++++ molecule/default/prepare.yml | 18 +++++++++++++++++ molecule/default/verify.yml | 12 +++++++++++ tasks/configure_postgres.yml | 12 +++++++++++ tasks/init_postgres.yml | 12 +++++++++++ tasks/install.yml | 19 ++++++++++++++++++ tasks/main.yml | 4 ++++ templates/pg_hba.conf.j2 | 8 ++++++++ test.json | 0 tests/inventory | 2 ++ tests/test.yml | 5 +++++ vars/main.yml | 2 ++ 21 files changed, 306 insertions(+) create mode 100644 .travis.yml create mode 100644 .yamllint create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/default/INSTALL.rst create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/playbook.yml create mode 100644 molecule/default/prepare.yml create mode 100644 molecule/default/verify.yml create mode 100644 tasks/configure_postgres.yml create mode 100644 tasks/init_postgres.yml create mode 100644 tasks/install.yml create mode 100644 tasks/main.yml create mode 100644 templates/pg_hba.conf.j2 create mode 100644 test.json create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..8827676 --- /dev/null +++ b/.yamllint @@ -0,0 +1,33 @@ +--- +# Based on ansible-lint config +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + colons: + max-spaces-after: -1 + level: error + commas: + max-spaces-after: -1 + level: error + comments: disable + comments-indentation: disable + document-start: disable + empty-lines: + max: 3 + level: error + hyphens: + level: error + indentation: disable + key-duplicates: enable + line-length: disable + new-line-at-end-of-file: disable + new-lines: + type: unix + trailing-spaces: disable + truthy: disable diff --git a/README.md b/README.md new file mode 100644 index 0000000..b671ac5 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +Ansible Postgresql +========= + +Requirements +------------ +no requirements + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: +```yaml +- hosts: servers + roles: + - role: ansible-postgres + postgres_version: 13 + pg_hba_user_options: + - contype: host + users: all + source: 127.0.0.1/32 + databases: all + method: md5 +``` + +License +------- + +MIT + +## Devel +for devel use +https://github.com/ansible-community/molecule-libvirt + +requirements for testings +KVM host +``` +libvirt-python +``` diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..5cb85a1 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,26 @@ +--- +# defaults file for postgres +postgres_repo: 'https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm' +postgres_version: 13 + +pg_hba_user_options: +- contype: host + users: all + source: 127.0.0.1/32 + databases: all + method: md5 +- contype: host + users: all + source: 192.168.100.10/24 + databases: all + method: md5 +- contype: host + users: all + source: 192.168.200.1/24 + databases: all + method: md5 +- contype: host + users: all + source: 192.169.100.1/24 + databases: all + method: md5 \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..3904767 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,17 @@ +--- +# handlers file for postgres +- name: start postgreql + ansible.builtin.systemd: + state: started + name: "postgresql-{{postgres_version}}" + enabled: yes + +- name: restart postgreql + ansible.builtin.systemd: + state: restarted + name: "postgresql-{{postgres_version}}" + +- name: reloaded postgreql + ansible.builtin.systemd: + state: reloaded + name: "postgresql-{{postgres_version}}" diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..5bdb03d --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,31 @@ +galaxy_info: + role_name: role-postgres + author: Attilio Greco + description: install postgres in REHL family + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + license: MIT + + min_ansible_version: 4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + platforms: + - name: Rocky linux + versions: + - 8.4 + + galaxy_tags: [database, postgres] + +dependencies: [] diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..a1fb637 --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,11 @@ +********************************* +LibVirt driver installation guide +********************************* + +Requirements +============ + +* Configure libvirt + +Install +======= diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..09a2dcd --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + become: true + tasks: + - name: "Include postgres" + include_role: + name: "postgres" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..48277c1 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,13 @@ +--- +name: galaxy +driver: + name: libvirt +platforms: + - name: instance-1 + image_url: "http://remoteshare.4sigma.top/Rocky8.4-try-srinked.qcow2" + disk_size: "10G" + memory_size: "1" # in GB +provisioner: + name: ansible +verifier: + name: ansible \ No newline at end of file diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..668ede7 --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,6 @@ +--- +- name: Converge + hosts: all + become: yes + roles: + - role: postgres \ No newline at end of file diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml new file mode 100644 index 0000000..1754d9d --- /dev/null +++ b/molecule/default/prepare.yml @@ -0,0 +1,18 @@ +- name: prepare + hosts: all + become: true + tasks: + - name: prepare | install python3-pip + dnf: + name: + - python3-pip + - python3-devel + - gcc + + - name: prepare | create ansible requirements + ansible.builtin.file: + path: /home/molecule/ansible + state: directory + owner: molecule + group: molecule + mode: '0644' diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..bd42402 --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,12 @@ +--- +# This is an example playbook to execute Ansible tests. + +- name: Verify + hosts: all + vars: + ansible_python_interpreter: + tasks: + - name: Verify | check postgres is running + ansible.builtin.systemd: + name: postgresql-13.service + state: started diff --git a/tasks/configure_postgres.yml b/tasks/configure_postgres.yml new file mode 100644 index 0000000..7d67c9a --- /dev/null +++ b/tasks/configure_postgres.yml @@ -0,0 +1,12 @@ +- name: Grant users joe and simon access to databases sales and logistics from ipv6 localhost ::1/128 using peer authentication + community.postgresql.postgresql_pg_hba: + dest: /var/lib/pgsql/{{postgres_version}}/data/pg_hba.conf + contype: "{{ item.contype }}" + users: "{{ item.users }}" + source: "{{ item.source }}" + databases: "{{ item.databases }}" + method: "{{ item.method }}" + create: true + loop: "{{pg_hba_user_options}}" + notify: + - reloaded postgreql diff --git a/tasks/init_postgres.yml b/tasks/init_postgres.yml new file mode 100644 index 0000000..399427e --- /dev/null +++ b/tasks/init_postgres.yml @@ -0,0 +1,12 @@ + +- name: postgres | init_postgres | init database + ansible.builtin.command: /usr/pgsql-{{postgres_version}}/bin/postgresql-{{postgres_version}}-setup initdb + register: postgresql_database_init + args: + creates: /var/lib/pgsql/13/initdb.log + notify: + - start postgreql + +- name: postgres | init_postgres | show posgres init + debug: + var: postgresql_database_init diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..b949792 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,19 @@ +--- +- name: postgres | install | install postgres repo + dnf: + name: 'https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm' + state: present + disable_gpg_check: yes + +- name: postgres | install | disable postgresql module + ansible.builtin.command: dnf -qy module disable postgresql + register: postgresql_disable_repo + args: + creates: /etc/dnf/modules.d/postgresql.module + +- name: postgres | install | install postgresql + dnf: + name: + - postgresql{{postgres_version}} + - postgresql{{postgres_version}}-server + state: present diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..1629f0b --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- import_tasks: tasks/install.yml +- import_tasks: tasks/init_postgres.yml +- import_tasks: configure_postgres.yml diff --git a/templates/pg_hba.conf.j2 b/templates/pg_hba.conf.j2 new file mode 100644 index 0000000..b17378f --- /dev/null +++ b/templates/pg_hba.conf.j2 @@ -0,0 +1,8 @@ +# TYPE DATABASE USER ADDRESS METHOD +{% for option_line in pg_hba_user_options -%} + {{ " " + option_line.type }} {{ option_line.database }} {{ option_line.user }} {{ option_line.address }} {{ option_line.method }} +{% endfor %} + +{% for option_line in pg_hba_default_options -%} + {{ " " + option_line.type }} {{ option_line.database }} {{ option_line.user }} {{ option_line.address }} {{ option_line.method }} +{% endfor %} \ No newline at end of file diff --git a/test.json b/test.json new file mode 100644 index 0000000..e69de29 diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..728654a --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - postgres diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..e6e9d7c --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for postgres