【无标题】

角色部署haproxy实现轮询


1,部署apache

添加主机清单

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts 
[webservers]
node1
node2

[haproxy]
node3

定义角色

[root@ansible roles]# ansible-galaxy init httpd
- Role httpd was created successfully

部署tasks任务

[root@ansible roles]# cd httpd/
[root@ansible httpd]# ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars 
[root@ansible httpd]# cat tasks/main.yml 
---
# tasks file for httpd

- name: stop firewalld
  state: stopped
  enabled: no

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

- name: selinux1
  shell:
    cmd: setenforce 0

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

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

- name: repo2
  yum_repository:
    name: appsteram
    description: appstream
    file: server
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

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

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

- name: start httpd
  service:
    name: httpd
    state: restarted
    enabled: yes

编写index.html.j2模板


[root@ansible httpd]# cat  templates/index.html.j2
welcome to {{ ansible_fqdn }} of {{ ansible_ens160.ipv4.address }}

编写启动模板

[root@ansible ansible]# cat  a.yml
---
- name: use httpd 
  hosts: node2
  roles:
    - httpd

启动

[root@ansible ansible]#  ansible-playbook a.yml

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

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

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

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

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

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

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

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

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

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

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

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

//测试 
[root@ansible ansible]# curl http://node2
welcome to node2.example.com of 192.168.17,128

在这里插入图片描述

2,部署haproxy

创建角色

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

部署tasks 任务

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

- name: stop selinux
  shell:
      cmd: setenforce 0

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

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

- name: repo2
  yum_repository:
    name: appsteram
    description: appstream
    file: server
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

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

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

- name: restart haproxy
  service:
    name: haproxy
    state: restarted
    enabled: yes

定义模板

[root@ansible ansible]# yum -y install haproxy
[root@ansible haproxy]# cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
[root@ansible haproxy]# vim templates/haproxy.cfg.j2 
{% for rww in groups.webservers %}
server {{ hostvars[gbj].ansible_fqdn }} {{hostvars[gbj].ansible_ens160.ipv4.address}}:80 check
{% endfor %}

编写启动文件

[root@ansible ansible]# vim b.yml
[root@ansible ansible]# cat b.yml
---
- name: get webservers 
  hosts : webservers

- name: use haproxy
  hosts: node3
  roles:
    - haproxy

启动

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

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

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

PLAY [use haparoxy] *****************************************************************************************************************

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

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

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

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

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

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

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

TASK [haproxy : cp] *****************************************************************************************************************
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=9    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值