---
- name: 分发SSH公钥到新节点
  hosts: browser,influxdb1,hcp1,warden
  become: yes
  vars:
    ssh_public_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMSUUfma8FKEFvH8Nq65XM2PZ9kitfgv1q727cKV9y5Z houzhongxu@seekkey.tech"
  
  tasks:
    - name: 确保 .ssh 目录存在
      file:
        path: "/root/.ssh"
        state: directory
        mode: '0700'
        owner: root
        group: root
    
    - name: 添加SSH公钥到 authorized_keys
      copy:
        content: "{{ ssh_public_key }}"
        dest: "/root/.ssh/authorized_keys"
        mode: '0600'
        owner: root
        group: root
    
    - name: 验证SSH公钥已添加
      command: cat /root/.ssh/authorized_keys
      register: ssh_key_check
      changed_when: false
    
    - name: 显示SSH公钥内容
      debug:
        var: ssh_key_check.stdout_lines