centos ansible 远程部署安装tomcat playbook
时间: 2024-10-14 12:13:45 浏览: 28
kubernets centos ansible playbook 安装 国内镜像源
CentOS 和 Ansible 结合可以方便地进行远程服务器的 Tomcat 安装和配置。Ansible 是一种强大的自动化工具,通过 Playbook(剧本文件)来定义任务序列和操作步骤。
下面是一个简单的 Ansible playbook 示例,用于在 CentOS 上安装并启动 Tomcat:
```yaml
---
- name: Install and start Apache Tomcat
hosts: your_server_ip_or_hostname
become: true
vars:
tomcat_version: '8.5.60'
tomcat_dir: '/usr/local/tomcat'
tasks:
- name: Update packages and install Java
yum:
name: java-1.8.0-openjdk-devel
update_cache: yes
- name: Download and extract Tomcat
unarchive:
src: 'https://www.apache.org/dist/tomcat/{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz'
dest: /tmp
remote_src: yes
- name: Move Tomcat to the desired location and set permissions
move:
src: '/tmp/apache-tomcat-{{ tomcat_version }}'
dest: '{{ tomcat_dir }}'
file:
owner: root
group: root
mode: '0755'
- name: Configure startup script
template:
src: templates/init.d/tomcat.j2
dest: '/etc/init.d/tomcat'
owner: root
group: root
mode: '0755'
notify: restart_tomcat
- name: Create init directory if not exists
file:
path: /etc/init.d
state: directory
mode: '0755'
- name: Enable Tomcat service at boot
command: chkconfig tomcat on
- name: Start Tomcat
service:
name: tomcat
state: started
- name: Check if Tomcat is running
command: systemctl status tomcat
register: tomcat_status
ignore_errors: yes
handlers:
- name: Restart Tomcat
service:
name: tomcat
state: restarted
```
**注意事项**:
1. 将`your_server_ip_or_hostname`替换为你实际的服务器IP地址或主机名。
2. 确保你的Ansible服务器已配置好SSH免密登录到目标服务器。
3. `templates/init.d/tomcat.j2`是一个模板文件,你需要创建它来编写启动脚本。
阅读全文