I have a script that removes and then adds firewall restrictions on Azure WebApp before doing a deployment. Below you will find the script
az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012
az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012
The issue with the above command is that, suppose someone has manually removed the firewall or the firewall does not exist for that user, then the script fails with an error in this case.
Is there a way to first check all the firewalls enabled for different users, then traverse and remove each of them and then finally again add all the firewall rules for the removed users.
Can someone please help me create this script as I am just learning scripting
Thanks
Firstly, you are using Azure CLI command rather than Power Shell command.
Here is the command for removing access restriction rule using power shell:
Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "Default-Web-WestUS" -WebAppName "ContosoSite" -Name IpRule
For checking the rule exist or not, you could use Get-AzWebAppAccessRestrictionConfig.
If you want check and remove automatically, try this:
$results = (Get-AzWebAppAccessRestrictionConfig -ResourceGroupName "ResourceGroup" -Name "yourweb").MainSiteAccessRestrictions
$results
foreach($result in $results)
{
if($result){
Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup" -WebAppName "yourweb" -Name $result.RuleName
sleep 10
}
}
Related
I have a problem with using --dns-servers flag in az cli.
When I try to update more than one DNS server it gets broken
az network vnet update \
--name $VNET_name \
--subscription $SUBSCRIPTION \
--resource-group $RGRP_name \
--dns-servers "${LOCATION[3]} ${LOCATION[4]}"
the output:
IP address is not valid '0.0.0.1 0.0.0.2'
The MS documentation says:
--dns-servers
Space-separated list of DNS server IP addresses.
You can try with something like below which I tested in my environment :
$LOCATION = #(
'10.0.0.1',
'10.0.0.2',
'10.0.0.3',
'10.0.0.4'
)
$VNET_name="ansuman-vnet"
$SUBSCRIPTION = "<SubscriptionId>"
$RGRP_name="ansumantest"
az network vnet update --name $VNET_name --subscription $SUBSCRIPTION --resource-group $RGRP_name --dns-servers $LOCATION[2,3]
Output:
Your DNS IP addresses '0.0.0.1 0.0.0.2' are not usable as a real IP addresses.
IANA states that 0.0.0.0/8 (0...) is reserved as a source address only. You can get into situation where it appears you have this address but that's normally because no address has been assigned to you (by DHCP, for example).
See also Wikipedia entry on IPv4.
Try to set real --dns-servers like 8.8.8.8
Is 0.0.0.0 a valid IP address?
ok, figured it out somehow.
if you use those vars stright away without closing them withing quotation marks it all works
. ./location_arrays
declare -n LOCATION='EM21'
...
az network vnet create \
--name $VNET_name \
--subscription $SUBSCRIPTION \
--resource-group $RGRP_name \
--location ${LOCATION[2]} \
--dns-servers ${LOCATION[-2]} ${LOCATION[-1]}
I am running a script that requires the resource group and name of current AKS client config. Previously configured with az aks get-credentials ...
Current script:
(I type AKS=something and RG=SOMETHING before running)
az aks update -g $RG -n $AKSNAME ...
Wanted script:
(I type nothing before running)
AKSNAME=$(what goes here?)
RG=$(what goes here?)
az aks update -g $RG -n $AKSNAME ...
How can I load RG and AKSNAME values automatically through a shell script?
EDIT: I current assign the values to those variables by hand. I want the script to find the values automatically, corresponding to the cluster in the current context e.g. which kubectl is using.
If you just get the credential via the command az aks get-credentials .... without parameter --admin, then you can get the cluster name like this:
AKSNAME=$(kubectl config current-context)
And if you use the parameter --admin, then you need to change the command like this:
AKSNAME=$(kubectl config view --minify -o jsonpath='{.contexts[0].context.cluster}')
Then you can get the group name like this:
RG=$(az aks list --query "[?name == '$AKSNAME'].resourceGroup" -o tsv)
I need to convert below into a cli command into a CLI command and need some help.
New-AzDnsRecordSet -Name www -RecordType A -ZoneName host.com -ResourceGroupName devdnsgroup -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "10.10.10.10")
I have started with:
az network dns record-set a add-record -g devdnsgroup -z host.com --ttl 3600
However I am unsure on how to accomplish the last part: -DnsRecords (New-AzDnsRecordConfig -IPv4Address "10.10.10.10")
Here is my reference
https://learn.microsoft.com/en-us/cli/azure/network/dns/record-set/a?view=azure-cli-latest#az-network-dns-record-set-a-add-record
You can follow the command here
az network dns record-set a add-record -g MyResourceGroup -z contoso.xyz -n www -a 10.10.10.10
How can you find the size of an existing VM using the Azure CLI? I can see how to find what sizes are available, or what sizes a VM can be resized to, but not simply what the existing size is. You'd think that might be one of the details in az vm show --show-details but it's not.
In the screenshot in my comment, I use the command.
az vm show -g '<resource group name>' -n '<VM name>' -d
You could use --query 'hardwareProfile.vmSize' to get the vmSize directly.
az vm show -g '<resource group name>' -n '<VM name>' --query 'hardwareProfile.vmSize' -o tsv
Just as additional info, when using
az vm show -g <resourcegroupname> -n <vmname>
you can see which info is available to access.
For the VMSize you would notice in the responsetree:
JSONResponse
Which you can then access by using the query posted by Joy Wang.
I'm starting to write a bash script to provision a VM in a new or existing resource group so that we can enforce naming convention and configuration.
In a bash script how can I check that a resource already exists so I don't try to create it again?
# 1. If a new resource group is desired, create it now. Microsoft Docs
az group create --name $RESOURCEGROUPNAME --location $LOCATION
# 2. Create a virtual network and subnet if one has not already been created. Microsoft Docs
# Consider a separate VNet for each resource group.
# az network vnet list -output table
az network vnet create \
--resource-group $RESOURCEGROUPNAME \
--name $RESOURCEGROUPNAME-vnet \
--address-prefix 10.0.x.0/24 \
--subnet-name default \
--subnet-prefix 10.0.x.0/24
# x is the next available 3rd octet value
# 3. Create a public IP Address. Microsoft Docs
az network public-ip create \
--resource-group $RESOURCEGROUPNAME \
--name $VMNAME-ip \
--dns-name $DNSNAME
# 4. Create a network security group. Microsoft Docs
az network nsg create \
--resource-group $RESOURCEGROUPNAME \
--name $VMNAME-nsg
# 5. Create a rule to allow SSH to the machine. Microsoft Docs
az network nsg rule create \
--resource-group $RESOURCEGROUPNAME \
--nsg-name $VMNAME-nsg \
--name allow-ssh \
--protocol tcp \
--priority 1000 \
--destination-port-range 22 \
--access allow
# 6. Create a virtual NIC. Microsoft Docs
az network nic create \
--resource-group $RESOURCEGROUPNAME \
--name $VMNAME-nic \
--vnet-name $RESOURCEGROUPNAME-vnet \
--subnet default \
--public-ip-address $VMNAME-ip \
--network-security-group $VMNAME-nsg
# 7. Create an availability set, if redundancy is required. Microsoft Docs
az vm availability-set create \
--resource-group $RESOURCEGROUPNAME \
--name $AVSETNAME-as
# 8. Create the VM. Microsoft Docs
az vm create \
--resource-group $RESOURCEGROUPNAME \
--location $LOCATION \
--name $VMNAME \
--image UbuntuLTS \
--size $VMSIZE \
--availability-set $AVSETNAME-as \
--nics $VMNAME-nic \
--admin-username $ADMINUSERNAME \
--authentication-type ssh
--ssh-key-value #$SSHPUBLICKEYFILE \
--os-disk-name $VMNAME-osdisk
This should work in bash script:
if [ $(az group exists --name $RESOURCEGROUPNAME) = false ]; then
az group create --name $RESOURCEGROUPNAME --location $LOCATION
fi
In a bash script how can I check that a resource already exists so I
don't try to create it again?
We can use CLI 2.0 command az group exists to test the resource group exist or not, like this:
C:\Users\user>az group exists -n jasontest
false
In this way, before we create it, we can test the name available or not. In new resource group, we can create new Vnet and other resources.
For now, there is no CLI 2.0 command to test other resource exist or not. If you want to create resource in an existing resource group, maybe we should use CLI 2.0 command to list the resources, and use bash to make sure the resource exist or not.
You can use JMESPath queries to do this. All resource types support this, AFAIK.
For example, for VMs:
az vm list --resource-group $RESOURCEGROUPNAME --query "[?name=='$VMNAME'] | length(#)"
This will output the number of matching VMs - either 1 or 0.
You can use this to create if/else logic in bash as follows.
if [[ $(az vm list --resource-group $RESOURCEGROUPNAME --query "[?name=='$VMNAME'] | length(#)") > 0 ]]
then
echo "VM exists"
else
echo "VM doesn't exist"
fi
If a resource show command returns an empty string and a success status code (0), then the resource does not exist.
Edit: ChrisWue pointed out that this is no longer true. It must have changed since I left the Azure CLI team (it used to be a requirement that all commands worked like this). Or it may be that there is a bug for the key vault commands he mentioned below.
this work for my batch commands
call az webapp show --subscription <yoursubs> --resource-group <yourrg> --name <yourappname> -query name
if %errorlevel% == 1 (
az webapp create ...
)
As mentioned in another answer - there is no generic "exists" command. One line of reasoning I've found was that "create" is meant to be idem potent - therefor if you have a script that creates resources (for example as part of a build pipeline) it doesn't matter how often you execute it since "it will do the right thing".
If you still need to do this you can do it in shell like this (the example is for keyvault but it should work for all resource types that have a show command)
if az keyvault show -n my-keyvault -o none; then
echo "keyvault exists"
else
echo "keyvault doesn't exist"
fi
It should be noted that az will output an error message to stderr if the resource doesn't exists - this doesn't affect the check but if it bothers you then you can redirect stderr to /dev/null
In our case we needed this because we don't run the infra scripts if the setup hasn't changed (cuts our build time in half). We dectect this by creating a hash of the infra-scripts and store it in a keyvault. When the script runs it creates the keyvault (to make sure it exists) and then tries to check the secret that contains the hash. If the hash is still the same then don't run the rest of the script.
Catch is that keyvault create nukes the access policies which also includes the web-app managed identity access policy which won't get added if the rest of the script doesn't run ... so the fix is to check if the keyvault exists first and to not create it if it does.