用ansible主控机控制debian12和centos8安装apache
时间: 2024-09-06 17:03:57 浏览: 76
在Ansible中,你可以使用YAML语法创建一个Playbook来从主控机控制Debian 12和CentOS 8上的Apache安装。以下是一个基本示例:
```yaml
---
- name: Install Apache on Debian and CentOS
hosts: all
become: true
vars:
os_family: "!"
tasks:
- name: Conditionally install Apache based on OS
when: ansible_os_family == 'Debian'
apt:
name: apache2
state: present
- name: Conditionally install Apache based on OS (CentOS)
when: ansible_os_family == 'RedHat'
yum:
name: httpd
state: present
- name: Restart Apache service if installed
service:
name: "{{ 'apache2' if ansible_os_family == 'Debian' else 'httpd' }}"
state: restarted
notify: restart_service
handlers:
- name: restart_service
service:
name: "{{ 'apache2' if ansible_os_family == 'Debian' else 'httpd' }}"
state: restarted
- name: Notify the success message
debug:
msg: Apache has been successfully installed.
# 如果需要在playbook中加入对应平台的额外步骤,可以在tasks中添加更多的when条件
```
在这个Playbook中,我们首先检查主机的操作系统家族,然后根据是Debian还是CentOS执行相应的包管理命令(apt或yum),并确保服务重启。`handlers`部分定义了当服务状态改变时执行的动作。
阅读全文