Skip to content

Instantly share code, notes, and snippets.

@sbeliakou
Created July 6, 2021 18:11
Show Gist options
  • Save sbeliakou/d3efab1ed4442f57257f2db73cb90a75 to your computer and use it in GitHub Desktop.
Save sbeliakou/d3efab1ed4442f57257f2db73cb90a75 to your computer and use it in GitHub Desktop.
```yaml
- name: Installing Tomcat
hosts: app-server
become: yes
vars:
tomcat_user: tomcat
tomcat_group: tomcat
tomcat_version: 8.5.58
tomcat_version_major: "{{ tomcat_version.split('.')[0] }}"
tomcat_url: https://archive.apache.org/dist/tomcat/tomcat-{{ tomcat_version_major }}/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
tomcat_home: /usr/share/tomcat
tomcat_admin_username: admin
tomcat_admin_password: password
tasks:
- name: Install Java 1.8
yum:
name: java-1.8.0-openjdk
state: present
- name: Create tomcat Group
group:
name: "{{ tomcat_group }}"
- name: Add user "tomcat"
user:
name: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
home: "{{ tomcat_home }}"
createhome: no
- name: Download Tomcat
get_url:
url: "{{ tomcat_url }}"
dest: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
checksum: "md5:9def3ec8010601a837373a1754e97d9d"
- name: Unpack Tomcat Archive
unarchive:
src: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: /opt/
remote_src: yes
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
# use this as a workaround for an idempetency matter
creates: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
notify: restart webapp-server
- name: Symlink install directory
file:
src: /opt/apache-tomcat-{{ tomcat_version }}
path: "{{ tomcat_home }}"
state: link
- name: Copy Users Configuration
template:
src: tomcat-users.xml.j2
dest: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
- name: Disable Manager Access Restrictions
replace:
path: "/opt/apache-tomcat-{{ tomcat_version }}/webapps/manager/META-INF/context.xml"
regexp: (\s\+<)(Valve[^>]*/)>
replace: \1!--\2--
backup: yes
- name: Copy Tomcat Service File
template:
src: tomcat.service.j2
dest: /usr/lib/systemd/system/tomcat.service
- name: Ensure Tomcat Service Enabled and Running
systemd:
name: tomcat
state: started
enabled: yes
- name: Check port 8080
wait_for:
port: 8080
delay: 10
handlers:
- name: restart webapp-server
systemd: daemon_reload=yes name=webapp-server state=restarted
```
Idempotancy check:
```yaml
- name: Installing Tomcat
hosts: app-server
vars:
tomcat_user: tomcat
tomcat_group: tomcat
tomcat_version: 8.5.58
tomcat_version_major: "{{ tomcat_version.split('.')[0] }}"
tomcat_url: https://archive.apache.org/dist/tomcat/tomcat-{{ tomcat_version_major }}/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
tomcat_home: /usr/share/tomcat
tomcat_admin_username: admin
tomcat_admin_password: password
tasks:
- name: Install Java 1.8
yum:
name: java-1.8.0-openjdk
state: present
become: yes
- name: Create tomcat Group
group:
name: "{{ tomcat_group }}"
become: yes
- name: Add user "tomcat"
user:
name: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
home: "{{ tomcat_home }}"
createhome: no
become: yes
- name: Download Tomcat
get_url:
url: "{{ tomcat_url }}"
dest: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
checksum: "md5:9def3ec8010601a837373a1754e97d9d"
become: yes
- name: Unpack Tomcat Archive
unarchive:
src: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: /opt/
remote_src: yes
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
# use this as a workaround for an idempotency matter
creates: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
notify: restart server
become: yes
- name: Symlink install directory
file:
src: /opt/apache-tomcat-{{ tomcat_version }}
path: "{{ tomcat_home }}"
state: link
become: yes
- name: Copy Users Configuration
template:
src: tomcat-users.xml.j2
dest: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
notify: restart server
become: yes
- name: Disable Manager Access Restrictions
replace:
path: "{{ tomcat_home }}/webapps/manager/META-INF/context.xml"
regexp: (\s\+<)(Valve[^>]*/)>
replace: \1!--\2--
backup: yes
become: yes
- name: Copy Tomcat Service File
template:
src: tomcat.service.j2
dest: /usr/lib/systemd/system/tomcat.service
notify: reload daemons
become: yes
- name: Ensure Tomcat Service Enabled and Running
systemd:
name: tomcat
state: started
enabled: yes
become: yes
- wait_for:
host: 0.0.0.0
port: 8080
state: drained
timeout: 60
handlers:
- name: Restart Tomcat after config changes
systemd:
name: tomcat
state: restarted
listen: restart server
become: yes
- name: Reload daemon
systemd:
name: tomcat
state: reloaded
listen: reload daemons
become: yes
```
```yaml
- name: Installing Tomcat
hosts: app-server
become: yes
vars:
tomcat_user: tomcat
tomcat_group: tomcat
tomcat_version: 8.5.58
tomcat_version_major: "{{ tomcat_version.split('.')[0] }}"
tomcat_url: https://archive.apache.org/dist/tomcat/tomcat-{{ tomcat_version_major }}/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
tomcat_home: /usr/share/tomcat
tomcat_admin_username: admin
tomcat_admin_password: password
tasks:
- name: Install Java 1.8
yum:
name: java-1.8.0-openjdk
state: present
- name: Create tomcat Group
group:
name: "{{ tomcat_group }}"
- name: Add user "tomcat"
user:
name: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
home: "{{ tomcat_home }}"
createhome: no
- name: Download Tomcat
get_url:
url: "{{ tomcat_url }}"
dest: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
checksum: "md5:9def3ec8010601a837373a1754e97d9d"
- name: Unpack Tomcat Archive
unarchive:
src: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: /opt/
remote_src: yes
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
# use this as a workaround for an idempetency matter
creates: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
notify: "restart tomcat"
- name: Symlink install directory
file:
src: /opt/apache-tomcat-{{ tomcat_version }}
path: "{{ tomcat_home }}"
state: link
- name: Copy Users Configuration
template:
src: tomcat-users.xml.j2
dest: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
owner: "{{ tomcat_user }}"
group: "{{ tomcat_group }}"
- name: Disable Manager Access Restrictions
replace:
path: "{{ tomcat_home }}/webapps/manager/META-INF/context.xml"
regexp: (\s\+<)(Valve[^>]*/)>
replace: \1!--\2--
backup: yes
- name: Copy Tomcat Service File
template:
src: tomcat.service.j2
dest: /usr/lib/systemd/system/tomcat.service
notify: "reload daemon"
- name: Ensure Tomcat Service Enabled and Running
systemd:
name: tomcat
state: started
enabled: yes
- name: Waits for port 8080 of any IP
wait_for:
host: 0.0.0.0
port: 8080
state: drained
handlers:
- name: Restart Tomcat
systemd:
name: tomcat
state: restarted
listen: restart tomcat
become: yes
- name: Reload daemon
systemd:
name: tomcat
state: reloaded
listen: reload daemon
become: yes
```
```yaml
- name: Installing Tomcat
hosts: app-server
become: yes
vars:
tomcat_user: tomcat
tomcat_group: tomcat
tomcat_version: 8.5.58
tomcat_version_major: "{{ tomcat_version.split('.')[0] }}"
tomcat_url: https://archive.apache.org/dist/tomcat/tomcat-{{ tomcat_version_major }}/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
tomcat_home: /usr/share/tomcat
tomcat_admin_username: admin
tomcat_admin_password: password
tasks:
- name: Install Java 1.8
yum:
name: java-1.8.0-openjdk
state: present
- name: Create tomcat Group
group:
name: "{{ tomcat_group }}"
- name: Add user "tomcat"
user:
name: "{{ tomcat_name }}"
group: "{{ tomcat_group }}"
home: "{{ tomcat_home }}"
createhome: no
- name: Download Tomcat
get_url:
url: "{{ tomcat_url }}"
dest: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
checksum: "md5:9def3ec8010601a837373a1754e97d9d"
- name: Unpack Tomcat Archive
unarchive:
src: /opt/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: /opt/
remote_src: yes
owner: "{{ tomcat_name }}"
group: "{{ tomcat_group }}"
# use this as a workaround for an idempetency matter
creates: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
- name: Symlink install directory
file:
src: /opt/apache-tomcat-{{ tomcat_version }}
path: "{{ tomcat_home }}"
state: link
- name: Copy Users Configuration
template:
src: tomcat-users.xml.j2
dest: /opt/apache-tomcat-{{ tomcat_version }}/conf/tomcat-users.xml
owner: "{{ tomcat_name }}"
group: "{{ tomcat_group }}"
notify:
- Restart Tomcat
- wait port before
- name: Disable Manager Access Restrictions
replace:
path: "/opt/apache-tomcat-{{ tomcat_version }}/webapps/manager/META-INF/context.xml"
regexp: (\s\+<)(Valve[^>]*/)>
replace: \1!--\2--
backup: yes
- name: Copy Tomcat Service File
template:
src: tomcat.service.j2
dest: /usr/lib/systemd/system/tomcat.service
notify:
- Restart Tomcat
- wait port before
- name: Ensure Tomcat Service Enabled and Running
systemd:
name: tomcat
state: started
enabled: yes
daemon_reload: yes
handlers:
- name: wait port before
wait_for: port=8080 delay=5
- name: Restart Tomcat
systemd: name=tomcat state=restarted
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment