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

New module to pull facts from the Azure Metadata service #55527

Conversation

Projects
None yet
6 participants
@DevoKun
Copy link

commented Apr 18, 2019

SUMMARY

This is a new module for pulling Facts from the Azure metadata service.
With Amazon EC2, you can quickly pull in facts from the EC2 Metadata service using:

  tasks:
    - ec2_metadata_facts:
    - debug:
        msg:
          - "This instance is a {{ ansible_ec2_instance_type }}"
          - "Placement Region: {{ ansible_ec2_placement_region }}"
          - "AZ: {{ ansible_ec2_placement_availability_zone }}"

The new module in this pull request adds the ability to pull metadata facts in Azure:

  tasks:
    - azure_vm_metadata_facts:

    - debug:
        msg:
          - "region: {{ ansible_azure_vm_compute_location }}"
ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

azure_vm_metadata_facts

ADDITIONAL INFORMATION

A test run playbook with

- azure_vm_metadata_facts:

- debug:
    msg: "{{ ansible_facts }}"

Produces

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "azure_vm": {
                "compute": {
                    "azenvironment": "AzurePublicCloud",
                    "customdata": "",
                    "location": "eastus",
                    "name": "t1",
                    "offer": "UbuntuServer",
                    "ostype": "Linux",
                    "placementgroupid": "",
                    "plan": {
                        "name": "",
                        "product": "",
                        "publisher": ""
                    },
                    "platformfaultdomain": "0",
                    "platformupdatedomain": "0",
                    "provider": "Microsoft.Compute",
                    "publickeys": [
                        {
                            "keydata": "ssh-rsa xxxxxx",
                            "path": "/home/DevoKun/.ssh/authorized_keys"
                        }
                    ],
                    "publisher": "Canonical",
                    "resourcegroupname": "images",
                    "sku": "18.04-LTS",
                    "subscriptionid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                    "tags": "",
                    "version": "18.04.201904020",
                    "vmid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                    "vmscalesetname": "",
                    "vmsize": "Standard_B1s",
                    "zone": ""
                },
                "network": {
                    "interface": [
                        {
                            "ipv4": {
                                "ipaddress": [
                                    {
                                        "privateipaddress": "xx.x.xx.x",
                                        "publicipaddress": ""
                                    }
                                ],
                                "subnet": [
                                    {
                                        "address": "xx.x.xx.0",
                                        "prefix": "24"
                                    }
                                ]
                            },
                            "ipv6": {
                                "ipaddress": []
                            },
                            "macaddress": "xxxxxxxxxxxx"
                        }
                    ]
                }
            },
            "azure_vm_compute_azenvironment": "AzurePublicCloud",
            "azure_vm_compute_customdata": "",
            "azure_vm_compute_location": "eastus",
            "azure_vm_compute_name": "t1",
            "azure_vm_compute_offer": "UbuntuServer",
            "azure_vm_compute_ostype": "Linux",
            "azure_vm_compute_placementgroupid": "",
            "azure_vm_compute_plan_name": "",
            "azure_vm_compute_plan_product": "",
            "azure_vm_compute_plan_publisher": "",
            "azure_vm_compute_platformfaultdomain": "0",
            "azure_vm_compute_platformupdatedomain": "0",
            "azure_vm_compute_provider": "Microsoft.Compute",
            "azure_vm_compute_publickeys_keydata": "ssh-rsa xxxxx",
            "azure_vm_compute_publickeys_path": "/home/DevoKun/.ssh/authorized_keys",
            "azure_vm_compute_publisher": "Canonical",
            "azure_vm_compute_resourcegroupname": "images",
            "azure_vm_compute_sku": "18.04-LTS",
            "azure_vm_compute_subscriptionid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "azure_vm_compute_tags": "",
            "azure_vm_compute_version": "18.04.201904020",
            "azure_vm_compute_vmid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "azure_vm_compute_vmscalesetname": "",
            "azure_vm_compute_vmsize": "Standard_B1s",
            "azure_vm_compute_zone": "",
            "azure_vm_network_interface_ipv4_ipaddress_privateipaddress": "xx.x.xx.x",
            "azure_vm_network_interface_ipv4_ipaddress_publicipaddress": "",
            "azure_vm_network_interface_ipv4_subnet_address": "xx.x.xx.0",
            "azure_vm_network_interface_ipv4_subnet_prefix": "24",
            "azure_vm_network_interface_macaddress": "xxxxxxxxxxxx",
