playbook部署httpd+haproxy

本文详细介绍使用Ansible自动化工具部署HTTPD服务的过程,包括安装HTTPD、配置httpd.conf文件、启动服务、配置防火墙及测试服务可用性。同时,文章展示了如何利用模板引擎动态配置httpd.conf,并通过haproxy实现负载均衡。

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

部署httpd

[devops@server1 ansible]$ cat playbook.yml 
---
- hosts: prod
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
  
    - name: cpoy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: cconfigure httpd
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd
      service:
        name: httpd
        state: started

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

在这里插入图片描述

添加防火墙及自动访问

[devops@server1 ansible]$ cat playbook.yml 
---
- hosts: prod
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
  
    - name: cpoy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: cconfigure httpd
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd and firewalld
      service:
        name: "{{ item }}"
        state: started
      loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: http
        permanent: yes
        immediate:  yes
        state: enabled

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

- hosts: localhost
  become: no
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.14.3
        status_code: 200

变量引用,template模板引用:

---
- hosts: webserver
  vars:
    http_port: 80
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: cpoy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: cconfigure httpd
      template:
        src: templates/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd and firewalld
      service:
        name: "{{ item }}"
        state: started
loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: http
        permanent: yes
        immediate:  yes
        state: enabled

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

- hosts: localhost
  become: no
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.14.3
        status_code: 200

修改http.conf.j2

$ cp ../files/httpd.conf httpd.conf.j2
vim http.conf.j2

在这里插入图片描述
修改全局配置

vim inventory

在这里插入图片描述

[devops@server1 ansible]$ ansible-playbook playbook.yml 

在这里插入图片描述

主机: {{ ansible_facts[‘nodename’] }}

主机IP:default_ipv4addressa ddress
{{ hostvars[host][‘ansible_facts’][‘eth0’][‘ipv4’][‘address’] }}

haproxy动态加载主机

---
- hosts: webserver
  vars:
    http_port: 80
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
  
    - name: cpoy index.html
      copy:
        content: "{{ ansible_facts['hostname'] }}"
        dest: /var/www/html/index.html

    - name: cconfigure httpd
      template:
        src: templates/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd and firewalld
      service:
        name: "{{ item }}"
        state: started
      loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: http
        permanent: yes
        immediate:  yes
        state: enabled

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

- hosts: localhost
  tasks:
    - name: install haproxy
      yum:
        name: haproxy
        state: present

    - name: configure haproxy
      template:
        src: templates/haproxy.cfg.j2
        dest: /etc/haproxy/haproxy.cfg
      notify: restart haproxy
    
    - name: start haproxy
      service:
        name: haproxy
        state: started
 
  handlers:
    - name: restart haproxy
      service:
        name: haproxy
        state: restarted

监控

    stats uri /status
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
{% for host in groups['webserver'] %}
    server {{ hostvars[host]['ansible_facts']['hostname'] }} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check
{% endfor %}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值