Ansible Tomcat Installation Continued
- The Playbook which we have written so far is as shown below
---
- name: Install tomcat 9
become: yes
hosts: appserver
vars:
- username: tomcat
- home_dir: /opt/tomcat
- default_shell: /bin/false
- tomcat_version: 9.0.60
- tomcat_latest: /opt/tomcat/latest
- tomcat_bin_directory: /opt/tomcat/latest/bin/
- tomcat_service_location: /etc/systemd/system/tomcat.service
tasks:
- name: update apt repo and cache
apt:
update_cache: yes
when: ansible_facts['os_family'] == "Debian"
- name: install openjdk 11
package:
name: "{{ java_package }}"
state: present
- name: create a tomcat user
ansible.builtin.user:
create_home: yes
home: "{{ home_dir }}"
name: "{{ username }}"
shell: "{{ default_shell }}"
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Download tomcat
ansible.builtin.get_url:
url: "https://dlcdn.apache.org/tomcat/tomcat-9/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz"
dest: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"
when: ansible_facts['os_family'] == "Debian"
- name: Extract tomcat
ansible.builtin.unarchive:
src: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"
dest: "{{ home_dir }}"
creates: "{{ home_dir }}/apache-tomcat-{{ tomcat_version }}"
group: "{{ username }}"
owner: "{{ username }}"
remote_src: yes
when: ansible_facts['os_family'] == "Debian"
- name: create symlink
ansible.builtin.file:
src: "{{ home_dir }}/apache-tomcat-{{ tomcat_version }}"
dest: "{{ tomcat_latest }}"
group: "{{ username }}"
owner: "{{ username }}"
state: link
when: ansible_facts['os_family'] == "Debian"
- name: change ownership to tomcat user
ansible.builtin.file:
path: "{{ home_dir }}"
state: directory
recurse: yes
group: "{{ username }}"
owner: "{{ username }}"
when: ansible_facts['os_family'] == "Debian"
- name: find all the .sh files in the tomcat bin directory
ansible.builtin.find:
paths: "{{ tomcat_bin_directory }}"
patterns: "*.sh"
when: ansible_facts['os_family'] == "Debian"
register: sh_files_tomcat_bin
- name: print the files from the previous
debug:
var: sh_files_tomcat_bin
when: ansible_facts['os_family'] == "Debian"
- name: Make shell scripts executable
ansible.builtin.file:
path: '{{ item.path }}'
owner: "{{ username }}"
group: "{{ username }}"
mode: '0755'
loop: "{{ sh_files_tomcat_bin.files }}"
when: ansible_facts['os_family'] == "Debian"
- name: copy the tomcat service file
ansible.builtin.copy:
src: 'tomcat.service'
dest: "{{ tomcat_service_location }}"
owner: "{{ username }}"
group: "{{ username }}"
when: ansible_facts['os_family'] == "Debian"
- name: reload the daemon, enable and start the tomcat service
ansible.builtin.systemd:
name: 'tomcat.service'
daemon_reload: yes
enabled: yes
state: started
when: ansible_facts['os_family'] == "Debian"
- When we are copying configuration files, we might have dynamic values in it, so copying the files with static information is not a good idea.
- If we can copy the file with dynamic values that is generally considered as template in configuration management.
- Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts.
- Refer Here for the templating and Refer Here for copying the template file to the remote node
- Refer Here for the changes done to accomodate the template
- To know more about ansible handlers Refer Here
- Refer Here for the addition of the meta to flush all the handlers.
- Refer Here for the changes done to accomodate tomcat users
- Made necessary changes to allow manager app and host manager app from all ip addresses Refer Here for the changes
- Refer Here for the changes added to deploy any war file to tomcat
Lets Make this playbook work for RedHat
- Refer Here for the changes made
- For the approach refer class room video
- The contents of the playbook
Exercises
- Install nop commerce on ubuntu 20.04 Refer Here
- Skip the mysql part and install the lamp stack on
- ubuntu 20.04 Refer Here
- redhat/centos 7.x Refer Here