Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure_rm_virtualmachine: better support for User Assigned and System Assigned identities #1177

Merged
merged 13 commits into from
Jun 15, 2023

Conversation

cpiment
Copy link
Contributor

@cpiment cpiment commented Jun 2, 2023

SUMMARY

Allows to set/unset User and System managed identities to Virtual Machines

Fixes #1173

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

azure_rm_virtualmachine

ADDITIONAL INFORMATION

Before

- name: Create minimal VM with UserAssigned Identity 
  azure_rm_virtualmachine:
    allocated: no
    resource_group: "{{ resource_group }}"
    name: "{{ vm_name }}"
    vm_identity: 'SystemAssigned'
    admin_username: "testuser"
    ssh_password_enabled: false    
    public_ip_allocation_method: Disabled
    location: westeurope
    network_interface_names:
      - name: "{{ interface_name }}-identity"
        resource_group: "{{ resource_group }}"
    ssh_public_keys:
      - path: /home/testuser/.ssh/authorized_keys
        key_data: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfoYlIV4lTPZTv7hXaVwQQuqBgGs4yeNRX0SPo2+HQt9u4X7IGwrtXc0nEUm6LfaCikMH58bOL8f20NTGz285kxdFHZRcBXtqmnMz2rXwhK9gwq5h1khc+GzHtdcJXsGA4y0xuaNcidcg04jxAlN/06fwb/VYwwWTVbypNC0gpGEpWckCNm8vlDlA55sU5et0SZ+J0RKVvEaweUOeNbFZqckGPA384imfeYlADppK/7eAxqfBVadVvZG8IJk4yvATgaIENIFj2cXxqu2mQ/Bp5Wr45uApvJsFXmi+v/nkiOEV1QpLOnEwAZo6EfFS4CCQtsymxJCl1PxdJ5LD4ZOtP xiuxi.sun@qq.com"
    vm_size: Standard_D4s_v3
    virtual_network: "{{ network_name }}-identity"
    image:
      offer: UbuntuServer
      publisher: Canonical
      sku: 16.04-LTS
      version: latest

Now

- name: Create minimal VM with UserAssigned Identity 
  azure_rm_virtualmachine:
    allocated: no
    resource_group: "{{ resource_group }}"
    name: "{{ vm_name }}"
    vm_identity:
      type: UserAssigned
      user_assigned_identities:
        id:
        - "{{ user_identity_1 }}"
    admin_username: "testuser"
    ssh_password_enabled: false    
    public_ip_allocation_method: Disabled
    location: westeurope
    network_interface_names:
      - name: "{{ interface_name }}-identity"
        resource_group: "{{ resource_group }}"
    ssh_public_keys:
      - path: /home/testuser/.ssh/authorized_keys
        key_data: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfoYlIV4lTPZTv7hXaVwQQuqBgGs4yeNRX0SPo2+HQt9u4X7IGwrtXc0nEUm6LfaCikMH58bOL8f20NTGz285kxdFHZRcBXtqmnMz2rXwhK9gwq5h1khc+GzHtdcJXsGA4y0xuaNcidcg04jxAlN/06fwb/VYwwWTVbypNC0gpGEpWckCNm8vlDlA55sU5et0SZ+J0RKVvEaweUOeNbFZqckGPA384imfeYlADppK/7eAxqfBVadVvZG8IJk4yvATgaIENIFj2cXxqu2mQ/Bp5Wr45uApvJsFXmi+v/nkiOEV1QpLOnEwAZo6EfFS4CCQtsymxJCl1PxdJ5LD4ZOtP xiuxi.sun@qq.com"
    vm_size: Standard_D4s_v3
    virtual_network: "{{ network_name }}-identity"
    image:
      offer: UbuntuServer
      publisher: Canonical
      sku: 16.04-LTS
      version: latest

…ties

First version, still not complete. Working:
* change UserAssigned for SystemAssigned
* add UserAssigned to SystemAssigned
* Remove SystemAssigned and leave UserAssigned
* Tested None vm_identity (remove identity)
* Checks pressence of 'user_assigned_identities' when UserAssigned 'type' is used
* user_assigned_identities has two fields: id (list) and append (bool)
* you can append new managed identities to existing with append=True
* you can add SystemAssigned without removing current appended UserAssigned
Tasks to do
* Review and clean up
* Add comments
* Leave test main.yaml as it was
* Modify inventory to use a custom subnet
* Commented code
* Removed self.log sentences
* Set options defaults
* hosts of tests of azure_rm_virtualmachine changed back to 'all'
@cpiment
Copy link
Contributor Author

