2018年9月13日木曜日

AnsibleでRundeckとPostgreSQL10をインストールする(Ubuntu18.04用)

Rundeckはオープンソースのジョブスケジューラです。

〇Rundeckの画面

ブラウザからhttp://<ホスト名またはIP>:4440にアクセスします。デフォルトのユーザ名/パスワードはadmin/adminです。

〇インストール方法
1.下準備
~/.ansible.cfgに以下の内容を設定します
[ssh_connection]
pipelining=True
[defaults]
host_key_checking = False

2. インストール対象ホストのユーザやパスワードを環境に合わせてinventoryファイルに記入します

inventory例
[rundeck-postgresql]
192.168.55.61

[all:vars]
ansible_ssh_port=22
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_sudo_pass=vagrant

3.inventory、rundeck-postgresql.ymlを準備し、以下のコマンドを実行します。
ansible-playbook -i inventory rundeck-postgresql.yml

rundeck-postgresql.yml
- hosts: rundeck-postgresql
  vars:
    - dbname: rundeck
    - dbuser: rundeck
    - dbpassword: rundeck
    - dbhost: localhost
    - dbport: 3306
    - serverurl: http://192.168.55.61:4440
  tasks:
    - name: generate ssh key
      shell: ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
      become: yes
    - 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
      become: true
    - name: add an apt key
      apt_key:
        url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
        state: present
      become: yes
    - name: install packages
      apt:
        name: ca-certificates
        state: latest
      become: yes
    - name: Install required software
      apt: name={{ item }} state=latest
      with_items:
        - postgresql-10
        - libpq-dev
        - python-dev
        - python-pip
      become: true
    - name: install psycopg2 using pip
      pip:
        name: "{{ item }}"
        state: forcereinstall
      with_items:
        - psycopg2-binary
      become: true
    - name: configure postgresql.conf
      blockinfile:
        dest: /etc/postgresql/10/main/postgresql.conf
        create: yes
        block: |
          listen_addresses='*'
      become: true
    - name: edit pg_hba.conf
      replace: dest=/etc/postgresql/10/main/pg_hba.conf regexp="host.*all.*all.*127.0.0.1" replace="#host    all             all             127.0.0.1"
      become: true
    - name: edit pg_hba.conf
      replace: dest=/etc/postgresql/10/main/pg_hba.conf regexp="^host.*all.*all.*::1/128.*ident" replace="host    all             all             ::1/128    password"
      become: true
    - name: configure postgresql.conf
      blockinfile:
        dest: /etc/postgresql/10/main/pg_hba.conf
        create: yes
        block: |
          host    all         all         127.0.0.1/32          password
          host    all         all         192.168.1.0/24          password
          host    all         all         192.168.55.0/24          password
      become: true
    - name: enable and start postgresq.service
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: postgresql.service
      become: true
    - name: create PostgreSQL user
      postgresql_user:
        name: "{{ dbuser }}"
        password: "{{ dbpassword }}"
        login_user: postgres
        encrypted: yes
      become: true
      become_user: postgres
    - name: create a database
      postgresql_db:
        name: "{{ dbname }}"
        owner: "{{ dbuser }}"
        encoding: 'UTF-8'
        lc_collate: 'ja_JP.UTF-8'
        lc_ctype: 'ja_JP.UTF-8'
        template: 'template0'
        login_user: postgres
      become: true
      become_user: postgres
    - name: download jdbc driver
      get_url:
        url: https://jdbc.postgresql.org/download/postgresql-42.2.4.jar
        dest: /usr/share/java
      become: true
    - name: Install openjdk
      apt: name={{ item }} state=present
      with_items:
        - openjdk-8-jdk
      become: true
    - name: create a directory
      file: path=/opt/rundeck state=directory owner=root group=root
      become: yes
    - name: download rundeck
      get_url:
        url: http://dl.bintray.com/rundeck/rundeck-maven/rundeck-launcher-2.11.4.jar
        dest: /opt/rundeck
      become: true
    - name: install rundeck
      shell: /usr/bin/java -Xmx1024m -jar rundeck-launcher-2.11.4.jar --installonly
      args:
        chdir: /opt/rundeck/
      become: yes
    - name: edit rundeck-config.properties
      replace: dest=/opt/rundeck/server/config/rundeck-config.properties regexp="grails.serverURL=http://.*:4440" replace="grails.serverURL={{ serverurl }}"
      become: true
    - name: edit rundeck-config.properties
      replace: dest=/opt/rundeck/server/config/rundeck-config.properties regexp="dataSource.url = jdbc:h2:file:/opt/rundeck/server/data/grailsdb;MVCC=true" replace="dataSource.url = jdbc:postgresql://localhost:5432/rundeck"
      become: true
    - name: edit rundeck-config.properties
      blockinfile:
        dest: /opt/rundeck/server/config/rundeck-config.properties
        create: yes
        block: |
          dataSource.username = rundeck
          dataSource.password = rundeck
      become: true
    - name: setup systemd
      blockinfile:
        dest: /etc/systemd/system/rundeck.service
        create: yes
        block: |
          [Unit]
          Description=rundeck
          [Service]
          Type=simple
          ExecStart=/usr/bin/java -Xmx2048m -jar rundeck-launcher-2.11.4.jar -b /opt/rundeck
          WorkingDirectory=/opt/rundeck
          Restart=always
          RestartSec=10
          [Install]
          WantedBy=multi-user.target
      become: true
    - name: enable and start rundeck.service
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: rundeck.service
      become: true


○関連情報
・Ansibleに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