ansible-collections / azure

Development area for Azure Collections
https://galaxy.ansible.com/azure/azcollection
GNU General Public License v3.0
247 stars 331 forks source link

SSH key not added to created VM when `ssh_key` parameter is set #1734

Closed TheSpy0 closed 2 weeks ago

TheSpy0 commented 1 month ago
SUMMARY

When creating a virtual machine inside a DevTest Lab, an SSH public key is not added to the authorized_keys file when the ssh_key parameter is set using either a path to the public key, the contents of the key (have tried as a string and using a file lookup), or the name of an SSH key stored in Azure. The module documentation was not clear on how it is supposed to be set.

ISSUE TYPE
COMPONENT NAME

azure_rm_devtestlabvirtualmachine module

ANSIBLE VERSION
ansible [core 2.16.4]
  config file = None
  configured module search path = ['/home/the-spy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/the-spy/.local/lib/python3.12/site-packages/ansible
  ansible collection location = /home/the-spy/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/the-spy/.local/bin/ansible
  python version = 3.12.5 (main, Aug  7 2024, 00:00:00) [GCC 13.3.1 20240522 (Red Hat 13.3.1-1)] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True
COLLECTION VERSION
Collection         Version
------------------ -------
azure.azcollection 2.7.0 
CONFIGURATION
CONFIG_FILE() = None
EDITOR(env: EDITOR) = /usr/bin/nano
OS / ENVIRONMENT

Fedora release 39 (Thirty Nine)

STEPS TO REPRODUCE

Run a playbook with the task below defined, using either the contents of the SSH key, or a lookup plugin with the file path to the key.

# Only including the specific task here. Will post entire whole playbook if necessary.
- name: Create a VM within the lab
      azure.azcollection.azure_rm_devtestlabvirtualmachine:
        name: "{{ vm_name }}"
        lab_name: "{{ lab_name }}"
        resource_group: "{{ resource_group_name }}"
        notes: Virtual machine notes
        os_type: linux
        vm_size: Standard_A2_v2
        user_name: dtladmin
        password: Th!s1saP@ssw0rd
        ssh_key: "contents of ssh key" # Also tried "{{ lookup('file', '/path/to/pub/key.pub') }}"
        lab_subnet:
          virtual_network_name: "{{ vn_name }}"
          name: "{{ vn_name }}Subnet"
        disallow_public_ip_address: false
        image:
          offer: RHEL
          publisher: RedHat
          sku: 8-lvm
          os_type: Linux
          version: latest
        allow_claim: false
EXPECTED RESULTS

Expecting there to be a public key in ~/.ssh/authorized_keys file.

ACTUAL RESULTS

The ~/.ssh/authorized_keys file is present but empty.

[dtladmin@created-vm-name ~]$ cat ~/.ssh/authorized_keys 
[dtladmin@created-vm-name ~]$ 
Fred-sun commented 1 month ago

@TheSpy0 Sorry for the trouble, this is a bug, if you need ssh authorization login, you need to set I(is_authentication_with_ssh_key=True), this parameter has been added in #1736. Thank you!

Fred-sun commented 1 month ago

sample as:


- name: Create a VM within the lab
      azure.azcollection.azure_rm_devtestlabvirtualmachine:
        name: "{{ vm_name }}"
        lab_name: "{{ lab_name }}"
        resource_group: "{{ resource_group_name }}"
        notes: Virtual machine notes
        os_type: linux
        vm_size: Standard_A2_v2
        user_name: dtladmin
        password: Th!s1saP@ssw0rd
        ssh_key: "contents of ssh key" # Also tried "{{ lookup('file', '/path/to/pub/key.pub') }}"
        lab_subnet:
          virtual_network_name: "{{ vn_name }}"
          name: "{{ vn_name }}Subnet"
        disallow_public_ip_address: false
        is_authentication_with_ssh_key: True
        image:
          offer: RHEL
          publisher: RedHat
          sku: 8-lvm
          os_type: Linux
          version: latest
        allow_claim: false
TheSpy0 commented 1 month ago

@Fred-sun Thank you. To be able to test this parameter, I just need to clone your repository in the project directory and use the Add-support-is_authentication_with_ssh_key branch, correct?

Fred-sun commented 1 month ago

Yes! Please!

TheSpy0 commented 1 month ago

It works for me when using your updated collection. Thank you!