云网牛站
所在位置:首页 > Linux云服务器 > 使用Ansible在Ubuntu 18.04/CentOS 7上安装Apache Tomcat

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

2019-06-02 21:19:19作者:李群稿源:云网牛站

本文介绍使用Ansible在Ubuntu 18.04/CentOS 7操作系统上安装Apache Tomcat的方法。在Linux系统(如Ubuntu/CentOS/Debian)上安装Tomcat的标准方法是手动的,但耗时,本文将讨论一种更好的方法,该方法是自动化的,可以轻松复制。同时假设你有一个CentOS 7+,Ubuntu 16.04+系统和Systemd服务管理器,此Ansible安装不适用于Upstart或Sysvinit。

 

一、安装Ansible

你的工作站的主要依赖是Ansible,使用下面的命令在Linux系统上安装Ansible:

1、CentOS/Fedora/RHEL

sudo yum install ansible

sudo dnf install ansible

2、Ubuntu/Debian/Linux Mint

sudo apt-get install -y  software-properties-common

sudo apt-add-repository --yes --update ppa:ansible/ansible

sudo apt-get update

sudo apt-get install -y ansible

3、Arch/Manjaro

$ sudo pacman -S ansible

4、macOS

sudo easy_install pip

sudo pip install ansible

参考:在Ubuntu 18.04系统中安装Ansible 2.7.5的方法

 

二、创建Ansible文件夹及创建Ansible playbook文件

1、创建Ansible文件夹

让我们为我们的项目创建一个目录:

mkdir -p ~/projects/ansible/roles

cd ~/projects/ansible/roles

我们的Ansible YAML文件将位于此父目录中。

2、创建Ansible playbook文件

我们现在需要创建ansible角色和任务作为YAML文件来编排手动排序过程的步骤:

cd ~/projects/ansible/roles

mkdir -p tomcat/{tasks,handlers,defaults,vars,templates}

tomcat角色具有以下文件夹:

tasks - 包含任务文件。

handlers - 包含处理程序文件。

defaults - 此角色的默认优先级较低的变量。

vars - 具有与角色关联的变量。

templates - 保存用于模板资源的文件。

运行tree命令看看:

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

然后让我们创建将设置完整Tomcat环境的任务。

1]、对于Debian family

$ vim tomcat/tasks/tomcat-setup-Debian.yml

加:

- name: Ensure the system can use the HTTPS transport for APT.

stat:

path: /usr/lib/apt/methods/https

register: apt_https_transport

- name: Install APT HTTPS transport.

apt:

name: "apt-transport-https"

state: present

update_cache: yes

when: not apt_https_transport.stat.exists

- name: Install basic packages

package:

name: ['vim','aptitude','bash-completion','tmux','tree','htop','wget','unzip','curl','git']

state: present

update_cache: yes

- name: Install Default Java (Debian/Ubuntu)

apt:

name: default-jdk

state: present

- name: Add tomcat group

group:

name: tomcat

- name: Add "tomcat" user

user:

name: tomcat

group: tomcat

home: /usr/share/tomcat

createhome: no

system: yes

- name: Download Tomcat

get_url:

url: "{{ tomcat_archive_url }}"

dest: "{{ tomcat_archive_dest }}"

- name: Create a tomcat directory

file:

path: /usr/share/tomcat

state: directory

owner: tomcat

group: tomcat

- name: Extract tomcat archive

unarchive:

src: "{{ tomcat_archive_dest }}"

dest: /usr/share/tomcat

owner: tomcat

group: tomcat

remote_src: yes

extra_opts: "--strip-components=1"

creates: /usr/share/tomcat/bin

- name: Copy tomcat service file

template:

src: templates/tomcat.service.j2

dest: /etc/systemd/system/tomcat.service

when: ansible_service_mgr == "systemd"

- name: Start and enable tomcat

service:

daemon_reload: yes

name: tomcat

state: started

enabled: yes

when: ansible_service_mgr == "systemd"

2]、对于RedHat family

$ vim tomcat/tasks/tomcat-setup-Debian.yml

加:

- name: Add EPEL repository to CentOS

yum:

name: epel-release

state: present

- name: Install basic packages

package:

name: ['vim','bash-completion','tmux','tree','htop','wget','unzip','curl','git']

state: present

- name: Install Java 8 CentOS

yum:

name: java-1.8.0-openjdk

state: present

- name: Add tomcat group

group:

name: tomcat

- name: Add "tomcat" user

user:

name: tomcat

group: tomcat

home: /usr/share/tomcat

createhome: no

system: yes

- name: Download Tomcat

get_url:

url: "{{ tomcat_archive_url }}"

