diff --git a/roles/init-server/tasks/spawn.yml b/roles/init-server/tasks/spawn.yml index a11c854..eaa2178 100644 --- a/roles/init-server/tasks/spawn.yml +++ b/roles/init-server/tasks/spawn.yml @@ -2,14 +2,22 @@ --- # tasks file for roles/init-vps # @NOTE server deployment method is based on task tags compiled herein -# @TODO review 'loop' task attribute return values and make compliant changes - name: Finding SSH public keys for root + delegate_facts: true + delegate_to: localhost ansible.builtin.find: - paths: "{{ cnode_homedir | default('/home/' ~ ansible_user ~ '/.ssh') }}" # @TODO define 'cnode_homedir' in playbook - patterns: "{{ ['^'] | product(keys) | map('join') | list }}" + paths: "{{ local_facts['user_dir'] }}/.ssh" # @TODO define 'cnode_homedir' in playbook + patterns: "{{ ['^'] | product(ssh_keys) | map('join') | list }}" file_type: file use_regex: true register: ssh_keypairs +- name: Reducing SSH key-pair results to list of SSH public key paths + ansible.builtin.set_fact: + root_pubkey_paths: "{{ ssh_keypairs.files | selectattr('path', 'search', '\\.pub$') | map(attribute='path') | list }}" +- name: Converting SSH public key paths to their file contents + ansible.builtin.set_fact: + root_pubkeys: "{{ root_pubkeys | default([]) + [lookup('file', item)] }}" + loop: "{{ root_pubkey_paths }}" - name: Bootstrapping VPS block: - name: Creating VPS via Linode VPS service API @@ -24,11 +32,11 @@ region: "{{ origin }}" private_ip: true root_pass: "{{ password }}" - authorized_keys: "{{ ssh_keypairs.files | selectattr('path', 'search', '\\.pub$') | map(attribute='path') | map('lookup', 'file') | list }}" + authorized_keys: "{{ root_pubkeys }}" state: present register: new_instance - name: Waiting for that VPS to come online - delegate_to: "{{ new_instance.instance[ip_pref][0] }}" + delegate_to: "{{ item }}" delegate_facts: true ansible.builtin.wait_for_connection: delay: 20 @@ -36,6 +44,7 @@ vars: ansible_ssh_private_key_file: "{{ chosen_privkey | default(ssh_keypairs.files | rejectattr('path', 'search', '\\.pub$') | map(attribute='path') | list | random) }}" # @TODO define 'chosen_privkey'in playbook ansible_user: root + loop: "{{ new_instance.instance[ip_pref] }}" tags: - linode tags: @@ -50,34 +59,40 @@ tags: - unimplemented - name: Waiting for that server to come online - delegate_to: "{{ hostvars[instance]['ansible_default_' ~ ip_pref].address }}" + delegate_to: "{{ item }}" delegate_facts: true remote_user: root + when: item is ansible.utils['ip_pref'] ansible.builtin.wait_for_connection: delay: 20 timeout: 300 vars: ansible_user: root + loop: "{{ groups[instance] | default(hostvars[instance]) }}" - name: Checking if that server has required operating system - delegate_to: "{{ hostvars[instance]['ansible_default_' ~ ip_pref].address }}" + delegate_to: "{{ item }}" delegate_facts: true remote_user: root - when: ansible_facts["system"] != "Linux" + when: ansible_facts["system"] != "Linux" and item is ansible.utils['ip_pref'] ansible.builtin.fail: msg: Unsupported operating system found vars: ansible_user: root + loop: "{{ groups[instance] | default(hostvars[instance]) }}" - name: Checking if that server has required Linux distro - delegate_to: "{{ hostvars[instance]['ansible_default_' ~ ip_pref].address }}" + delegate_to: "{{ item }}" delegate_facts: true remote_user: root - when: ansible_facts["system"] == "Linux" and ansible_facts["os_family"] != "Debian" + when: ansible_facts["system"] == "Linux" and ansible_facts["os_family"] != "Debian" and item is ansible.utils['ip_pref'] ansible.builtin.fail: msg: Unsupported Linux distro found vars: ansible_user: root + loop: "{{ groups[instance] | default(hostvars[instance]) }}" + # @TODO find way to incorporate use of 'groups[instance] | default(hostvars[instance]' for + # looping without loop nesting in below task - name: Providing authorized keys for server root account - delegate_to: "{{ hostvars[instance]['ansible_default_' ~ ip_pref].address }}" + delegate_to: "{{ (groups[instance] | default(hostvars[instance]))[0] }}" delegate_facts: true remote_user: root ansible.posix.authorized_key: @@ -85,7 +100,7 @@ key: "{{ lookup('file', item) }}" state: present vars: - ansible_root: root - loop: "{{ ssh_keypairs.files | selectattr('path', 'search', '\\.pub$') | map(attribute='path') | list }}" + ansible_user: root + loop: "{{ root_pubkey_paths }}" tags: - lan