はじめに
先日作成した記事の工程をさくっと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かな、と思います。