# 自动设置LB节点变量'LB_IF' - name: 注册变量 LB_IF_TMP shell: "ip a|grep '{{ inventory_hostname }}/'|awk '{print $NF}'" register: LB_IF_TMP tags: restart_lb - name: 设置变量 LB_IF set_fact: LB_IF={{ LB_IF_TMP.stdout }} tags: restart_lb - name: 创建相关目录 file: name={{ item }} state=directory with_items: - /etc/haproxy - /etc/keepalived - name: 配置 haproxy template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg tags: restart_lb - name: 配置 keepalived 主节点 template: src=keepalived-master.conf.j2 dest=/etc/keepalived/keepalived.conf when: LB_ROLE == "master" tags: restart_lb - name: 配置 keepalived 备节点 template: src=keepalived-backup.conf.j2 dest=/etc/keepalived/keepalived.conf when: LB_ROLE == "backup" tags: restart_lb - name: 安装 haproxy+keepalived package: name={{ item }} state=present with_items: - haproxy - keepalived when: 'INSTALL_SOURCE != "offline"' ignore_errors: true # 离线安装 haproxy+keepalived - import_tasks: offline.yml when: 'INSTALL_SOURCE == "offline"' - name: 修改centos的haproxy.service template: src=haproxy.service.j2 dest=/usr/lib/systemd/system/haproxy.service when: 'ansible_distribution in ["CentOS","RedHat","Amazon","Aliyun"]' tags: restart_lb - name: daemon-reload for haproxy.service shell: systemctl daemon-reload tags: restart_lb - name: 开机启用haproxy服务 shell: systemctl enable haproxy ignore_errors: true - name: 重启haproxy服务 shell: systemctl restart haproxy tags: restart_lb - name: 开机启用keepalived服务 shell: systemctl enable keepalived ignore_errors: true - name: 重启keepalived服务 shell: systemctl restart keepalived tags: restart_lb