LoginSignup
0
0

More than 1 year has passed since last update.

nginxとtomcatの連携をansibleで書いてみた

Last updated at Posted at 2022-08-01

はじめに

先日作成した記事の工程をさくっとAnsibleのplaybookで作成してみました。

Ansibleの設定はすでにできているものとして、playbookのみ記載しています。

バージョン

OS AmazonLinux2
nginx 1.20.0
JDK 1.8.0_332
tomcat 7.0.76

playbook

- hosts: 172.31.0.0 #Ansibleを流される側のIPアドレス
  gather_facts: no #リモートのOSの情報を取得しない
  become: yes #rootユーザーで実行する(sudoで実行)

  tasks:
  - name: enable corretto8
    shell: "amazon-linux-extras enable corretto8"
    changed_when: false
#corretto8のトピックを有効にする
#changed_whenの設定で結果に関わらず次のタスクを実行
#(何度playbookを流してもエラーが出ない設定)

  - name: install java
    yum: name={{ item }}
    with_items:
     - java-1.8.0-amazon-corretto
     - java-1.8.0-amazon-corretto-devel

  - name: install tomcat
    yum: name={{ item }}
    with_items:
     - tomcat
     - tomcat-webapps
     - tomcat-admin-webapps

  - name: enable nginx
    shell: "amazon-linux-extras enable nginx1"
    changed_when: false

  - name: install nginx
    yum:
      name: nginx

  - name: setup proxy
    lineinfile:
      path: /etc/nginx/nginx.conf
      insertbefore: '        error_page'
      line: "\n        location / {\n        proxy_pass http://172.31.0.0:8080/;\n        }\n"
      firstmatch: yes
#nginxの設定ファイルに書き込み、リクエストをtomcatに流すproxyを設定
#最初にマッチした'        error_page'を含む行の上に設定を書き込む

  - name: start nginx and tomcat
    systemd:
      name: "{{ item }}"
      state: restarted
      enabled: yes
    with_items:
      - nginx
      - tomcat

終わりに

プロキシの設定で思う位置に書き込みができず苦戦しました。
実際このplaybookを何度も同じサーバーに流すと、プロキシの設定が流した回数分書き込まれてしまうので、一発限りのplaybookかな、と思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0