diff --git a/defaults/main.yml b/defaults/main.yml index d9f78d1..d3ef840 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -2,7 +2,6 @@ ssh_host_key_types: - ed25519 - ecdsa - rsa -ssh_ip: "{{ansible_host}}" ssh_host_key_state: configured ssh_scan_host: "{% if ssh_ip is defined %}{{ ssh_ip }}{% else %}{{inventory_hostname}}{% endif %}" diff --git a/tasks/local.yml b/tasks/local.yml index 93f7596..d9bf8e0 100644 --- a/tasks/local.yml +++ b/tasks/local.yml @@ -4,8 +4,7 @@ local_action: known_hosts args: path: "{{ lookup('env','HOME') }}/.ssh/known_hosts" - name: "{{ item }}" + name: "{{ inventory_hostname }}" key: "{{ lookup('template','ssh_host_key.j2') }}" - with_items: "{{play_hosts}}" - when: hostvars[item].ssh_host_key_ed25519_public is defined - run_once: true + when: ssh_host_key_ed25519_public is defined + throttle: 1 diff --git a/tasks/save.yml b/tasks/save.yml index f84bd5e..67554e4 100644 --- a/tasks/save.yml +++ b/tasks/save.yml @@ -1,13 +1,14 @@ --- -- name: host_vars directory +- name: make sure host_vars directory is present for host local_action: file args: path: "{{inventory_dir}}/host_vars/{{inventory_hostname}}" state: directory -- name: host_vars file - local_action: template +- name: record ssh host key in host_vars + local_action: copy args: - src: host_vars.j2 - dest: "{{inventory_dir}}/host_vars/{{inventory_hostname}}/ssh_host_key.yaml" + content: + ssh_host_key_ed25519_public: "{{ ssh_host_key_ed25519_public }}" + dest: "{{inventory_dir}}/host_vars/{{inventory_hostname}}/ssh_host_key.auto.json" diff --git a/templates/host_vars.j2 b/templates/host_vars.j2 deleted file mode 100644 index 26af1cb..0000000 --- a/templates/host_vars.j2 +++ /dev/null @@ -1 +0,0 @@ -ssh_host_key_ed25519_public: {{ ssh_host_key_ed25519_public }} diff --git a/templates/ssh_host_key.j2 b/templates/ssh_host_key.j2 index 3abda2d..7256751 100644 --- a/templates/ssh_host_key.j2 +++ b/templates/ssh_host_key.j2 @@ -1,5 +1,5 @@ -{% if hostvars[item].ssh_ip is defined %} -{{ item }},{{ ansible_host }},{{ hostvars[item].ssh_ip }} ssh-ed25519 {{ hostvars[item].ssh_host_key_ed25519_public }} +{% if ssh_ip is defined %} +{{ inventory_hostname }},{{ ssh_ip }} ssh-ed25519 {{ ssh_host_key_ed25519_public }} {% else %} -{{ item }},{{ ansible_host }} ssh-ed25519 {{ hostvars[item].ssh_host_key_ed25519_public }} +{{ inventory_hostname }},{{ ansible_host }} ssh-ed25519 {{ ssh_host_key_ed25519_public }} {% endif %}