2019年12月3日火曜日

AnsibleでPostgreSQL11をインストールする

以下のAnsibleのRoleを使用して、PostgreSQL11をインストールする事ができます。

roles/pg11/tasks/main.yml
---
- name: check whether postgres is installed
  stat:
    path: "/usr/bin/psql"
  register: chk_pg11

- name: configure sources.list
  blockinfile:
    dest: /etc/apt/sources.list.d/pgdg.list
    create: yes
    block: |
      deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
  when: chk_pg11.stat.exists == false
  become: true

- name: add an apt key
  apt_key:
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    state: present
  when: chk_pg11.stat.exists == false
  become: yes


- name: Install required software
  apt: name={{ item }} state=latest update_cache=yes
  vars:
    item:
    - ca-certificates
    - postgresql-11
  when: chk_pg11.stat.exists == false
  become: true

- name: configure postgresql.conf
  blockinfile:
    dest: /etc/postgresql/11/main/postgresql.conf
    create: yes
    block: |
      listen_addresses='*'
  when: chk_pg11.stat.exists == false
  become: true

- name: edit pg_hba.conf
  replace: dest=/etc/postgresql/11/main/pg_hba.conf regexp="host.*all.*all.*127.0.0.1" replace="#host    all             all             127.0.0.1"
  when: chk_pg11.stat.exists == false
  become: true

- name: edit pg_hba.conf
  replace: dest=/etc/postgresql/11/main/pg_hba.conf regexp="^host.*all.*all.*::1/128.*ident" replace="host    all             all             ::1/128    password"
  when: chk_pg11.stat.exists == false
  become: true

- name: configure postgresql.conf
  blockinfile:
    dest: /etc/postgresql/11/main/pg_hba.conf
    create: yes
    block: |
      host    all         all         127.0.0.1/32          password
      host    all         all         {{ ansible_eth0.ipv4.address }}/24          password
  when: chk_pg11.stat.exists == false
  become: true

- name: enable and start postgresq.service
  systemd:
    daemon_reload: yes
    enabled: yes
    state: started
    name: postgresql.service
  when: chk_pg11.stat.exists == false
  become: true

0 件のコメント:

コメントを投稿