使用Ansible在Ubuntu 20.04/18.04/CentOS 7上安装Apache Tomcat

时间:2020-02-23 14:31:17  来源:igfitidea点击:

Apache Tomcat是旨在提供Java网页的免费开放源代码HTTP服务器。 Tomcat是Java Servlet,JavaServer Pages,Java Expression Language和Java WebSocket技术的实现。它被广泛部署,并为世界各地的各种关键任务Web应用程序提供支持。

在Linux系统(例如Ubuntu/CentOS/Debian)上安装Tomcat的标准方法是手动且耗时的。本指南将讨论一种更好的方法,该方法是自动化的,并且可以轻松复制。

环境设定

我假设我们有一个带有Systemd服务管理器的CentOS 7 +,Ubuntu 16.04+系统。此Ansible安装不适用于Upstart或者Sysvinit。

安装Ansible

对我们工作站的主要依赖是Ansible。使用以下共享命令在Linux系统上安装Ansible。

###### CentOS  ######
sudo yum -y install epel-release && sudo yum -y install ansible

###### Fedora  ######
sudo dnf -y install ansible

###### Ubuntu/Linux Mint ######
sudo apt -y update
sudo apt -y install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt update
sudo apt -y install ansible

###### Debian ######
sudo apt -y update
sudo apt -y software-properties-common
echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo apt update
sudo apt install ansible

###### Arch/Manjaro ######
$sudo pacman -S ansible

###### macOS ######
sudo easy_install pip
sudo pip install ansible

确认安装正确:

$ansible --version 
ansible 2.9.2
   config file = /etc/ansible/ansible.cfg
   configured module search path = [u'/home/ubuntu/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
   ansible python module location = /usr/lib/python2.7/dist-packages/ansible
   executable location = /usr/bin/ansible
   python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]

从Github克隆Ansible角色

tomcat ansible角色可以在Github上公开使用。将其克隆到工作站。

git clone https://github.com/jmutai/tomcat-ansible.git
cd tomcat-ansible

更新库存,例如:

$vim hosts
[tomcat-nodes]
192.168.20.55

更新剧本文件中的变量设置Tomcat版本,远程用户和Tomcat UI访问凭据

$vim tomcat-setup.yml
--
- name: Tomcat deployment playbook
  hosts: tomcat-nodes       # Inventory hosts group/server to act on
  become: yes               # If to escalate privilege
  become_method: sudo       # Set become method
  remote_user: root         # Update username for remote server
  vars:
    tomcat_ver: 9.0.30                          # Tomcat version to install
    ui_manager_user: manager                    # User who can access the UI manager section only
    ui_manager_pass: theitroad@localhost      # UI manager user password
    ui_admin_username: admin                    # User who can access bpth manager and admin UI sections
    ui_admin_pass: theitroad@localhost          # UI admin password
  roles:
    - tomcat

在发行页面上检查Tomcat版本。

当使用非root远程用户时,begin_method是必需的。

become: yes
become_method: sudo

使用Ansible安装Apache Tomcat 9

更新所有值后,即可对节点运行该剧本。

使用ssh键以root用户身份执行的Playbook:

$ansible-playbook -i hosts tomcat-setup.yml

以密码的root用户身份执行Playbook:

$ansible-playbook -i hosts tomcat-setup.yml --ask-pass

以sudo用户身份使用密码执行的Playbook:

$ansible-playbook -i hosts tomcat-setup.yml --ask-pass --ask-become-pass

使用ssh密钥和sudo密码以sudo用户身份执行的Playbook:

$ansible-playbook -i hosts tomcat-setup.yml --ask-become-pass

使用ssh密钥和无密码sudo以sudo用户身份执行的Playbook:

$ansible-playbook -i hosts tomcat-setup.yml --ask-become-pass

成功的安装输出将显示类似于以下的输出。

PLAY [Tomcat deployment playbook] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****

TASK [Gathering Facts] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
Enter passphrase for key '/var/home/jkmutai/.ssh/id_rsa': 
ok: [ubuntu01]

TASK [tomcat : Add the OS specific variables] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
ok: [ubuntu01] => (item=/tmp/tomcat-ansible/tmp2/tomcat-ansible/roles/tomcat/vars/Debian.yml)

TASK [tomcat : include_tasks] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
included: /tmp/tomcat-ansible/tmp2/tomcat-ansible/roles/tomcat/tasks/tomcat-setup-Debian.yml for ubuntu01

TASK [tomcat : Ensure the system can use the HTTPS transport for APT.] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
ok: [ubuntu01]

TASK [tomcat : Install APT HTTPS transport.] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
skipping: [ubuntu01]

TASK [tomcat : Install basic packages] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
[WARNING]: Updating cache and auto-installing missing dependency: python-apt

changed: [ubuntu01]

TASK [tomcat : Install Default Java (Debian/Ubuntu)] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
changed: [ubuntu01]

TASK [tomcat : Add tomcat group] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
changed: [ubuntu01]

TASK [tomcat : Add "tomcat" user] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
changed: [ubuntu01]

TASK [tomcat : Download Tomcat] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
changed: [ubuntu01]

TASK [tomcat : Create a tomcat directory] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
changed: [ubuntu01]

TASK [tomcat : Extract tomcat archive] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
changed: [ubuntu01]

TASK [tomcat : Copy tomcat service file] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
changed: [ubuntu01]

TASK [tomcat : Start and enable tomcat] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
changed: [ubuntu01]

TASK [tomcat : Set UI access credentials] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
changed: [ubuntu01]

TASK [tomcat : Allow access to Manager and Host Manager apps from any IP] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
changed: [ubuntu01] => (item=/usr/share/tomcat/webapps/host-manager/META-INF/context.xml)
changed: [ubuntu01] => (item=/usr/share/tomcat/webapps/manager/META-INF/context.xml)

RUNNING HANDLER [tomcat : restart tomcat] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
changed: [ubuntu01]

PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
ubuntu01                   : ok=16   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

测试Tomcat安装

访问端口8080上的服务器URL。以测试tomcat的安装和配置.Tomcat Web应用程序管理器仪表板:http://<domain_or_IP_address>:8080/manager/htmlTomcat虚拟主机管理器仪表板:http://<domain_or_IP_address> :8080/host-manager/html