Paybook部署keepalived

1、编写Playbook

cat << 'CAT_END' > install_keepalived.yaml
- hosts: httpd
  tasks:
    - name: Install keepalived server
      yum: 
        name: keepalived
        state: present
    - name: configure keepalived server
      template:
        src: ./conf/keepalived.conf.j2
        dest: /etc/keepalived/keepalived.conf
      notify: restart keepalived server
    - name: system start keepalived server
      systemd:
        name: keepalived
        state: started
        enabled: yes
  handlers:
    - name: restart keepalived server
      systemd:
        name: keepalived
        state: restarted
CAT_END

2、准备配置文件

cat <<'CAT_END'>keepalived.conf.j2
global_defs {     
    router_id {{ ansible_fqdn }}
}

vrrp_instance VI_1 {
{% if ansible_fqdn  == 'http1' %}
    state MASTER
    priority 150
{% elif ansible_fqdn  == 'http2' %}
    state Backup
    priority 100
{% endif %}

    interface ens33
    virtual_router_id 50
    advert_int 3
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        192.168.10.200
    }
}
CAT_END

3、语法检查

ansible-playbook install_keepalived.yaml --syntax-check

4、部署

]# ansible-playbook install_keepalived.yaml

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

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.18]
ok: [192.168.10.17]

TASK [Install keepalived server] *****************************************************************************************************************************
changed: [192.168.10.18]
changed: [192.168.10.17]

TASK [configure keepalived server] ***************************************************************************************************************************
changed: [192.168.10.17]
changed: [192.168.10.18]

TASK [system start keepalived server] ************************************************************************************************************************
changed: [192.168.10.18]
changed: [192.168.10.17]

RUNNING HANDLER [restart keepalived server] ******************************************************************************************************************
changed: [192.168.10.17]
changed: [192.168.10.18]

PLAY RECAP ***************************************************************************************************************************************************
192.168.10.17              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.18              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

5、查询vip是否开启

[root@http1 ~]# ip addr | grep 200
    inet 192.168.10.200/32 scope global ens33

 

posted @ 2023-05-12 15:54  小粉优化大师  阅读(15)  评论(0编辑  收藏  举报