dest: "{{ tomcat_archive_dest }}"

- name: Create a tomcat directory

file:

path: /usr/share/tomcat

state: directory

owner: tomcat

group: tomcat

- name: Extract tomcat archive

unarchive:

src: "{{ tomcat_archive_dest }}"

dest: /usr/share/tomcat

owner: tomcat

group: tomcat

remote_src: yes

extra_opts: "--strip-components=1"

creates: /usr/share/tomcat/bin

- name: Copy tomcat service file

template:

src: templates/tomcat.service.j2

dest: /etc/systemd/system/tomcat.service

when: ansible_service_mgr == "systemd"

- name: Start and enable tomcat

service:

daemon_reload: yes

name: tomcat

state: started

enabled: yes

when: ansible_service_mgr == "systemd"

- name: Start and enable firewalld

service:

name: firewalld

state: started

enabled: yes

when: ansible_service_mgr == "systemd"

- name: Open tomcat port on the firewall

firewalld:

port: 8080/tcp

permanent: true

state: enabled

immediate: yes

when: ansible_service_mgr == "systemd"

创建main.yml文件:

$ vim tasks/main.yml

---

- name: Add the OS specific variables

include_vars: "{{ item }}"

with_first_found:

- "{{ ansible_distribution }}{{ ansible_distribution_major_version }}.yml"

- "{{ ansible_os_family }}.yml"

- include_tasks: "tomcat-{{ ansible_os_family }}.yml"

3]、创建vars文件

$ vim defaults/main.yml

---

tomcat_ver: 9.0.20

tomcat_archive_url: https://archive.apache.org/dist/tomcat/tomcat-9/v{{ tomcat_ver }}/bin/apache-tomcat-{{ tomcat_ver }}.tar.gz

tomcat_archive_dest: /tmp/apache-tomcat-{{ tomcat_ver }}.tar.gz

到网站 https://tomcat.apache.org/ 的tomcat发布页面以获取最新的Tomcat 9.x。

然后添加OS系列特定变量:

$ vim tomcat/vars/Debian.yml

---

JAVA_HOME: /usr/lib/jvm/default-java

$ vim tomcat/vars/RedHat.yml

JAVA_HOME: /usr/lib/jvm/jre

4]、创建服务systemd模板

我们还需要添加Tomcat systemd服务作为模板:

vim templates/tomcat.service.j2

加:

[Unit]

Description=Tomcat

After=syslog.target network.target

[Service]

Type=forking

User=tomcat

Group=tomcat

Environment=JAVA_HOME={{ JAVA_HOME }}

Environment='JAVA_OPTS=-Djava.awt.headless=true'

Environment=CATALINA_HOME=/usr/share/tomcat

Environment=CATALINA_BASE=/usr/share/tomcat

Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid

ExecStart=/usr/share/tomcat/bin/catalina.sh start

ExecStop=/usr/share/tomcat/bin/catalina.sh stop

[Install]

WantedBy=multi-user.target

5]、添加处理程序

$ vim handlers/main.yml

- name: restart tomcat

service:

name: tomcat

state: restarted

6]、定义playbook执行文件

Playbook文件定义要执行的角色以及在哪些实例上:

cd ~/ansible/

vim tomcat-setup.yml

Paste:

- name: Tomcat deployment playbook

hosts: app-servers

become: yes

become_method: sudo

remote_user: vagrant

roles:

- tomcat

使用清单文件中定义的服务器IP地址或主机名或服务器组替换app-servers,这可以是自定义文件,例如项目目录中的主机,或全局库存文件-/etc/ansible/hosts:

$ sudo vim /etc/ansible/hosts

例:

$ cat /etc/ansible/hosts

192.168.10.11

servera.example.com

[app-servers]

server1

server2

在/etc/hosts上使用的名称的DNS映射或通过网络中的DNS服务器可解析的记录:

$ cat /etc/hosts

192.168.121.52 server1

192.168.121.108 server2

server1是CentOS 7机器,server2运行Ubuntu 18.04。

这是最终的文件夹结构:

$ cd ~/projects/ansible

$ tree

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

 

三、执行Playbook

我们准备运行tomcat playbook来开始行动:

$ cd ~/projects/ansible

$ ansible-playbook setup-tomcat.yml

我的执行输出示例,如下图:

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

通过访问8080端口上的服务器URL来测试你的安装和配置,如下:

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

至此,已成功使用Ansible在Ubuntu 18.04/CentOS 7系统上安装Apache Tomcat。

 

相关主题

在CentOS 7/Fedora 29系统上安装Apache Tomcat 9的步骤

精选文章
热门文章