ansible 安装或者卸载 httpd

发布于:2025-08-11 ⋅ 阅读:(14) ⋅ 点赞:(0)

#安装

1.编写ansible剧本

vim install_httpd.yml


---
- name: Install and configure httpd
  hosts: web
  remote_user: root
  gather_facts: no
  tasks:
    - name: Install httpd
      yum:
        name: httpd
        state: present

    - name: Install configure file
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf

    - name: Modify config
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen'
        line: 'Listen 8080'
      notify:
        - Restart httpd service

    - name: Ensure /data/html directory exists
      file:
        path: /data/html
        state: directory
        mode: '0755'

    - name: Copy web HTML
      copy:
        src: files/index1.html
        dest: /data/html/index.html

    - name: Start and enable httpd service
      service:
        name: httpd
        state: started
        enabled: yes

  handlers:
    - name: Restart httpd service
      service:
        name: httpd
        state: restarted

2. 执行安装剧本

 ansible-playbook install_httpd.yml

#卸载httpd

1.编写卸载httpd的剧本

vim uninstall_httpd.yml

---
- name: Remove httpd and associated files
  hosts: web
  remote_user: root
  tasks:
    - name: Remove httpd package
      yum:
        name: httpd
        state: absent

    - name: Remove apache user
      user:
        name: apache
        state: absent

    - name: Remove httpd configuration files
      file:
        path: /etc/httpd
        state: absent

    - name: Remove web HTML
      file:
        path: /data/index.html
        state: absent

2. 执行卸载剧本

ansible-playbook uninstall_httpd.yml

 


网站公告

今日签到

点亮在社区的每一天
去签到