40 lines
1.1 KiB
YAML
40 lines
1.1 KiB
YAML
---
|
|
- name: Ensure homelab's SSH key is present on all reachable hosts
|
|
hosts: all
|
|
gather_facts: false
|
|
become: true
|
|
|
|
vars:
|
|
ssh_pub_key: "{{ lookup('file', '/home/homelab/.ssh/id_ed25519.pub') }}"
|
|
ssh_user: "{{ ansible_user | default('vish') }}"
|
|
ssh_port: "{{ ansible_port | default(22) }}"
|
|
|
|
tasks:
|
|
- name: Check if SSH is reachable
|
|
wait_for:
|
|
host: "{{ inventory_hostname }}"
|
|
port: "{{ ssh_port }}"
|
|
timeout: 8
|
|
state: started
|
|
delegate_to: localhost
|
|
ignore_errors: true
|
|
register: ssh_port_check
|
|
|
|
- name: Add SSH key for user
|
|
authorized_key:
|
|
user: "{{ ssh_user }}"
|
|
key: "{{ ssh_pub_key }}"
|
|
state: present
|
|
when: not ssh_port_check is failed
|
|
ignore_unreachable: true
|
|
|
|
- name: Report hosts where SSH key was added
|
|
debug:
|
|
msg: "SSH key added successfully to {{ inventory_hostname }}"
|
|
when: not ssh_port_check is failed
|
|
|
|
- name: Report hosts where SSH was unreachable
|
|
debug:
|
|
msg: "Skipped {{ inventory_hostname }} (SSH not reachable)"
|
|
when: ssh_port_check is failed
|