DEV Community

Kai Boschung for Experts Inside

Posted on

1

Automated Pi-Hole setup in Azure

The whole concept behind Pi-Hole got me hooked: A DNS sinkhole that blocks traffic I don't want (trackers, malware, etc.). And this can be done for the whole network if you set the upstream DNS server on your router to the Pi-Hole instance. So, no add blocker needed anymore.

Disclaimer: I was not able to get it working... I got the same error discussed in this thread. So, if any skilled linuxer can fix it. I'd be glad!

The instance is quickly set up in Azure thanks to this article:

I went a little further and extended the script to replace yaml placeholders via PowerShell. Line 30 to 45 shows how the replacement is done.

# Source: https://dev.to/ganesshkumar/pi-hole-in-azure-container-instances-4abf
# Download Azure CLI optionally
#Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
cd $PSScriptRoot
$subscriptionId = "1231321332144" # shown after logging in with 'az login'
$rgName = "rg-pi-hole"
$location = "WestEurope" # use 'az account list-locations' to list all locations
$storageName = "piholestorage"
$containerGroupName = "pi-hole-container-group"
$containerName = "pi-hole-container"
$yamlOriginalFileName = "deploy-pi-hole-original.yaml"
$yamlModifiedFileName = "deploy-pi-hole.yaml"
$customDnsName = "pihole"
$webPassword = "superSecretLongAndCrazyPass22..334"
az login
az account set --subscription $subscriptionId
az group create --name $rgName --location $location
az storage account create --resource-group $rgName --name $storageName --location $location --sku Standard_LRS
az storage share create --account-name $storageName --name etc-pihole
az storage share create --account-name $storageName --name etc-dnsmasq
$STORAGE_KEY=$(az storage account keys list --resource-group $rgName --account-name $storageName --query "[0].value" --output tsv)
# prepare yaml file
$stringsToReplace = @(
@("<location>", $location),
@("<container_group_name>", $containerGroupName),
@("<container_name>", $containerName),
@("<custom_large_string>", $webPassword),
@("<custom_dnsname>", $customDnsName),
@('<value of $STORAGE_KEY>', $STORAGE_KEY),
@('<storage_name>', $storageName)
)
Copy-Item -Path $yamlOriginalFileName -Destination $yamlModifiedFileName
# Replacing all placeholder in the yaml file
foreach($replace in $stringsToReplace){
(Get-Content $yamlModifiedFileName).replace($replace[0], $replace[1]) | Set-Content $yamlModifiedFileName
}
az container create --resource-group $rgName --file $yamlModifiedFileName
az container show --resource-group $rgName --name $containerGroupName --query ipAddress.ip --output tsv

The whole gist including the yaml input file is available here.

More posts on my blog: https://engineerer.ch/

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay