ansible-部署haproxy-httpd/lamp轮询

ansible-部署haproxy-httpd/lamp轮询

设置主机清单

[root@server ansible]# vim hosts 
[webservers]
node1
node2

[haproxy]
node3

创建角色

[root@server roles]# ansible-galaxy init haproxy
- Role haproxy was created successfully
[root@server roles]# ansible-galaxy init httpd
- Role httpd was created successfully
[root@server roles]# ls
apache  haproxy  httpd  mysql  php

在node2上部署httpd

编写角色playbook

[root@server roles]# cd httpd/
[root@server httpd]# ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars
[root@server httpd]# vim tasks/main.yml 
[root@server httpd]# cat 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 selinux
  shell: 
    cmd: setenforce 0

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

- name: set repo1
  yum_repository: 
    file: server
    name: aa
    description: aa1
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no 

- name: set repo2
  yum_repository:
    file: server
    name: bb
    description: bb1
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

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

- name: index.html
  template: 
    src: index.html.j2
    dest: /var/www/html/index.html

- name: restart httpd
  service: 
    name: httpd
    state: restarted
    enabled: yes
创建测试网页模板文件index,html.j2
[root@server ansible]# cd roles/httpd/
[root@server httpd]# vim templates/index.html.j2
[root@server httpd]# cat templates/index.html.j2 
Welcome to {{ ansible_fqdn }} on {{ ansible_ens33.ipv4.address }}

创建调用剧角色playbook

[root@server httpd]# cd /etc/ansible/
[root@server ansible]# ls
ansible.cfg  apache.yml  hosts  mysql.yml  php.yml  roles
[root@server ansible]# vim httpd.yml
[root@server ansible]# cat httpd.yml 
---
- name: use httpd role  
  hosts: node2
  roles: 
    - httpd

执行playbook

[root@server ansible]# ansible-playbook httpd.yml 

PLAY [use httpd role] **********************************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [node2]

TASK [httpd : stop firewalld] **************************************************************************
ok: [node2]

TASK [httpd : stop selinux] ****************************************************************************
ok: [node2]

TASK [httpd : stop selinux] ****************************************************************************
changed: [node2]

TASK [httpd : mount cdrom] *****************************************************************************
ok: [node2]

TASK [httpd : set repo1] *******************************************************************************
ok: [node2]

TASK [httpd : set repo2] *******************************************************************************
ok: [node2]

TASK [httpd : install] *********************************************************************************
ok: [node2]

TASK [httpd : index.html] ******************************************************************************
changed: [node2]

TASK [restart httpd] ***********************************************************************************
changed: [node2]

PLAY RECAP *********************************************************************************************
node2                      : ok=10   changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

测试

[root@server ansible]# curl node2
Welcome to node2.example.com on 192.168.87.131

部署haproxy

本地安装haproxy

因为haproxy需要修改的配置文件多,不易修改,所以在本地安装haproxy

[root@server ansible]# yum install -y haproxy

编写haproxy角色playbook

[root@server haproxy]# vim tasks/main.yml 
[root@server haproxy]# cat 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 selinux
  shell: 
    cmd: setenforce 0

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

- name: set repo1
  yum_repository: 
    file: server
    name: aa
    description: aa1
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no 

- name: set repo2
  yum_repository:
    file: server
    name: bb
    description: bb1
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

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

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

- name: restart haproxy
  service: 
    name: haproxy
    state: restarted
    enabled: yes
编写配置模本文件
[root@server ansible]# cd roles/haproxy/
[root@server haproxy]# ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars
[root@server haproxy]# cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
[root@server haproxy]# vim templates/haproxy.cfg.j2 
···
frontend main
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
{% for zbc in groups.webservers %}
server {{ hostvars[zbc].ansible_fqdn }} {{ hostvars[zbc].ansible_ens33.ipv4.address }}:80 check
{% endfor %}

创建调用角色haproxy的playbook

[root@server haproxy]# cd /etc/ansible/
[root@server ansible]# vim haproxy.yml
[root@server ansible]# cat haproxy.yml 
---
- name: get webservers facts
  hosts: webservers
- name: use haproxy
  hosts: node3
  roles: 
    - haproxy

执行

[root@server ansible]# ansible-playbook haproxy.yml 

PLAY [get webservers facts] ****************************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [node2]
ok: [node1]

PLAY [use haproxy] *************************************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [node3]

TASK [haproxy : stop firewalld] ************************************************************************
ok: [node3]

TASK [haproxy : stop selinux] **************************************************************************
ok: [node3]

TASK [haproxy : stop selinux] **************************************************************************
changed: [node3]

TASK [haproxy : mount cdrom] ***************************************************************************
ok: [node3]

TASK [haproxy : set repo1] *****************************************************************************
ok: [node3]

TASK [haproxy : set repo2] *****************************************************************************
ok: [node3]

TASK [install haproxy] *********************************************************************************
ok: [node3]

TASK [haproxy : cp config] *****************************************************************************
changed: [node3]

TASK [restart haproxy] *********************************************************************************
changed: [node3]

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=10   changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

验证

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seven凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值