ansible to provision azure loadbalncer with private ip - azure

I was trying to provision an Azure load-balancer with only a private IP address in an existing subnet. I could accomplish this with the below code.
- name: create load balancer
azure_rm_loadbalancer:
resource_group: "{{resource_group_name}}"
name: my_new_lb
frontend_ip_configurations:
- name: frontendipconf0
private_ip_address:
private_ip_allocation_method: dynamic
subnet: /subscriptions/1234445sdffew11111111/resourceGroups/RG_NAME/providers/Microsoft.Network/virtualNetworks/vnet-stage/subnets/default
backend_address_pools:
- name: backend_pool1
probes:
- name: prob0
port: 80
load_balancing_rules:
- name: lbrbalancingrule0
frontend_ip_configuration: frontendipconf0
backend_address_pool: backend_pool1
frontend_port: 8000
backend_port: 443
probe: prob0
But is there a way we can provision a loadbalancer without providing a complete subnet id in the task file?

I could resolve the issue with the help of azure_rm_resource_info as well. Adding the script below.
azure_rm_resource_info:
api_version: '2018-08-01'
resource_group: "{{ resource_group_name }}"
provider: network
resource_type: virtualnetworks
resource_name: "{{ vnet_name }}"
subresource:
- type: subnets
name: "{{ subnet_name }}"
register: subnet
- name: create load balancer
azure_rm_loadbalancer:
resource_group: "{{resource_group_name}}"
name: lb_name
frontend_ip_configurations:
- name: frontendipconf0
private_ip_address:
private_ip_allocation_method: dynamic
subnet: "{{subnet.response[0].id }}"

If you want to provision the load balancer with a private IP address, then you must associate a subnet to the load balancer, so I think an existing subnet resource id is necessary. It's impossible not to provide a complete subnet id in the task file.
Update:
Here is the example to get the virtual network info:
- name: Get the Load Balancer info
azure_rm_virtualnetwork_info:
resource_group: "{{ resource_group_name }}"
name: "{{ vnet_name }}"
register: vnet
Then you can input the subnet id in the load balancer like this:
subnet: "{{ vnet.virtualnetworks[0].subnets[0].id }}"
You can get the VNet info and change the subnet you want to use. Please read the azure_rm_virtualnetwork_info example carefully.

Related

ansible failed with 'dict object' has no attribute 'ipAddress

I was trying to provision an Azure application gateway by referring to the below document. https://learn.microsoft.com/en-us/azure/developer/ansible/application-gateway-configure?tabs=ansible but instead of containers I have used a virtual machine as my backend pool.
- name: Get info of backend server 1
azure_rm_resource_info:
api_version: '2018-04-01'
resource_group: "{{ resource_group }}"
provider: compute
resource_type: virtualmachines
resource_name: "{{ vm_1_name }}"
register: vm_1_output
- name: Create instance of Application Gateway
azure_rm_appgateway:
resource_group: "{{ resource_group }}"
name: "{{ appgw_name }}"
frontend_ip_configurations:
- public_ip_address: "publicip-{{env}}-{{appgw_name}}"
name: appGatewayFrontendIP
frontend_ports:
- port: 80
name: appGatewayFrontendPort
backend_address_pools:
- backend_addresses:
- ip_address: "{{ vm_1_output.response[0].properties.ipAddress.ip }}"
name: appGatewayBackendPool
But am getting the below error.
"The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ipAddress'\n\nThe error appears to be in
I have tried changing the values IpAddress.ip, IPAddress.ip and private_ip_address.id but but still failing. Any help will be much appreciated.
Thanks
Looking at the return values for the azure_rm_resource_info module in Ansible, there doesn't seem to be an ipaddress property being returned in the response.
Use the azure_rm_networkinterface_info module to get facts for the network interface attached to the VM for retrieving the IP address. You should then be able to get to the IP with an expression similar to: networkinterface_output.networkinterfaces[0].ip_configurations[0].private_ip_address
Check this SO post that discusses a similar issue.

Deploying multiple VM's with ansible

