Skip to content

Commit

Permalink
Use become/become_user instead of sudo/sudo_user.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesselang committed Jan 20, 2016
1 parent 49fee3f commit f6d4205
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
20 changes: 10 additions & 10 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
register: pgdata_dir_exist

- name: PostgreSQL | Ensure the locale is generated | Debian
sudo: yes
become: yes
locale_gen: name="{{ postgresql_locale }}" state=present
when: ansible_os_family == "Debian"

- name: PostgreSQL | Ensure the locale is generated | RedHat
sudo: yes
become: yes
command: >
localedef -c -i {{ postgresql_locale_parts[0] }} -f {{ postgresql_locale_parts[1] }}
{{ postgresql_locale }}
Expand All @@ -31,17 +31,17 @@

- name: PostgreSQL | Reset the cluster - drop the existing one | Debian
shell: pg_dropcluster --stop {{ postgresql_version }} {{ postgresql_cluster_name }}
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed

- name: PostgreSQL | Reset the cluster - create a new one (with specified encoding and locale) | Debian
shell: >
pg_createcluster --start --locale {{ postgresql_locale }}
-e {{ postgresql_encoding }} -d {{ postgresql_data_directory }}
{{ postgresql_version }} {{ postgresql_cluster_name }}
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed

- name: PostgreSQL | Check whether the postgres data directory is initialized
Expand All @@ -54,8 +54,8 @@
command: >
{{ postgresql_bin_directory }}/initdb -D {{ postgresql_data_directory }}
--locale={{ postgresql_locale }} --encoding={{ postgresql_encoding }}
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "RedHat" and
(postgresql_cluster_reset or
pgdata_dir_exist.changed or
Expand Down Expand Up @@ -89,8 +89,8 @@
register: postgresql_configuration_pt2

- name: PostgreSQL | Update configuration - pt. 3 (pgtune)
sudo: true
sudo_user: "{{ postgresql_service_user }}"
become: true
become_user: "{{ postgresql_service_user }}"
shell: |
set -e
TMPDIR=$(mktemp -d)
Expand Down
36 changes: 18 additions & 18 deletions tasks/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@
template: "template0"
state: present
login_user: "{{postgresql_admin_user}}"
sudo: yes
sudo_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: postgresql_databases
when: postgresql_databases|length > 0

- name: PostgreSQL | Add extensions to the databases
shell: "psql {{item.0.db}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS {{ item.1 }};'"
sudo: yes
sudo_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_service_user}}"
with_subelements:
- postgresql_database_extensions
- extensions
register: result
changed_when: "'NOTICE' not in result.stderr"

- name: PostgreSQL | Add hstore to the databases with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
with_items: postgresql_databases
register: hstore_ext_result
Expand All @@ -42,8 +42,8 @@
when: item.hstore is defined and item.hstore

- name: PostgreSQL | Add uuid-ossp to the database with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
with_items: postgresql_databases
register: uuid_ext_result
Expand All @@ -52,36 +52,36 @@
when: item.uuid_ossp is defined and item.uuid_ossp

- name: PostgreSQL | Add postgis to the databases with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS postgis;'&&psql {{item.name}} -c 'CREATE EXTENSION IF NOT EXISTS postgis_topology;'"
with_items: postgresql_databases
when: item.gis is defined and item.gis

- name: PostgreSQL | add cube to the database with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists cube;'"
with_items: postgresql_databases
when: item.cube is defined and item.cube

- name: PostgreSQL | Add plpgsql to the database with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'CREATE EXTENSION IF NOT EXISTS plpgsql;'"
with_items: postgresql_databases
when: item.plpgsql is defined and item.plpgsql

- name: PostgreSQL | add earthdistance to the database with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists earthdistance;'"
with_items: postgresql_databases
when: item.earthdistance is defined and item.earthdistance

- name: PostgreSQL | Add citext to the database with the requirement
sudo: yes
sudo_user: "{{ postgresql_service_user }}"
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS citext;'"
with_items: postgresql_databases
register: citext_ext_result
Expand Down
4 changes: 2 additions & 2 deletions tasks/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
port: "{{postgresql_port}}"
state: present
login_user: "{{postgresql_admin_user}}"
sudo: yes
sudo_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: postgresql_users
when: postgresql_users|length > 0
4 changes: 2 additions & 2 deletions tasks/users_privileges.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
login_host: "{{item.host | default(omit)}}"
login_user: "{{postgresql_admin_user}}"
role_attr_flags: "{{item.role_attr_flags | default(omit)}}"
sudo: yes
sudo_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: postgresql_user_privileges
when: postgresql_users|length > 0
2 changes: 1 addition & 1 deletion tests/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- hosts: all
remote_user: root
sudo: yes
become: yes
vars_files:
- ./vars.yml
roles:
Expand Down

0 comments on commit f6d4205

Please sign in to comment.