---
- name: 更新客户端节点的/etc/hosts文件
  hosts: nomad_clients
  become: yes
  vars:
    hosts_entries:
      - ip: "100.116.158.95"
        hostnames: ["semaphore", "bj-semaphore"]
      - ip: "100.81.26.3"
        hostnames: ["ash1d"]
      - ip: "100.103.147.94"
        hostnames: ["ash2e"]
      - ip: "100.90.159.68"
        hostnames: ["ch2"]
      - ip: "100.86.141.112"
        hostnames: ["ch3"]
      - ip: "100.98.209.50"
        hostnames: ["onecloud1", "bj-onecloud1"]
      - ip: "100.120.225.29"
        hostnames: ["de"]
      - ip: "100.117.106.136"
        hostnames: ["ch4"]
      - ip: "100.116.80.94"
        hostnames: ["ash3c", "influxdb1"]
      - ip: "100.116.112.45"
        hostnames: ["browser"]
      - ip: "100.97.62.111"
        hostnames: ["hcp1", "bj-hcp1"]
      - ip: "100.122.197.112"
        hostnames: ["warden"]
  
  tasks:
    - name: 添加主机名解析到/etc/hosts文件
      lineinfile:
        path: /etc/hosts
        line: "{{ item.ip }} {{ item.hostnames | join(' ') }}"
        create: yes
        owner: root
        group: root
        mode: '0644'
      loop: "{{ hosts_entries }}"
    
    - name: 显示更新后的/etc/hosts文件内容
      command: cat /etc/hosts
      register: hosts_content
      changed_when: false
    
    - name: 显示/etc/hosts文件内容
      debug:
        var: hosts_content.stdout_lines