aboutsummaryrefslogtreecommitdiff
path: root/tasks/main.yml
blob: e1cc02a7c7f5e265854a0c48f65b5ccfdb34f84d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- name: Authorize the "{{ ssh_relayed_hostname | default(ssh_hostname) }}" host key
  tags:
    - ssh
  block:
    - name: Retrieve the host's key
      ansible.builtin.command: cat /etc/ssh/ssh_host_ed25519_key.pub
      register: host_key
      delegate_to: "{{ ssh_relayed_hostname | default(ssh_hostname) }}"
      changed_when: false

    - name: Authorize the host key on a linux host
      when: ansible_facts['os_family'] != 'Windows'
      vars:
        known_hosts_path: "{{ system_known_hosts_path if system_known_hosts_file.stat.exists and system_known_hosts_file.stat.writeable else user_known_hosts_path }}"
      block:
        - name: Detect if global known hosts file is writable
          ansible.builtin.stat:
            path: "{{ system_known_hosts_path }}"
          register: system_known_hosts_file

        - name: Authorize the {{ ssh_hostname }} host key
          vars:
            host_line: "{% if ssh_port == 22 %}{{ ssh_hostname }}{% else %}[{{ ssh_hostname }}]:{{ ssh_port }}{% endif %}"
          ansible.builtin.known_hosts:
            path: "{{ known_hosts_path }}"
            name: "{{ host_line }}"
            key: "{{ host_line }} {{ host_key.stdout }}"

        - name: "Authorize the {{ ssh_hostname }} IP addresses"
          when: include_host_ip_addresses
          block:
            - name: Authorize the {{ ssh_hostname }} IPv4 Address
              vars:
                ipv4_address: "{{ lookup('community.general.dig', ssh_hostname) }}"
                host_line: "{% if ssh_port == 22 %}{{ ipv4_address }}{% else %}[{{ ipv4_address }}]:{{ ssh_port }}{% endif %}"
              ansible.builtin.known_hosts:
                path: "{{ known_hosts_path }}"
                name: "{{ host_line}}"
                key: "{{ host_line }} {{ host_key.stdout }}"

            - name: Authorize the {{ ssh_hostname }} IPv6 Address
              vars:
                ipv6_address: "{{ lookup('community.general.dig', ssh_hostname, 'qtype=AAAA') }}"
                host_line: "{% if ssh_port == 22 %}{{ ipv6_address }}{% else %}[{{ ipv6_address }}]:{{ ssh_port }}{% endif %}"
              ansible.builtin.known_hosts:
                path: "{{ known_hosts_path }}"
                name: "{{ host_line}}"
                key: "{{ host_line }} {{ host_key.stdout }}"

    - name: Authorize the host key on a windows host
      when: ansible_facts['os_family'] == 'Windows'
      vars:
        ipv4_address: "{{ lookup('community.general.dig', ssh_hostname) }}"
        ipv6_address: "{{ lookup('community.general.dig', ssh_hostname, 'qtype=AAAA') }}"
        host_line: >-
          {% if ssh_port == 22 %}{{ ssh_hostname }}{% else %}[{{ ssh_hostname }}]:{{ ssh_port }}{% endif %}"
          {% if include_host_ip_addresses %}
          ,{% if ssh_port == 22 %}{{ ipv4_address }}{% else %}[{{ ipv4_address }}]:{{ ssh_port }}{% endif %}"
          ,{% if ssh_port == 22 %}{{ ipv6_address }}{% else %}[{{ ipv6_address }}]:{{ ssh_port }}{% endif %}"
          {% endif %}
      community.windows.win_lineinfile:
        create: true
        path: "{{ windows_system_known_hosts_path }}"
        line: "{{ host_line }} {{ host_key.stdout }}"