@ansibot

This comment has been minimized.

@ansibot

This comment has been minimized.

Copy link
Contributor

commented Apr 18, 2019

@DevoKun, just so you are aware we have a dedicated Working Group for azure.
You can find other people interested in this in #ansible-azure on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

click here for bot help

@Fred-sun

This comment has been minimized.

Copy link
Contributor

commented Apr 19, 2019

@yungezz @zikalino @yuwzho please help to review this PR when you're available! Thanks!

@yungezz

This comment has been minimized.

Copy link
Contributor

commented Apr 22, 2019

@DevoKun thanks for contribution. You can use existing module azure_rm_virtualmachine_facts to get Azure VM facts. It contains very verbose info. Could you pls check that module?

@ansibot ansibot removed the needs_triage label Apr 22, 2019

@DevoKun

This comment has been minimized.

Copy link
Author

commented Apr 22, 2019

@DevoKun thanks for contribution. You can use existing module azure_rm_virtualmachine_facts to get Azure VM facts. It contains very verbose info. Could you pls check that module?

Hello, @yungezz .

azure_rm_virtualmachine_facts requires API access credentials and is designed to return information about a VM by querying Azure. The intention of azure_vm_metadata_facts is to provide anonymous access to the facts provided by the Azure Metadata service in a way similar to how ec2_metadata_facts works.

azure_rm_virtualmachine_facts seems more targeted towards a scenario where Ansible is used to provision and configure Azure VM's. azure_vm_metadata_facts is more for use in the creation of configuration files on hosts where facts like the location are needed but more extensive facts like those that require user authentication are not required.

azure_vm_metadata_facts Allows Azure metadata facts to be used without installing the azure python module or the az cli tool.

@yuwzho
Copy link
Contributor

left a comment

This PR looks really good, I test this with Azure VM and get it works. Appreciate for your great work. I have some comments on the UX design and the code readable, please check.

description:
- This module fetches data from the instance metadata endpoint in Azure as per
U(https://azure.microsoft.com/en-us/blog/announcing-general-availability-of-azure-instance-metadata-service/).
The module must be called from within the Azure VM instance itself.

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

Make this sentence to the first paragraph in the description, also declare in short_description

- This module fetches data from the instance metadata endpoint in Azure as per
U(https://azure.microsoft.com/en-us/blog/announcing-general-availability-of-azure-instance-metadata-service/).
The module must be called from within the Azure VM instance itself.
- Based on the ec2_metadata_facts module.

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

No need


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['stableinterface'],
'supported_by': 'core'}

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor
ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}
self.module = module
self.uri_meta = azure_vm_metadata_uri or self.azure_vm_metadata_uri
self._data = {}
self._prefix = 'ansible_azure_vm%s'

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

Here we already assign the current host's properties to the ansible_facts, and the module is running on the azure vm, ansible_azure_vm sounds like one of the azure vms' properties, ansible_host is enough

# Metadata URL comes from:
# https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service
#
azure_vm_metadata_uri = 'http://169.254.169.254/metadata/instance?api-version=2019-02-01'

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

As the link you shared, use 2018-10-01 api version for all regions.

encoded_url = quote(url, safe='%/:=&?~#+!$,;\'@()*[]')
response, info = fetch_url(self.module, encoded_url, method="GET", force=True, headers={'Metadata': 'true'})
if info['status'] not in (200, 404):
time.sleep(3)

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

Is there any reason to add the retry logic?

elif isinstance(val, dict):
self._json_to_facts(val, str(newkey) + "_")
else:
self._data[newkey] = val

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

construct a local dict in this method and add result to to this dict, finally return it. DON'T modified a complicated global variable in different methods, this makes the code hard to read and maintain.

#
#
if isinstance(val, dict):
self._data[newkey] = self._keyfix(val)

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

for the self._data, as above comments

self.module.warn("Exception: " + str(e))


def run(self):

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor

Replace this method with fetch, with return value.


EXAMPLES = '''
# Gather Azure VM metadata facts
- azure_vm_metadata_facts:

This comment has been minimized.

Copy link
@yuwzho

yuwzho Apr 29, 2019

Contributor
- name: Gather metadata facts on Azure VM host
  azure_vm_metadata_facts:

@ansibot ansibot added the stale_ci label Apr 30, 2019

@Fred-sun

This comment has been minimized.

Copy link
Contributor

commented May 5, 2019

@DevoKun Please help recheck this PR and update according by the above comments! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.