I'm learning ansible to create Linux VM's on azure and I used this sample playbook in this link (https://learn.microsoft.com/en-us/azure/developer/ansible/vm-configure?tabs=ansible) to create one VM on azure. If I want to deploy 10 VM's exactly like this with ansible-playbook how should I do it? Please help. Thanks in advance
Update: I tried it like this but the script fails after creating two public IP addresses.
- name: Create Azure VM
hosts: localhost
connection: local
tasks:
- name: Create resource group to hold VM
azure_rm_resourcegroup:
name: TestingResource
location: eastus
- name: Create virtual network
azure_rm_virtualnetwork:
resource_group: TestingResource
name: testingvnet
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: TestingResource
name: testingsubnet
address_prefix: "10.0.1.0/24"
virtual_network: testingvnet
- name: Create public IP address
azure_rm_publicipaddress:
resource_group: TestingResource
allocation_method: Static
name: "{{ item }}" #CHANGE HERE
loop:
- testingpublicIP2
- testingpublicIP3
register: output_ip_address
#- name: Dump public IP for VM which will be created
#debug:
#msg: "The public IP is {{ output_ip_address.state.ip_address }}."
- name: Create Network Security Group that allows SSH
azure_rm_securitygroup:
resource_group: TestingResource
name: TestingSecurityGroup
rules:
- name: SSH
protocol: Tcp
destination_port_range: 22
access: Allow
priority: 1001
direction: Inbound
- name: Create virtual network interface card
azure_rm_networkinterface:
resource_group: TestingResource
name: "{{ item }}" #CHANGE HERE
loop:
- TestingNIC2
- TestingNIC3
virtual_network: testingvnet
subnet: testingsubnet
public_ip_name: "{{ item }}" #CHANGE HERE
loop:
- testingpublicIP2
- testingpublicIP3
security_group: TestingSecurityGroup
- name: Create VM
azure_rm_virtualmachine:
resource_group: TestingResource
name: "{{ item }}" #CHANGE HERE VM NAME
loop:
- TestingResource2
- TestingResource3
vm_size: Standard_B2s
admin_username: admin
admin_password: password#123
ssh_password_enabled: true
network_interfaces: "{{ item }}" #CHANGE HERE
loop:
- TestingNIC2
- TestingNIC3
image:
offer: UbuntuServer
publisher: Canonical
sku: '18.04-LTS'
version: latest
You can use the loops function to create multiple VMs through ansible as you showed in the question, but you'd better use a list variable to loop so that you don't need to write all the elements every time. And the variables also can be used for other things like resource group name, location, and so on that use multiple times in the code. Here is the example:
- hosts: localhost
vars:
resource_group: myResourceGroup
...
tasks:
- name: Create resource group to hold VM
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: eastus
...
And the variable for the loop:
loop: "{{ var_list }}"
I found out it's quite easy with terraform to deploy multiple VM's on azure just by changing one variable in the configuration file, Here's the configuration file that i used (https://github.com/RichardPhilipsRoy/terraform-azure-linuxVMs)

How to set target VM for an Azure loadbalancer inbound NAT rule with Ansible-azure?

I need to create an inbound nat rule on my loadbalancer to redirect a certain port to a virtual machine. I've created my loadbalancer like so. I'm on Ansible 2.9.6.
- name: Create loadbalancers
azure_rm_loadbalancer:
resource_group: "{{ item.lb_resource_group }}"
name: "{{ item.lb_name }}"
frontend_ip_configurations: "{{ item.lb_frontend_ip_configurations }}"
backend_address_pools: "{{ item.lb_backend_address_pools }}"
probes: "{{ item.lb_probes }}"
load_balancing_rules: "{{ item.lb_load_balancing_rules }}"
inbound_nat_rules: "{{ item.lb_inbound_nat_rules }}"
with_items:
- "{{ lbs }}"
tags:
- lb
The inbound nat-rule looks like this.
- name: "nat-rule-in"
backend_port: 821
protocol: Tcp
frontend_port: 380
frontend_ip_configuration: "lb-frontend"
I've looked in this documentation and can not find anything that says something about this. Is it not possible to set a target VM for an inbound nat-rule using Ansible or do I need to do it somewhere else?
I've also searched the VM documentation for Ansible-azure but can't find anything related to NAT rules there either.
What you need to find is not the azure_rm_virtualmachine module in Ansible, it should be the azure_rm_networkinterface module. You can configure the ip_configurations property of the azure_rm_networkinterface to set the load_balancer_backend_address_pools, this property can associate the VM to the Load Balancer.
Seems as if this really isn't an option with Ansible-azure itself.
I instead used the Azure-CLI with an SPN and ran the command through Ansible using command.
$ sudo apt-get install azure-cli
Within Ansible I used the code below to set the target VM after creating the inbound nat rule.
- name: Create inbound NAT rules
command: az network nic ip-config inbound-nat-rule add --ip-config-name <name> --resource-group <name> --lb-name <name> --nic-name <name> --inbound-nat-rule <name>
Same issue with az cli ...
It costed me a lot of time, glad I found this page. Indeed the way to (currently still) link the NAT rule to a backend is via the ipconfig object of the nic of the VM. https://learn.microsoft.com/en-us/cli/azure/network/nic/ip-config/inbound-nat-rule?view=azure-cli-latest

Error Unsupported kubernetes version when using Ansible Playbook to install AKS

I created an Ansible-Playbook to install AKS on Ubuntu .
Flowing microsoft tutorial
https://learn.microsoft.com/en-us/azure/ansible/ansible-create-configure-aks
Yml file:
- name: Create Azure Kubernetes Service
hosts: localhost
connection: local
vars:
resource_group: myResourceGroup
location: eastus
aks_name: myAKSCluster
username: azureuser
ssh_key: "your_ssh_key"
client_id: "your_client_id"
client_secret: "your_client_secret"
tasks:
- name: Create resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
- name: Create a managed Azure Container Services (AKS) cluster
azure_rm_aks:
name: "{{ aks_name }}"
location: "{{ location }}"
resource_group: "{{ resource_group }}"
dns_prefix: "{{ aks_name }}"
linux_profile:
admin_username: "{{ username }}"
ssh_key: "{{ ssh_key }}"
service_principal:
client_id: "{{ client_id }}"
client_secret: "{{ client_secret }}"
agent_pool_profiles:
- name: default
count: 2
vm_size: Standard_D2_v2
tags:
Environment: Production
But it given error:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unsupported
kubernetes version. Expected one of [u'1.11.9', u'1.11.8', u'1.10.12',
u'1.10.13', u'1.12. 8', u'1.13.5', u'1.12.7'] but got None"}
Then i switched to another Kubernetes Service which its version is 1.12.8
But it still given this error.
How can i fix it? Or how can i change the version of kubernetes service ?
you need to specify kubernetes_version according to the module wiki. this is also what the error text is telling you
- name: Create a managed Azure Container Services (AKS) cluster
azure_rm_aks:
name: "{{ aks_name }}"
location: "{{ location }}"
resource_group: "{{ resource_group }}"
dns_prefix: "{{ aks_name }}"
kubernetes_version: 1.13.5
xxx

Get private IP for Azure scale set VMs in Ansible

How can you get the private IP for a VM in am Azure scale set in Ansible?
None of these seem to have the info:
azure_rm_virtualmachinescaleset_facts
azure_rm_virtualmachinescalesetinstance_facts
azure_rm_virtualmachine_facts
azure_rm_subnet_facts
azure_rm_networkinterface_facts
in order to get the IPs of my ScaleSet instances, I take the little detour via the PrivateDNS of Azure with auto-registration. As soon as the machines are available, they will be updated in the DNS.
Code Snippet:
- name: Fetch IPs from ScaleSet instances
hosts: localhost
connection: local
vars:
resource_group: "test-lbs1"
vmss_name: "vmss"
zone_name: "my.private.dns.zone"
tasks:
#############################################
# Fetch all Instances from Scaleset
#############################################
- name: List all of the instances
azure.azcollection.azure_rm_virtualmachinescalesetinstance_info:
resource_group: "{{ resource_group }}"
vmss_name: "{{ vmss_name }}"
register: __instances
#############################################
# Fetch IP from PrivateDNS Record
#############################################
- name: Get network interfaces
azure.azcollection.azure_rm_privatednsrecordset_info:
resource_group: "{{ resource_group }}"
zone_name: "{{ zone_name }}"
relative_name: "{{ item.computer_name }}"
record_type: A
with_items: "{{ __instances.instances }}"
register: __network_ips
- debug:
var: __network_ips

Resources