diff --git a/.gitignore b/.gitignore index c9b2377..f56f5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.retry -tests/test.sh +*/__pycache__ +*.pyc diff --git a/.travis.yml b/.travis.yml index 75fb26b..d6e3452 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,48 +1,32 @@ --- +language: python services: docker env: - - distro: centos7 - postgresql_bin_dir: /usr/bin - postgresql_data_dir: /var/lib/pgsql/data - - distro: centos6 - postgresql_bin_dir: /usr/bin - postgresql_data_dir: /var/lib/pgsql/data - - distro: ubuntu1804 - postgresql_bin_dir: /usr/lib/postgresql/10/bin - postgresql_data_dir: /var/lib/postgresql/10/main - - distro: ubuntu1604 - postgresql_bin_dir: /usr/lib/postgresql/9.5/bin - postgresql_data_dir: /var/lib/postgresql/9.5/main - - distro: ubuntu1404 - postgresql_bin_dir: /usr/lib/postgresql/9.3/bin - postgresql_data_dir: /var/lib/postgresql/9.3/main - - distro: debian8 - postgresql_bin_dir: /usr/lib/postgresql/9.4/bin - postgresql_data_dir: /var/lib/postgresql/9.4/main - - distro: debian9 - postgresql_bin_dir: /usr/lib/postgresql/9.6/bin - postgresql_data_dir: /var/lib/postgresql/9.6/main + global: + - ROLE_NAME: postgresql + matrix: + - MOLECULE_DISTRO: centos7 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: ubuntu1604 + - MOLECULE_DISTRO: ubuntu1404 + - MOLECULE_DISTRO: debian9 + - MOLECULE_DISTRO: debian8 + +install: + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME + - cd geerlingguy.$ROLE_NAME script: - # Configure test script so we can run extra tests after playbook is run. - - export container_id=$(date +%s) - - export cleanup=false - - # Download test shim. - - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ - - chmod +x ${PWD}/tests/test.sh - # Run tests. - - ${PWD}/tests/test.sh - - # Check PostgreSQL status. - - 'docker exec -u postgres ${container_id} ${postgresql_bin_dir}/pg_ctl -D ${postgresql_data_dir} status' - -after_failure: - # Check what happened on systemd systems. - - 'docker exec --tty ${container_id} env TERM=xterm systemctl -l status postgresql.service' - - 'docker exec --tty ${container_id} env TERM=xterm journalctl -xe --no-pager' + - molecule test notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/README.md b/README.md index 4f0bdbf..6d81e4e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ The user and group under which PostgreSQL will run. The directories (usually one, but can be multiple) where PostgreSQL's socket will be created. + postgresql_service_state: started + postgresql_service_enabled: true + +Control the state of the postgresql service and whether it should start at boot time. + postgresql_global_config_options: - option: unix_socket_directories value: '{{ postgresql_unix_socket_directories | join(",") }}' diff --git a/defaults/main.yml b/defaults/main.yml index 7453eb5..fa6a87f 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -13,6 +13,9 @@ postgresql_group: postgres postgresql_unix_socket_directories: - /var/run/postgresql +postgresql_service_state: started +postgresql_service_enabled: true + # Global configuration options that will be set in postgresql.conf. postgresql_global_config_options: - option: unix_socket_directories @@ -21,10 +24,10 @@ postgresql_global_config_options: # Host based authentication (hba) entries to be added to the pg_hba.conf. This # variable's defaults reflect the defaults that come with a fresh installation. postgresql_hba_entries: - - { type: local, database: all, user: postgres, auth_method: peer } - - { type: local, database: all, user: all, auth_method: peer } - - { type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5 } - - { type: host, database: all, user: all, address: '::1/128', auth_method: md5 } + - {type: local, database: all, user: postgres, auth_method: peer} + - {type: local, database: all, user: all, auth_method: peer} + - {type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5} + - {type: host, database: all, user: all, address: '::1/128', auth_method: md5} # Debian only. Used to generate the locales used by PostgreSQL databases. postgresql_locales: @@ -32,30 +35,30 @@ postgresql_locales: # Databases to ensure exist. postgresql_databases: [] - # - name: exampledb # required; the rest are optional - # lc_collate: # defaults to 'en_US.UTF-8' - # lc_ctype: # defaults to 'en_US.UTF-8' - # encoding: # defaults to 'UTF-8' - # template: # defaults to 'template0' - # login_host: # defaults to 'localhost' - # login_password: # defaults to not set - # login_user: # defaults to '{{ postgresql_user }}' - # login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories - # port: # defaults to not set - # owner: # defaults to postgresql_user - # state: # defaults to 'present' +# - name: exampledb # required; the rest are optional +# lc_collate: # defaults to 'en_US.UTF-8' +# lc_ctype: # defaults to 'en_US.UTF-8' +# encoding: # defaults to 'UTF-8' +# template: # defaults to 'template0' +# login_host: # defaults to 'localhost' +# login_password: # defaults to not set +# login_user: # defaults to '{{ postgresql_user }}' +# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories +# port: # defaults to not set +# owner: # defaults to postgresql_user +# state: # defaults to 'present' # Users to ensure exist. postgresql_users: [] - # - name: jdoe #required; the rest are optional - # password: # defaults to not set - # encrypted: # defaults to not set - # priv: # defaults to not set - # role_attr_flags: # defaults to not set - # db: # defaults to not set - # login_host: # defaults to 'localhost' - # login_password: # defaults to not set - # login_user: # defaults to '{{ postgresql_user }}' - # login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories - # port: # defaults to not set - # state: # defaults to 'present' +# - name: jdoe #required; the rest are optional +# password: # defaults to not set +# encrypted: # defaults to not set +# priv: # defaults to not set +# role_attr_flags: # defaults to not set +# db: # defaults to not set +# login_host: # defaults to 'localhost' +# login_password: # defaults to not set +# login_user: # defaults to '{{ postgresql_user }}' +# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories +# port: # defaults to not set +# state: # defaults to 'present' diff --git a/meta/main.yml b/meta/main.yml index 8ed8f82..ccdd857 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -10,14 +10,14 @@ galaxy_info: platforms: - name: EL versions: - - 6 - - 7 + - 6 + - 7 - name: Ubuntu versions: - - all + - all - name: Debian versions: - - all + - all galaxy_tags: - database - postgresql diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..0339702 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,27 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint + options: + config-file: molecule/default/yaml-lint.yml +platforms: + - name: instance + image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible + command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"} + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tests/test.yml b/molecule/default/playbook.yml similarity index 53% rename from tests/test.yml rename to molecule/default/playbook.yml index 45dd9b5..78141d3 100644 --- a/tests/test.yml +++ b/molecule/default/playbook.yml @@ -1,5 +1,7 @@ --- -- hosts: all +- name: Converge + hosts: all + become: true vars: postgresql_databases: @@ -9,7 +11,7 @@ pre_tasks: - name: Update apt cache. - apt: update_cache=yes cache_valid_time=600 + apt: update_cache=true cache_valid_time=600 when: ansible_os_family == 'Debian' - name: Set custom variables for old CentOS 6 PostgreSQL install. @@ -18,7 +20,14 @@ postgresql_global_config_options: - option: unix_socket_directory value: '{{ postgresql_unix_socket_directories[0] }}' - when: ansible_os_family == 'RedHat' and ansible_distribution_version.split('.')[0] == '6' + when: + - ansible_os_family == 'RedHat' + - ansible_distribution_version.split('.')[0] == '6' roles: - - role_under_test + - role: geerlingguy.postgresql + + post_tasks: + - name: Verify postgres is running. + command: "{{ postgresql_bin_path }}/pg_ctl -D {{ postgresql_data_dir }} status" + changed_when: false diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/molecule/default/yaml-lint.yml b/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..a3dbc38 --- /dev/null +++ b/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 120 + level: warning diff --git a/tasks/databases.yml b/tasks/databases.yml index fa782ea..e01d804 100644 --- a/tasks/databases.yml +++ b/tasks/databases.yml @@ -14,7 +14,7 @@ owner: "{{ item.owner | default(postgresql_user) }}" state: "{{ item.state | default('present') }}" with_items: "{{ postgresql_databases }}" - become: yes + become: true become_user: "{{ postgresql_user }}" # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 vars: diff --git a/tasks/initialize.yml b/tasks/initialize.yml index 44ffd9b..0183121 100644 --- a/tasks/initialize.yml +++ b/tasks/initialize.yml @@ -22,7 +22,7 @@ - name: Ensure PostgreSQL database is initialized. command: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}" when: not pgdata_dir_version.stat.exists - become: yes + become: true become_user: "{{ postgresql_user }}" # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 vars: diff --git a/tasks/main.yml b/tasks/main.yml index 6859c73..fce7db8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -15,8 +15,8 @@ - name: Ensure PostgreSQL is started and enabled on boot. service: name: "{{ postgresql_daemon }}" - state: started - enabled: yes + state: "{{ postgresql_service_state }}" + enabled: "{{ postgresql_service_enabled }}" # Configure PostgreSQL. - import_tasks: databases.yml diff --git a/tasks/users.yml b/tasks/users.yml index fb7f5de..34746eb 100644 --- a/tasks/users.yml +++ b/tasks/users.yml @@ -15,7 +15,7 @@ state: "{{ item.state | default('present') }}" with_items: "{{ postgresql_users }}" no_log: true - become: yes + become: true become_user: "{{ postgresql_user }}" # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 vars: diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 6fb2117..0000000 --- a/tests/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Ansible Role tests - -To run the test playbook(s) in this directory: - - 1. Install and start Docker. - 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`: - - `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/` - 1. Make the test shim executable: `chmod +x tests/test.sh`. - 1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh` - -If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)` diff --git a/vars/Debian-7.yml b/vars/Debian-7.yml index 4d3a8ea..6b933bb 100644 --- a/vars/Debian-7.yml +++ b/vars/Debian-7.yml @@ -7,4 +7,4 @@ __postgresql_daemon: postgresql __postgresql_packages: - postgresql - postgresql-contrib - - libpq-dev \ No newline at end of file + - libpq-dev