---
# Copyright kubeinit contributors
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
### DOCUMENTATION
#
# title: Create an ssh config file for a list of hosts
#
# description: |-
# This task has two params for the template task below
# - config_path optional default: '~/.ssh/config'
# - file_mode optional default: '0644'
#
# and two passthrough params for the template itself
# - ssh_hosts required list of hosts
# - ssh_keytype optional default: 'rsa'
# - keypair_path optional default: '~/.ssh/id_' + ssh_keytype
# - include_paths optional default: empty list
#
# The hosts are used as hostvars keys meeting the following expectations:
#
# hostvars[host].ansible_host must be defined.
#
# A stanza will be added to the ssh config pairing each host with the
# corresponding ansible_host
#
# hostvars[host].ssh_connection_address may be defined
# hostvars[host].ssh_hostname may be defined
#
# A stanza will be added to the ssh config pairing each ansible_host with either of
# these, in the order listed. If neither is defined then the pairing will be omitted.
#
# examples: |-
# - include_role:
# name: kubeinit.kubeinit.kubeinit_prepare
# tasks_from: create_host_ssh_config
# vars:
# ssh_hosts: "{{ groups['kubeinit_hypervisors'] }}"
# keypair_path: "{{ kubeinit_cluster_keypair_path }}"
# config_path: "{{ kubeinit_cluster_ssh_config }}"
# file_mode: '0644'
- name: Create ssh config file from template
ansible.builtin.template:
src: ssh-config.j2
dest: "{{ _param_dest_path | default('~/.ssh/config') }}"
mode: "{{ _param_file_mode | default('0644') }}"
- name: Print the contents of the result
ansible.builtin.command: cat {{ _param_dest_path | default('~/.ssh/config') }}
register: _result
changed_when: "_result.rc == 0"