ansible: Add play to run produce known_hosts file using ssh-keyscan
authorKienan Stewart <kstewart@efficios.com>
Wed, 9 Aug 2023 18:10:26 +0000 (14:10 -0400)
committerKienan Stewart <kstewart@efficios.com>
Wed, 9 Aug 2023 20:03:54 +0000 (16:03 -0400)
Change-Id: Iaec237bb973d0f94717b24ea624bc87ea701e6e3

automation/ansible/playbooks/known-hosts.yml [new file with mode: 0644]

diff --git a/automation/ansible/playbooks/known-hosts.yml b/automation/ansible/playbooks/known-hosts.yml
new file mode 100644 (file)
index 0000000..e490a5f
--- /dev/null
@@ -0,0 +1,22 @@
+---
+# Note: this playbook requires ansible-core >= 2.11
+#
+- name: Generate local known_hosts files using ssh-keyscan
+  hosts: localhost
+  tasks:
+    - block:
+        - name: Scan keys
+          ansible.builtin.command:
+            argv: "{{ ['ssh-keyscan' ] + all_hosts }}"
+          register: keyscan_results
+        - ansible.builtin.known_hosts:
+            path: ../known_hosts
+            name: "{{ keyscan_host }}"
+            key: "{{ keyscan_key }}"
+          when: keyscan_results.rc == 0
+          vars:
+            keyscan_host: "{{ item | split(' ') | first }}"
+            keyscan_key: "{{ item }}"
+          with_items: "{{ keyscan_results.stdout | split('\n') }}"
+      vars:
+        all_hosts: "{{ groups['all'] | difference(['localhost']) }}"
This page took 0.024567 seconds and 4 git commands to generate.