---
- name: Generate '{{ ansible_user }}' user SSH key
  become: "{{ ansible_user }}"
  user:
    name: "{{ ansible_user }}"
    generate_ssh_key: yes
    ssh_key_bits: 2048
    ssh_key_file: ~/.ssh/id_rsa

- name: Read '{{ ansible_user }}' user SSH public key
  become: "{{ ansible_user }}"
  slurp:
    src: "~/.ssh/id_rsa.pub"
  register: ssh_key

- name: Authorize '{{ ansible_user }}' user SSH key on other nodes
  authorized_key:
    user: "{{ hostvars[item]['ansible_user'] }}"
    state: present
    key: "{{ ssh_key['content'] | b64decode }}"
  delegate_to: "{{ hostvars[item]['ansible_host'] }}"
  remote_user: "{{ hostvars[item]['ansible_user'] }}"
  with_items: "{{ groups['kolla-nodes'] }}"
  when: hostvars[item]['ansible_host'] != ansible_host

- name: Disable SSH host key checking in /etc/ssh/ssh_config
  blockinfile:
    create: yes
    block: |
      Host *
      StrictHostKeyChecking no
      UserKnownHostsFile=/dev/null
    dest: "/etc/ssh/ssh_config"

# - name: Scan for SSH public key on nodes
#   shell: "ssh-keyscan {{ item }},`dig +short {{ item }}`"
#   with_items: "{{ groups['kolla-nodes'] }}"
#   register: ssh_known_host
#   ignore_errors: yes
#   when: hostvars[item]['ansible_host'] != ansible_host

# - name: Add/Update the SSH public key in .ssh/known_hosts file
#   known_hosts:
#     name: "{{ item.item }}"
#     key: "{{ item.stdout }}"
#     state: "present"
#     path: "~/.ssh/known_hosts"
#   with_items: "{{ ssh_known_host.results }}"

---
- name: Generate '{{ ansible_user }}' user SSH key
  become: "{{ ansible_user }}"
  user:
    name: "{{ ansible_user }}"
    generate_ssh_key: yes
    ssh_key_bits: 2048
    ssh_key_file: ~/.ssh/id_rsa

- name: Read '{{ ansible_user }}' user SSH public key
  become: "{{ ansible_user }}"
  slurp:
    src: "~/.ssh/id_rsa.pub"
  register: ssh_key

- name: Authorize '{{ ansible_user }}' user SSH key on other nodes
  authorized_key:
    user: "{{ hostvars[item]['ansible_user'] }}"
    state: present
    key: "{{ ssh_key['content'] | b64decode }}"
  delegate_to: "{{ hostvars[item]['ansible_host'] }}"
  remote_user: "{{ hostvars[item]['ansible_user'] }}"
  with_items: "{{ groups['kolla-nodes'] }}"

- name: Disable SSH host key checking in /etc/ssh/ssh_config
  blockinfile:
    create: yes
    block: |
      Host *
      StrictHostKeyChecking no
      UserKnownHostsFile=/dev/null
    dest: "/etc/ssh/ssh_config"

Fix "gather facts" issue on same host
Fix "gather facts" issue on same host