cpiment commented Jun 5, 2023

I have found out that the user managed identities can be created during the tests using azure_rm_resource module, so I have modified the tests and edited the PR to remove the prerrequisites for the testing to succeed.

plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
@Fred-sun
Copy link
Collaborator

Fred-sun commented Jun 6, 2023

@cpiment Please! Thanks you very much!

@Fred-sun Fred-sun added medium_priority Medium priority new_feature New feature requirments labels Jun 6, 2023
Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>
@cpiment
Copy link
Contributor Author

cpiment commented Jun 6, 2023

@Fred-sun I have just commited your changes, thank you so much!

plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
plugins/modules/azure_rm_virtualmachine.py Outdated Show resolved Hide resolved
elif 'UserAssigned' in self.vm_identity.get('type') and len(self.vm_identity.get('user_assigned_identities',{}).get('id',[])) == 0:
# Fail if append is False
if vm_identity_user_assigned_append is False:
self.fail("UserAssigned specified but no User Assigned IDs provided and no UserAssigned identities are currently assigned to the VM")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long (165 characters)

Suggested change
self.fail("UserAssigned specified but no User Assigned IDs provided and no UserAssigned identities are currently assigned to the VM")
self.fail("UserAssigned specified but no User Assigned IDs provided and no UserAssigned identities are currently assigned to the VM")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have divided this line in two. Do you need me to rephrase the error message? If code reaches this point, the module arguments specify that a User Assigned Managed Identity has to be appended to the VM, but there aren't identities in the module arguments nor in the current VM configuration

# Fail if append is False
if vm_identity_user_assigned_append is False:
self.fail("UserAssigned specified but no User Assigned IDs provided and no UserAssigned identities are currently assigned to the VM")
# If append is true, user is changing from 'UserAssigned' to 'SystemAssigned, UserAssigned' and wants to keep current UserAssigned identities
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long (169 characters)

Suggested change
# If append is true, user is changing from 'UserAssigned' to 'SystemAssigned, UserAssigned' and wants to keep current UserAssigned identities
# If append is true, user is changing from 'UserAssigned' to 'SystemAssigned, UserAssigned' and wants to keep current UserAssigned identities

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have divided this line in two

cpiment and others added 2 commits June 6, 2023 19:00
Code style changes

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>
@@ -1813,6 +1897,49 @@ def exec_module(self, **kwargs):
if self.license_type is not None:
vm_resource.license_type = self.license_type

if self.vm_identity is not None:
# If 'append' is set to True save current user assigned managed identities to use later
if self.vm_identity.get('user_assigned_identities', {}) is not None and self.vm_identity.get('user_assigned_identities', {}).get('append', False) is True:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (178 > 160 characters)

Suggested change
if self.vm_identity.get('user_assigned_identities', {}) is not None and self.vm_identity.get('user_assigned_identities', {}).get('append', False) is True:
if self.vm_identity.get('user_assigned_identities', {}) is not None and self.vm_identity.get('user_assigned_identities', {}).get('append', False) is True:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have splitted this line in two

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>
@cpiment
Copy link
Contributor Author

cpiment commented Jun 12, 2023

Hi @Fred-sun

Is there anything else to review?

Thanks!

@Fred-sun
Copy link
Collaborator

@cpiment Perfact! I will push for merged! Thank you very much!

@Fred-sun Fred-sun added the ready_for_review The PR has been modified and can be reviewed and merged label Jun 13, 2023
@xuzhang3
Copy link
Collaborator

@cpiment LGTM

@xuzhang3 xuzhang3 merged commit ef944d9 into ansible-collections:dev Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium_priority Medium priority new_feature New feature requirments ready_for_review The PR has been modified and can be reviewed and merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

azure_rm_virtualmachine: Ability to modify/unset User Assigned and System Assigned identities on VMs
3 participants