Python自动化运维之ansible的Haproxy+LAMP+Nagios经典案例角色说明

一 haproxy角色

1 点睛

haproxy角色主要实现了haproxy平台的部署、配置功能。

2 roles/haproxy/tasks

---
# This role installs HAProxy and configures it.

- name: Download and install haproxy and socat
  yum: name={{ item }} state=present
  with_items:
  - haproxy
  - socat

- name: Configure the haproxy cnf file with hosts
  template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg

3 说明

任务(tasks)定义了两个功能,一为安装,二为同步配置文件, 安装使用了yum模块,循环安装haproxy、socat两个工具,同时根据配置参数渲染roles/haproxy/templates/haproxy.cfg.j2模板文件,完成后同步到目标

主机/etc/haproxy/haproxy.cfg位置,状态发生变化时重启haproxy 服务,使之生效。

4 roles/haproxy/templates/haproxy.cfg.j2

global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        root
    group       root
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats level admin

defaults
    mode                    {{ mode }}
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

backend app
    {% for host in groups['lbservers'] %}
        listen {{ daemonname }} {{ hostvars[host]['ansible_' + iface].ipv4.address }}:{{ listenport }}
    {% endfor %}
    balance     {{ balance }}
    {% for host in groups['webservers'] %}
        server {{ hostvars[host].ansible_hostname }} {{ hostvars[host]['ansible_' + iface].ipv4.address }}:{{ httpd_port }}
    {% endfor %}

{{hostvars[host]['ansible_'+iface].ipv4.address}}实现了获取网卡名变量iface(group_vars/lbservers中定义)的IPv4 IP地址。

二 web角色

1 点睛

web角色主要实现了php、php-mysql、git平台部署及SELinux的配 置功能。

2 roles/web/tasks/main.yml

---
# This will install nagios

- name: install nagios
  yum: pkg={{ item }} state=present
  with_items:
   - nagios
   - nagios-plugins
   - nagios-plugins-nrpe
   - nagios-plugins-ping
   - nagios-plugins-ssh
   - nagios-plugins-http
   - nagios-plugins-mysql
   - nagios-devel
  notify: restart httpd

- name: create nagios config dir
  file: path=/etc/nagios/ansible-managed state=directory

- name: configure nagios
  copy: src=nagios.cfg dest=/etc/nagios/nagios.cfg
  notify: restart nagios

- name: configure localhost monitoring
  copy: src=localhost.cfg dest=/etc/nagios/objects/localhost.cfg
  notify: restart nagios

- name: configure nagios services
  copy: src=ansible-managed-services.cfg dest=/etc/nagios/

- name: create the nagios object files
  template: src={{ item + ".j2" }}
            dest=/etc/nagios/ansible-managed/{{ item }}
  with_items:
    - webservers.cfg
    - dbservers.cfg
    - lbservers.cfg
  notify: restart nagios

- name: start nagios
  service: name=nagios state=started enabled=yes

判断sestatus变量(roles/common/tasks/main.yml中定义)返回的 rc(运行代码)不等于0(失败),则配置selinux httpd访问远程数据库的权限,使用的是Ansible的seboolean模块,该条语句等价于命令 行“setsebool httpd_can_network_connect_db 1”,其中“persistent=yes”表 示开机自启动。

三 nagios角色

1 点睛 

nagios角色主要实现了nagios监控平台的部署。

2 roles/nagios/tasks/main.yml

---
# This will install nagios

- name: install nagios
  yum: pkg={{ item }} state=present
  with_items:
   - nagios
   - nagios-plugins
   - nagios-plugins-nrpe
   - nagios-plugins-ping
   - nagios-plugins-ssh
   - nagios-plugins-http
   - nagios-plugins-mysql
   - nagios-devel
  notify: restart httpd

- name: create nagios config dir
  file: path=/etc/nagios/ansible-managed state=directory

- name: configure nagios
  copy: src=nagios.cfg dest=/etc/nagios/nagios.cfg
  notify: restart nagios

- name: configure localhost monitoring
  copy: src=localhost.cfg dest=/etc/nagios/objects/localhost.cfg
  notify: restart nagios

- name: configure nagios services
  copy: src=ansible-managed-services.cfg dest=/etc/nagios/

- name: create the nagios object files
  template: src={{ item + ".j2" }}
            dest=/etc/nagios/ansible-managed/{{ item }}
  with_items:
    - webservers.cfg
    - dbservers.cfg
    - lbservers.cfg
  notify: restart nagios

- name: start nagios
  service: name=nagios state=started enabled=yes

template分发多个模板文件时可以使用with_items来循环同步,变量与字符使用“+”号连接。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值