ansible部署haproxy实现lamp和apache两个站点的轮询

ansible部署haproxy实现lamp和apache两个站点的轮询

node2
部署apache

[root@ansible roles]# ansible-galaxy init httpd
[root@ansible ansible]# vim roles/httpd/tasks/main.yml
[root@ansible ansible]# cat roles/httpd/tasks/main.yml
---
# tasks file for httpd
- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell: sentenforce 0

- name: mount
  mount:
    src: /dev/cdrom
    path: /mnt
    fstype: iso9660
    state: mounted

- name: del yum_repository
  shell: rm -rf /etc/yum.repos.d/*

- name: yum_repository
  yum_repository:
    file: server
    name: baseos
    description: baseos
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no

- name: yum_repository1
  yum_repository:
    file: server
    name: AppStream
    description: AppStream
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

- name: install httpd
  yum:
    name: httpd
    state: present

- name: enabled httpd
  service:
    name: httpd
    enabled: yes
    state: restarted
[root@ansible ansible]# vim httpd.yml
[root@ansible ansible]# cat httpd.yml
---
- name: httpd
  hosts: node2
  roles:
    - httpd
[root@ansible ansible]# ansible-playbook httpd.yml
PLAY RECAP *********************************************************************
node2                      : ok=9    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


node3
配置haproxy

[root@ansible roles]# ansible-galaxy init haproxy
[root@ansible ansible]# yum -y install haproxy
[root@ansible ansible]# cp  /etc/haproxy/haproxy.cfg /etc/ansible/roles/haproxy/templates/haproxy.cfg.j2
[root@ansible ansible]# vim roles/haproxy/templates/haproxy.cfg
 bind *:80
backend app
    balance     roundrobin
{% for webservers in groups.webservers %}
server {{ hostvars[webservers].ansible_fqdn }} {{ hostvars[webservers].ansible_ens33.ipv4.address }}:80  check
{% endfor %}
[root@ansible ansible]# cat roles/haproxy/tasks/main.yml
---
# tasks file for haproxy
- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell: setenforce 0

- name: mount
  mount:
    src: /dev/cdrom
    path: /mnt
    fstype: iso9660
    state: mounted

- name: del yum_repository
  shell: rm -rf /etc/yum.repos.d/*

- name: yum_repository
  yum_repository:
    file: server
    name: baseos
    description: baseos
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no

- name: yum_repository1
  yum_repository:
    file: server
    name: AppStream
    description: AppStream
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no


- name: install haproxy
  yum:
    name: haproxy
    state: present

- name: template
  template:
    src: haproxy.cfg.j2
    dest: /etc/haproxy/haproxy.cfg

- name: restat haproxy
  service:
    name: haproxy
    state: restarted
    enabled: yes
[root@ansible ansible]# vim haproxy.yml
[root@ansible ansible]# cat haproxy.yml
---
- name:
  hosts: webservers

- name:
  hosts: node3
  roles:
    - haproxy
[root@ansible ansible]# ansible-playbook haproxy.yml

PLAY RECAP *********************************************************************
node1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
node2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
node3                      : ok=11   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

浏览器访问测试
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值