PostgreSQL10をansibleでインストールする
先日、PostgreSQL10がリリースされました。
早速インストールして追加機能を試したいのですが、DBは試験環境等々で何度もインストールが
発生するのでansbileで構築を自動化します。
公式
PostgreSQL: Documentation: 10: E.1. Release 10 www.ansible.com
環境
- CentOS7.3 x 1
- postgresql-10.0
- ansible-2.4.0.0
今回は、ansibleを実行する環境はmacOS high sierraで行います。
構築される側の設定(CentOS7)
centosは、root以外のユーザを作成しておきます。
これはansibleがログインするためのユーザです。
$ useradd ansible $ passwd ansible
続いて、上記作成したユーザがrootにsudo出来るように以下の設定を追加します。
$ vim /etc/sudoer ----- 以下を追加 ansible ALL=(ALL) ALL
これで、構築される側の設定は完了です。
構築する側の設定(Mac)
まずは、macにansbileをインストールします。
$ brew install ansible
$ ansible --version ansible 2.4.0.0
続いて、ansbileのコンフィグ類を配置するディレクトリを準備します。
$ mkdir -p ansible/hosts $ mkdir -p ansible/roles/tasks/src $ cd ansible
続いてansibleのコンフィグを以下の内容で作成します。
$ vim ansible.cfg --- [defaults] retry_files_enabled = False [privilege_escalation] become = True [ssh_connection] ecp_if_ssh = True
続いて、以下の内容でinventoryの設定を行います。
- centos7-ansibleはCentOS7のIPアドレスまたはホスト名を指定して下さい。
- ansible_ssh_userはCentOS7に接続するsshユーザ名を指定して下さい。
- ansible_ssh_passはCentOS7に接続するsshパスワードを指定して下さい。
- ansible_sudo_passはCentOS7に接続したユーザのsudoパスワードを指定して下さい。
$ vim hosts/inventory --- [db] centos7-ansible [all:vars] ansible_ssh_port=22 ansible_ssh_user=ansible ansible_ssh_pass=ansible_pass01 ansible_sudo_pass=ansible_pass01
続いて、postgresqlの起動ファイルを以下の内容でMac側に作成します。
$ vim ansible/roles/tasks/src/postgresql.service --- [Unit] Description=PostgreSQL database server After=network.target [Service] Type=forking User=pgsql OOMScoreAdjust=-1000 EnvironmentFile=/etc/sysconfig/postgresql ExecStart=/usr/local/pgsql/bin/pg_ctl -w -D /usr/local/pgsql/data start ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data stop -m fast TimeoutSec=300 [Install] WantedBy=multi-user.target $ vim ansible/roles/tasks/src/postgresql --- POSTGRES_HOME=/usr/local/pgsql PGLIB=$POSTGRES_HOME/lib PGDATA=$POSTGRES_HOME/data
最後に、実行する処理内容をtaskとして設定していきます。
$ vim roles/tasks/db.yml --- - hosts: db tasks: - name: check postgresql file shell: | ls -al /tmp/postgresql-10.0.tar.gz register: postgresql_ls_result filed_when: postgresql_ls_result.rc not in [0, 1, 2] - name: check pgsql user shell: | grep 'pgsql' /etc/passwd register: pgsql_exist_result failed_when: pgsql_exist_result.rc not in [0, 1, 2] - name: check postgresql install shell: | ls -al /usr/local/pgsql/bin/psql register: postgresql_installed_result failed_when: postgresql_installed_result.rc not in [0, 1, 2] - name: useradd -b /usr/local/pgsql pgsql user: name=pgsql password={{ 'pgsql' |password_hash('sha512') }} when: pgsql_exist_result.rc == 1 - name: install via yum yum: name={{ item }} state=installed with_items: - vim - git - gcc - readline - readline-devel - zlib-devel - name: postgresql10 source download get_url: url: https://ftp.postgresql.org/pub/source/v10.0/postgresql-10.0.tar.gz dest: /tmp/postgresql-10.0.tar.gz mode: 0755 when: postgresql_ls_result.rc == 2 - name: postgresql10 source decompression shell: tar xvzf /tmp/postgresql-10.0.tar.gz -C /tmp/ when: postgresql_installed_result.rc == 2 - name: postgresql10 source install shell: cd /tmp/postgresql-10.0 && ldconfig && ./configure && gmake && gmake install become: yes when: postgresql_installed_result.rc == 2 - name: postgresql lib include shell: echo "/usr/local/pgsql/lib" > /etc/ld.so.conf.d/pgsql.conf && ldconfig become: yes when: postgresql_installed_result.rc == 2 - name: add env-variable path shell: | echo "export PATH=$PATH:/usr/local/pgsql/bin" >> ~/.bashrc | echo "export POSTGRES_HOME=/usr/local/pgsql" >> ~/.bashrc | echo "export PGLIB=$POSTGRES_HOME/lib" >> ~/.bashrc | echo "export PGDATA=$POSTGRES_HOME/data" >> ~/.bashrc | echo "export MANPATH=$MANPATH:$POSTGRES_HOME/man" >> ~/.bashrc | echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$PGLIB"" >> ~/.bashrc | source ~/.bashrc become: yes when: postgresql_installed_result.rc == 2 - name: postgresql dir permission change become: yes shell: chown -R pgsql:pgsql /usr/local/pgsql when: postgresql_installed_result.rc == 2 - name: postgresql initdb become: yes become_user: pgsql shell: /usr/local/pgsql/bin/initdb --no-locale -D /usr/local/pgsql/data --encoding=UTF-8 when: postgresql_installed_result.rc == 2 - name: add postgresql start script copy: src=./src/postgresql.service dest=/usr/lib/systemd/system/postgresql.service owner=root group=root mode=0644 when: postgresql_installed_result.rc == 2 - name: postgresql service enable become: yes shell: systemctl enable postgresql when: postgresql_installed_result.rc == 2 - name: add postgresql envfile become: yes copy: src=./src/postgresql dest=/etc/sysconfig/postgresql owner=root group=root mode=0644 when: postgresql_installed_result.rc == 2 - name: postgresql service start become: yes shell: systemctl start postgresql when: postgresql_installed_result.rc == 2
構築実行
ansibleからCentOSに正常に接続できるか以下のコマンドで確認します。
$ ansible -i hosts all -m ping centos7-ansible | SUCCESS => { "changed": false, "failed": false, "ping": "pong" }
上記のように、pingに対してpongが帰ってきていればOKです。
続いて、構築taskを実行していきます。
$ ansible-playbook -i hosts roles/tasks/db.yml centos7-ansible : ok=9 changed=6 unreachable=0 failed=0
上記のように、failedが0であれば実行成功です。
まとめ
ansibleのいいところは、作業を自動化できるだけでなく、gitで管理が出来て処理自体のレビューができるので、メンバー内に有識者が増えてくると使用するメリットはかなり大きいですね。
今後もどんどん使って慣れていきたいと思います。
これで、postgresql10の環境が揃ったので、新機能を試していきます。