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 @   小粉优化大师  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示