ansible playbook安装、配置默认发布页并启动apache

本文介绍了如何通过Ansible Playbook自动化安装和配置Apache web服务器,包括设置默认发布页面并启动Apache服务,实现远程服务器管理的高效与便捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---
- name: Enable internet services
  hosts: webserver
  become: yes
  tasks:
    - name: lastst version of httpd and firewalld #检测httpd和firewalld是否安装最新版本
      yum:
        name:
          - httpd
          - firewalld
        state: latest

    - name: test html page is configured #检测是否配置默认发布页面
      copy:
        content: "Welcome to westos!\n"
        dest: /var/www/html/index.html

    - name: firewalld enabled and running #检测火墙是否开启且处于enabled状态
      service:
        name: firewalld
        enabled: true
        state: started

    - name: firewalld permit access to httpd #检测火墙是否允许httpd服务访问
      firewalld:
        service: http
        permanent: 
### 使用 Ansible Playbook 安装配置 Nagios #### 准备工作 为了成功使用 Ansible Playbook 部署 Nagios,需确保目标主机已安装正确配置Ansible 工具。此外,还需确认目标操作系统的兼容性以及必要的依赖项已经满足。 #### 创建 Playbook 文件 以下是基于 Ansible 的 Nagios 安装配置的示例 Playbook: ```yaml --- - name: Install and configure Nagios using Ansible hosts: all become: yes vars: nagios_version: "4.4.6" nagios_user: "nagios" nagios_group: "nagcmd" tasks: - name: Update apt cache (for Debian-based systems) apt: update_cache: yes when: ansible_os_family == "Debian" - name: Install required packages on Debian-based systems apt: name: "{{ item }}" state: present loop: - build-essential - wget - unzip - apache2 - php when: ansible_os_family == "Debian" - name: Install required packages on RedHat-based systems yum: name: "{{ item }}" state: present loop: - gcc - make - wget - httpd - php when: ansible_os_family == "RedHat" - name: Download Nagios Core source code get_url: url: "https://assets.nagios.com/downloads/nagioscore/releases/nagios-{{ nagios_version }}.tar.gz" dest: /tmp/nagios.tar.gz - name: Extract Nagios Core archive unarchive: src: /tmp/nagios.tar.gz dest: /usr/src/ remote_src: yes - name: Create Nagios user and group user: name: "{{ nagios_user }}" group: "{{ nagios_group }}" system: yes shell: /sbin/nologin create_home: no state: present - name: Compile and install Nagios from source command: > ./configure --with-command-group={{ nagios_group }} && make all && make install && make install-init && make install-config && make install-webconf args: chdir: "/usr/src/nagios-{{ nagios_version }}" creates: /usr/local/nagios/bin/nagios - name: Enable Apache service to start at boot systemd: name: httpd enabled: yes state: started when: ansible_os_family == "RedHat" - name: Configure firewall rules for HTTP access firewalld: service: http permanent: true immediate: true state: enabled when: ansible_distribution == 'CentOS' or ansible_distribution == 'Fedora' - name: Set default admin password for Nagios web interface lineinfile: path: /usr/local/nagios/etc/cgi.cfg regexp: '^default_user_name=' line: 'default_user_name=nagiosadmin' notify: Restart Apache handlers: - name: Restart Apache service: name: "{{ item }}" state: restarted with_items: - httpd - apache2 ``` 此 Playbook 将完成以下任务: 1. 更新包缓存安装所需的软件包[^2]。 2. 下载指定版本的 Nagios 源代码文件,解压到 `/usr/src` 目录下[^1]。 3. 创建 `nagios` 用户和组以运行 Nagios 进程[^1]。 4. 编译安装 Nagios 软件。 5. 启动 Web 接口支持的服务(如 Apache),设置防火墙规则允许外部访问[^4]。 6. 设置默认管理员密码以便登录 Nagios Web 控制面板[^3]。 #### 执行 Playbook 要执行该 Playbook,请按照如下方式调用命令行工具: ```bash ansible-playbook -i inventory_file nagios_installation.yml -e "nagios_version=4.4.6" ``` 其中 `-i inventory_file` 参数指定了包含目标主机列表的清单文件路径;而通过 `-e` 提供额外变量来覆盖默认值。 #### 测试连接 最后,在浏览器输入目标服务器 IP 地址加上端口号即可验证是否正常启动了 Nagios Web UI 页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值