In the last three posts we did the initial Ansible setup, installed the operating system packages, created the PostgreSQL group and user and downloaded, compiled and installed the PostgreSQL binaries from source. In this post we’ll look at how we can use Ansible to create our first PostgreSQL instance.
As a reminder this is our current playbook:
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 | [ansible@ansiblecontrol ansible]$ pwd /opt/ansible [ansible@ansiblecontrol ansible]$ cat roles /postgresqldbserver/tasks/main .yml --- - name: Install PostgreSQL dependencies yum: name={{item}} state=present with_items: - gcc - openldap-devel - python-devel - readline-devel - openssl-devel - redhat-lsb - bison - flex - perl-ExtUtils-Embed - zlib-devel - crypto-utils - openssl-devel - pam-devel - libxml2-devel - libxslt-devel - tcl - tcl-devel - openssh-clients - bzip2 - net-tools - wget - screen - ksh - name: Add PostgreSQL operating system group group: name=postgressomegroup state=present - name: Add PostgreSQL operating system user user: name=postgres comment= "PostgreSQL binaries owner" group=postgres - name: Download the PostgreSQL 9.5.3 sources get_url: url=https: //ftp .postgresql.org /pub/source/v9 .5.3 /postgresql-9 .5.3. tar .bz2 dest= /var/tmp mode=0755 - name: Copy PostgreSQL install script to targets copy: src=install_pg953.sh dest= /var/tmp/install_pg953 .sh owner=root group=root mode= "u=rwx" - name: Compile and install PostgreSQL shell: /var/tmp/install_pg953 .sh >> /var/tmp/install_pg_log args: executable: /usr/bin/bash |
What we need now is an additional task which does the creation of the PostgreSQL cluster on disk. As with the installation from source we’ll need to do some scripting for this. Here is a very basic script for doing this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/usr/bin/bash PGUSER= "postgres" PGGROUP= "postgres" DATADIRECTORY= "/u02/pgdata/PG1" XLOGLOCATION= "/u03/pgdata/PG1" PGHOME= "/u01/app/postgres/product/95/db_3/" POSTGRESDBPASSWORD= "postgres" mkdir -p ${DATADIRECTORY} mkdir -p ${XLOGLOCATION} chown ${PGUSER}:${PGGROUP} ${DATADIRECTORY} chown ${PGUSER}:${PGGROUP} ${XLOGLOCATION} su - ${PGUSER} -c "echo ${POSTGRESDBPASSWORD} > /var/tmp/tmp_pwd" su - ${PGUSER} -c "${PGHOME}/bin/initdb -D ${DATADIRECTORY} --pwfile=/var/tmp/tmp_pwd -X ${XLOGLOCATION} -k" rm -f /var/tmp/tmp_pwd su - ${PGUSER} -c "${PGHOME}/bin/pg_ctl -D ${DATADIRECTORY} start" |
As with the PostgreSQL installation script we’ll put this into the “files” directory of our “postgresqldbserver” role:
1 2 | [ansible@ansiblecontrol ansible]$ ls roles /postgresqldbserver/files/ create_pg953_cluster.sh install_pg953.sh |
We’ll use the same logic again and add two new tasks to our existing playbook:
1 2 3 4 5 6 7 | - name: Copy the PostgreSQL cluster creation script to the targets copy: src=create_pg953_cluster.sh dest= /var/tmp/create_pg953_cluster .sh owner=root group=root mode= "u=rwx" - name: Create and start the PostgreSQL instance shell: /var/tmp/create_pg953_cluster .sh >> /var/tmp/create_pg_cluster .log args: executable: /usr/bin/bash |
Once the playbook is executed again:
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 | [ansible@ansiblecontrol ansible]$ ansible-playbook -i /opt/ansible/development /opt/ansible/roles/postgresqldbserver/site .yml PLAY [postgresql-servers] ****************************************************** TASK [setup] ******************************************************************* ok: [192.168.22.171] ok: [192.168.22.172] TASK [postgresqldbserver : Install PostgreSQL dependencies] ******************** ok: [192.168.22.172] => (item=[u 'gcc' , u 'openldap-devel' , u 'python-devel' , u 'readline-devel' , u 'openssl-devel' , u 'redhat-lsb' , u 'bison' , u 'flex' , u 'perl-ExtUtils-Embed' , u 'zlib-devel' , u 'crypto-utils' , u 'openssl-devel' , u 'pam-devel' , u 'libxml2-devel' , u 'libxslt-devel' , u 'tcl' , u 'tcl-devel' , u 'openssh-clients' , u 'bzip2' , u 'net-tools' , u 'wget' , u 'screen' , u 'ksh' ]) ok: [192.168.22.171] => (item=[u 'gcc' , u 'openldap-devel' , u 'python-devel' , u 'readline-devel' , u 'openssl-devel' , u 'redhat-lsb' , u 'bison' , u 'flex' , u 'perl-ExtUtils-Embed' , u 'zlib-devel' , u 'crypto-utils' , u 'openssl-devel' , u 'pam-devel' , u 'libxml2-devel' , u 'libxslt-devel' , u 'tcl' , u 'tcl-devel' , u 'openssh-clients' , u 'bzip2' , u 'net-tools' , u 'wget' , u 'screen' , u 'ksh' ]) TASK [postgresqldbserver : Add PostgreSQL operating system group] ************** ok: [192.168.22.171] ok: [192.168.22.172] TASK [postgresqldbserver : Add PostgreSQL operating system user] *************** ok: [192.168.22.172] ok: [192.168.22.171] TASK [postgresqldbserver : Download the PostgreSQL 9.5.3 sources] ************** ok: [192.168.22.172] ok: [192.168.22.171] TASK [postgresqldbserver : Copy PostgreSQL install script to targets] ********** changed: [192.168.22.171] changed: [192.168.22.172] TASK [postgresqldbserver : Compile and install PostgreSQL] ********************* changed: [192.168.22.172] changed: [192.168.22.171] TASK [postgresqldbserver : Copy the PostgreSQL cluster creation script to the targets] *** ok: [192.168.22.171] changed: [192.168.22.172] TASK [postgresqldbserver : Create and start the PostgreSQL instance] *********** changed: [192.168.22.172] changed: [192.168.22.171] PLAY RECAP ********************************************************************* 192.168.22.171 : ok=9 changed=3 unreachable=0 failed=0 192.168.22.172 : ok=9 changed=4 unreachable=0 failed=0 |
… we should have a running PostgreSQL instance on both nodes, lets check:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@ansiblepg1 tmp] # ps -ef | grep postgres postgres 17284 1 0 08:47 ? 00:00:00 /u01/app/postgres/product/95/db_3/bin/postgres -D /u02/pgdata/PG1 postgres 17288 17284 0 08:47 ? 00:00:00 postgres: checkpointer process postgres 17289 17284 0 08:47 ? 00:00:00 postgres: writer process postgres 17290 17284 0 08:47 ? 00:00:00 postgres: wal writer process postgres 17291 17284 0 08:47 ? 00:00:00 postgres: autovacuum launcher process postgres 17292 17284 0 08:47 ? 00:00:00 postgres: stats collector process root 17294 10223 0 08:47 pts /1 00:00:00 grep --color=auto postgres [root@ansiblepg2 ~] # ps -ef | grep postgres postgres 16951 1 0 08:47 ? 00:00:00 /u01/app/postgres/product/95/db_3/bin/postgres -D /u02/pgdata/PG1 postgres 16955 16951 0 08:47 ? 00:00:00 postgres: checkpointer process postgres 16956 16951 0 08:47 ? 00:00:00 postgres: writer process postgres 16957 16951 0 08:47 ? 00:00:00 postgres: wal writer process postgres 16958 16951 0 08:47 ? 00:00:00 postgres: autovacuum launcher process postgres 16959 16951 0 08:47 ? 00:00:00 postgres: stats collector process root 16985 16964 0 08:48 pts /0 00:00:00 grep --color=auto postgres |
Isn’t that cool? Sure, there is much hard coding in here which needs to be extended by using variables. This is a topic for another post.
For your reference here is the complete playbook:
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 | [ansible@ansiblecontrol ansible]$ cat /opt/ansible/roles/postgresqldbserver/tasks/main .yml --- - name: Install PostgreSQL dependencies yum: name={{item}} state=present with_items: - gcc - openldap-devel - python-devel - readline-devel - openssl-devel - redhat-lsb - bison - flex - perl-ExtUtils-Embed - zlib-devel - crypto-utils - openssl-devel - pam-devel - libxml2-devel - libxslt-devel - tcl - tcl-devel - openssh-clients - bzip2 - net-tools - wget - screen - ksh - name: Add PostgreSQL operating system group group: name=postgressomegroup state=present - name: Add PostgreSQL operating system user user: name=postgres comment= "PostgreSQL binaries owner" group=postgres - name: Download the PostgreSQL 9.5.3 sources get_url: url=https: //ftp .postgresql.org /pub/source/v9 .5.3 /postgresql-9 .5.3. tar .bz2 dest= /var/tmp mode=0755 - name: Copy PostgreSQL install script to targets copy: src=install_pg953.sh dest= /var/tmp/install_pg953 .sh owner=root group=root mode= "u=rwx" - name: Compile and install PostgreSQL shell: /var/tmp/install_pg953 .sh >> /var/tmp/install_pg_log args: executable: /usr/bin/bash - name: Copy the PostgreSQL cluster creation script to the targets copy: src=create_pg953_cluster.sh dest= /var/tmp/create_pg953_cluster .sh owner=root group=root mode= "u=rwx" - name: Create and start the PostgreSQL instance shell: /var/tmp/create_pg953_cluster .sh >> /var/tmp/create_pg_cluster .log args: executable: /usr/bin/bash |