根据 inventory 文件中主机的顺序来决定 主备服务器.note

noteId: WEBa6a252efd086d20da5289f34ea2d00c4 · 原始路径:/ALL/Ansible 自动化管理/Ansible 配置合集/Ansible 自动化管理-Linux/根据 inventory 文件中主机的顺序来决定 主备服务器.note · 图片:2 · 附件待处理:2

 
 
 
方式一:定义组
# 使用groups变量按顺序列出dns组中的主机(顺序与hosts文件相同)
使用下标来定义server和slave,下表为0(第一台主机)为dns server,下标为1和1以上为dns slave
 
 
 
 
 
方式二:不定义组
如果只有两台主机:
# 剧本:
---
- name: all
  gather_facts:
  hosts: all
  tasks:
    - name: install sshpass
      raw: apt install -y sshpass
      
    - name: get inventory file
      raw: sshpass -p "Skills39" ssh root@192.168.1.254 'grep -n Server1 /etc/ansible/hosts | cut -d ":" -f 1'
      register: Server1
      
    - name: get inventory file
      raw: sshpass -p "Skills39" ssh root@192.168.1.254 'grep -n Server2 /etc/ansible/hosts | cut -d ":" -f 1'
      register: Server2
      
    - name: tasks
      raw: echo "your tasks"
      when: {{ Server1 | int }} > {{ Server2 | int }}
 
使用 grep -n 获取 'Server1' 在 /etc/ansible/hosts 中的行号
再使用 cut 过滤 冒号“:” 前面的内容,也就是行号,最后再使用行号来比大小
 
 
上面那个可能是旧版本,有时不太生效
---
- name: dns
  hosts: Server3,Server4
  gather_facts: false
  tasks:
    - name: Install Packet
      apt:
        name:
          - bind9
          - sshpass
        update_cache: false
 
    - name: dns
      raw: sshpass -p Skill2024 ssh skill@10.1.20.12 cat /etc/ansible/hosts | grep -n Server3 | cut -d ':' -f 1
      register: Server3
      changed_when: false
 
    - name: dns
      raw: sshpass -p Skill2024 ssh skill@10.1.20.12 cat /etc/ansible/hosts | grep -n Server4 | cut -d ':' -f 1
      register: Server4
      changed_when: false
 
    - name: resolv
      raw: |
        echo "nameserver {{ ansible_host }}" > /etc/resolv.conf
      changed_when: false
 
    - name: master dns
      copy:
        src: /data/named.conf
        dest: /etc/bind
      notify: restart
      when: 
        - Server3.stdout | int < Server4.stdout | int
        - inventory_hostname == "Server3"
 
    - name: dns
      copy:
        src: /data/named.slave
        dest: /etc/bind/named.conf
      notify: restart
      when: 
        - Server3.stdout | int < Server4.stdout | int
        - inventory_hostname == "Server4"
 
    - name: dns
      copy:
        src: /data/db.dns
        dest: /etc/bind
      notify: restart
 
    - name: master dns
      copy:
        src: /data/named.conf
        dest: /etc/bind
      notify: restart
      when: 
        - Server4.stdout | int < Server3.stdout | int
        - inventory_hostname == "Server4"
 
    - name: dns
      copy:
        src: /data/named.slave
        dest: /etc/bind/named.conf
      notify: restart
      when: 
        - Server3.stdout | int < Server4.stdout | int
        - inventory_hostname == "Server3"
 
    - name: dns
      copy:
        src: /data/db.dns
        dest: /etc/bind
      notify: restart
 
  handlers: 
    - name: restart  
      systemd:
        name: bind9
        state: restarted
 
 
如果有两台以上的机器:
 
---
- name: configure dns
  hosts: all
  gather_facts: false
  tasks:
    - name: Install DNS Server
      apt:
        name: bind9
        update_cache: false
      register: reg6
      
    - name: Configure DNS Server
      copy:
        src: /etc/ansible/data/named.conf
        dest: /etc/bind
      changed_when: reg6.changed
 
    - name: Get the host
      shell: |
        cat /etc/ansible/hosts | grep ansible_host | awk '{print $2}' | head -n 1
      register: hosts
      delegate_to: localhost
      changed_when: reg6.changed
 
    - name: Configure Slave DNS
      copy:
        src: /etc/ansible/data/named.slave
        dest: /etc/bind/named.conf
      changed_when: reg6.changed
 
    - name: Configure Slave DNS
      replace:
        path: /etc/bind/named.conf
        regexp: "masters.*$"
        replace: "masters { {{ hosts.stdout }}; };"
      changed_when: reg6.changed
 
    - name: Configure DNS
      copy:
        src: /etc/ansible/data/db.example.com
        dest: /etc/bind/
      changed_when: reg6.changed
 
    - name: Configure DNS
      copy:
        src: /etc/ansible/data/db.192
        dest: /etc/bind/
      changed_when: reg6.changed
 
    - name: Configure Master DNS
      copy:
        src: /etc/ansible/data/named.conf
        dest: /etc/bind/
      delegate_to: "{{ hosts.stdout }}"
      changed_when: reg6.changed
 
    - name: Configure DNS
      raw: |
        echo "nameserver {{ hosts.stdout }}" > /etc/resolv.conf
      changed_when: reg6.changed
 
    - name: Restart DNS
      systemd:
        name: bind9
        state: restarted
      changed_when: reg6.changed
 
 
·other: