add script generate config in ssh

This commit is contained in:
vrubelroman 2026-06-01 23:25:37 +03:00
parent c642a36406
commit e24ee19c19

32
generate-ssh-config.yml Normal file
View file

@ -0,0 +1,32 @@
---
- name: Generate local SSH config from Ansible inventory
hosts: localhost
gather_facts: false
vars:
ssh_config_file: "{{ lookup('env', 'HOME') }}/.ssh/config"
ssh_identity_file: "~/.ssh/id_ed25519"
ssh_host_suffix: "VM"
ssh_config_marker: "# {mark} ANSIBLE MANAGED HOME VM HOSTS"
tasks:
- name: Ensure ~/.ssh directory exists
ansible.builtin.file:
path: "{{ lookup('env', 'HOME') }}/.ssh"
state: directory
mode: "0700"
- name: Add Proxmox VM hosts to local SSH config
ansible.builtin.blockinfile:
path: "{{ ssh_config_file }}"
create: true
mode: "0600"
marker: "{{ ssh_config_marker }}"
block: |
{% for host in groups["proxmox_vms"] %}
Host {{ host }}{{ ssh_host_suffix }}
HostName {{ hostvars[host]["ansible_host"] }}
User {{ hostvars[host]["ansible_user"] | default("vrubel") }}
IdentityFile {{ ssh_identity_file }}
{% endfor %}