---
# tasks file for roles/init_lxd
- name: Install lxd
  become: true
  ansible.builtin.snap:
    name: lxd
    classic: yes

- register: SSH_KEY
  become: true
  user:
    name: "lxd"
    generate_ssh_key: yes
    ssh_key_type: rsa
    ssh_key_bits: 4096
    ssh_key_file: "{{ssh_key_file}}"
    force: no

- ansible.builtin.tempfile: state=file
  register: temp_lxd_init

- ansible.builtin.debug:
    var: hostvars[inventory_hostname]['ansible_default_ipv4']['address']
    verbosity: 1

#- delegate_to: localhost
#  delegate_facts: true
#  ansible.builtin.copy:
#    content: "{{hostvars[inventory_hostname]}}"
#    dest: ./test.json

- ansible.builtin.template: src="{{lxd_init_template_config_path}}" dest="{{temp_lxd_init.path}}"
  delegate_to: localhost

## tira el lxd init con los parametros
- name: "Preseeding LXD Init with template: {{lxd_init_template_config_path}}"
  become: true
  ansible.builtin.shell: |
    chmod 775 {{ssh_key_file}} {{ssh_key_file}}.pub
    cat <<EOF | lxd init --preseed
    {{ lookup('file', temp_lxd_init.path) }}
    EOF
  register: lxd_init_output

- ansible.builtin.debug:
    var: lxd_init_output
    verbosity: 1