ansible 远程批量安装 tomcat
·
ansible 远程批量安装 tomcat
| 角色 | IP | 主机名 | 软件 |
|---|---|---|---|
| ansible控制 | 192.168.98.165 | ansible1 | ansible |
| 节点1 | 192.168.98.166 | ansible2 | |
| 节点2 | 192.168.98.167 | ansible3 |
一、准备 ansible 环境,安装好 ansible
1.1 安装 epel 源
dnf install https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm
[root@ansible1 ~]# dnf install https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm -y
[root@ansible1 ~]# ls /etc/yum.repos.d/
dnf.repo docker-ce.repo epel-cisco-openh264.repo epel.repo epel-testing.repo redhat.repo
1.2 安装anible
dnf install ansible -y
[root@ansible1 ~]# dnf install ansible -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.
.....
1.3 验证安装是否成功
ansible --version
[root@ansible1 ~]# ansible --version
ansible [core 2.14.17]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.19 (main, Aug 23 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
1.4 配置目标主机,编辑映射关系
编辑 /etc/hosts 文件,把需要安装 tomcat 的服务器 IP 添加进去
[root@ansible1 ~]# vim /etc/hosts
[root@ansible1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.98.165 ansible1
192.168.98.166 ansible2
192.168.98.167 ansible3
二、使用Playbook来实现tomcat远程批量安装
2.1 创建项目目录、清单目录及文件
[root@ansible1 ~]# ls
apache-tomcat-11.0.9.tar.gz jdk-21_linux-x64_bin.tar.gz
[root@ansible1 ~]# mkdir -p remote_dnftomcat/inventory
[root@ansible1 ~]# ls
apache-tomcat-11.0.9.tar.gz jdk-21_linux-x64_bin.tar.gz remote_dnftomcat
[root@ansible1 ~]# cd remote_dnftomcat/inventory/
[root@ansible1 inventory]# ls
[root@ansible1 inventory]# vim hosts
[root@ansible1 inventory]# cat hosts
[server]
ansible[2:3]
[root@ansible1 inventory]# ls
hosts
[root@ansible1 inventory]# cd ..
[root@ansible1 remote_dnftomcat]# tree .
.
└── inventory
└── hosts
1 directory, 1 file
2.2 创建 ansible.cfg 配置文件
与 inventory 目录同级
[root@ansible1 remote_dnftomcat]# vim ansible.cfg
[root@ansible1 remote_dnftomcat]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
[root@ansible1 remote_dnftomcat]# ls
ansible.cfg inventory
[root@ansible1 remote_dnftomcat]# tree .
.
├── ansible.cfg
└── inventory
└── hosts
1 directory, 2 files
2.3 创建 playbook.yaml 文件
- 创建 下载目录(file)
- 下载 JDK、tomcat(get_url)
- 解压 JDK、tomcat(unarchive)[可用 unarchive 模块或者 shell 模块,使用 tar 命令]
- 部署 JDK、tomcat
- 下载 JDK
进入oracle官网: oracle,复制链接,回到虚拟机 wget 下载
下载:wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
# 下载JDK
[root@ansible1 ~]# wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
--2025-07-05 21:28:41-- https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
Resolving download.oracle.com (download.oracle.com)... 184.30.156.124
Connecting to download.oracle.com (download.oracle.com)|184.30.156.124|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 197677861 (189M) [application/x-gzip]
Saving to: ‘jdk-21_linux-x64_bin.tar.gz’
jdk-21_linux-x64_bin.tar. 100%[=====================================>] 188.52M 1.91MB/s in 96s
2025-07-05 21:30:18 (1.96 MB/s) - ‘jdk-21_linux-x64_bin.tar.gz’ saved [197677861/197677861]
- 解压 JDK
解压:tar -zxf jdk-21_linux-x64_bin.tar.gz -C /usr/local
# 解压
[root@ansible1 ~]# tar -zxf jdk-21_linux-x64_bin.tar.gz -C /usr/local
/etc/profile
vim /etc/profile 在末行添加以下内容:
- 部署JDK
export JAVA_HOME=/usr/local/jdk-21.0.6/,export:导出JAVA的安装目录
export PATH=$PATH:$JAVA_HOME/bin,要将JAVA_HOME这个命令放到PATH中
- 下载 tomcat
进入tomcat官网: tomcat,复制链接,回到虚拟机wget下载
下载:wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.9/bin/apache-tomcat-11.0.9.tar.gz
解压:tar -zxf apache-tomcat-11.0.5.tar.gz -C /usr/local/
[root@ansible1 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.9/bin/apache-tomcat-11.0.9.tar.gz
--2025-07-05 21:44:10-- https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.9/bin/apache-tomcat-11.0.9.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14109090 (13M) [application/x-gzip]
Saving to: ‘apache-tomcat-11.0.9.tar.gz’
apache-tomcat-11.0.9.tar. 100%[=====================================>] 13.46M 36.4KB/s in 5m 26s
2025-07-05 21:49:36 (42.3 KB/s) - ‘apache-tomcat-11.0.9.tar.gz’ saved [14109090/14109090]
# 解压
[root@ansible1 ~]# tar -zxf apache-tomcat-11.0.9.tar.gz -C /usr/local/
[root@ansible1 ~]# ls
apache-tomcat-11.0.9.tar.gz jdk-21_linux-x64_bin.tar.gz
-
解压 tomcat 包
-
部署 tomcat
export TOMCAT_HOME=/usr/local/apache-tomcat-11.0.5/
export PATH=$PATH:$TOMCAT_HOME/bin
- 刷新文件
刷新文件让配置生效,验证 JDK 是否安装成功、tomcat 是否配置成功
source /etc/profile
java -version
- 启动 tomcat
startup.sh
- 剧本(playbook)内容:
[root@ansible1 remote_dnftomcat]# vim playbook.yaml
2.4 执行 playbook
2.5
2.6 测试安装结果
更多推荐



所有评论(0)