The Linux VM is created using the Azure Pipeline. There is one folder which is created dynamically within the /data/config folder. The requirement is to return the name of this folder to the DevOps Pipeline.
The powershell code to invoke the shell script is as below
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VirtualMachineName -CommandId RunShellScript -ScriptPath "$($PSScriptRoot)/scripts/$PatchScript" -Parameter #{InputConf=$ReplInputConf}
I have done the following in the shell script and the folder name is written to a.txt
sudo -u $User find /data/config -maxdepth 1 -type d | sed 's/.*\///' >> /log/a.txt
sudo -u $User sed -i "/^\s*$/d" /log/a.txt
How can I return the string in a.txt back to the pipeline?
Thanks
You could try use a script to get the content of the txt file and set a variable $var for the content. Then use Logging commands to set another variable in the variable service of taskcontext. This variable is exposed to the following tasks as an environment variable.
echo "##vso[task.setvariable variable=testvar;]$var"
Related
I need to list all resources in all RGRPs on all subscriptions.
all what is there basically.
I try to do it with regex but does not work.
Get all resources on all subscriptions:
#! /bin/bash
for sub in $(az account list --query [].name -o tsv); do
az resource list -o tsv --subscription $sub 2>/dev/null
done
Check if your resource exists and print subscription of it
#! /bin/bash
for sub in $(az account list --query [].name -o tsv); do
az resource list -o tsv --subscription $sub 2>/dev/null --query [].name -o tsv 2>/dev/null | grep -i $1 && echo "SUBSCRIPTION: $sub" && exit
done
Let me know if there is simpler way.
Cheers
You can use a resource graph query (kusto/kcl) for that
Resources
| project name, type, location
| order by name asc
See also here:
https://learn.microsoft.com/en-us/azure/governance/resource-graph/samples/starter?tabs=azure-cli#list-resources
PowerShell:
Search-AzGraph -Query "Resources | project name, type, location | order by name asc"
I was wondering how I can set the system path variables in the GitHub actions workflow.
export "$PATH:$ANYTHING/SOMETHING:$AA/BB/bin"
You can use the following run command to set a system path variable in your actions workflow.
Syntax:
echo "{path}" >> $GITHUB_PATH
- run: |
echo "$AA/BB/bin" >> $GITHUB_PATH
Additionally, if you have downloaded some binaries and trying to set its path, GitHub uses a special directory called $GITHUB_WORKSPACE as your current directory. You may need to specify this variable in your path in that case.
- run: |
echo "$GITHUB_WORKSPACE/BB/bin" >> $GITHUB_PATH
If you are using Bash shell
- name: Add to PATH
shell: bash
run: |
echo "Folder PATH" >> $GITHUB_PATH
For Powershell as a shell:
- name: Add to PATH
shell: pwsh
run: |
echo "Folder PATH" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
I have a YAML anchor that deploys logic-apps. I want the pipeline to look for logic apps in subdirectory and loop through each one and deploy it. Here's my deploy-logicapp anchor
- step: &deploy-logicapp
name: Deploy logic app
script:
- source environment.sh
- pipe: microsoft/azure-arm-deploy:1.0.2
variables:
AZURE_APP_ID: $AZURE_CLIENT_ID
AZURE_PASSWORD: $AZURE_SECRET
AZURE_TENANT_ID: $AZURE_TENANT
AZURE_LOCATION: $AZURE_LOCATION
AZURE_RESOURCE_GROUP: $AZURE_RESOURCE_GROUP
AZURE_DEPLOYMENT_TEMPLATE_FILE: 'logic-apps/$DIR/template.$DEPLOYMENT_SLOT.json'
so in my pipeline, I loop through all the subdirectories and this works, it echoes each $DIR
- step:
script:
- cd logic-apps
- for DIR in $(ls -l | grep '^d' | awk '{print $9}'); do echo $DIR ; done
What I want to do is inside this loop I want to call my YAML anchor with the $DIR environment variable. I have tried a number of ways. The problem is the for loop is inside bash and not YAML so I can not call it.
Any guidance will be much appreciated.
As it turns out, I need to do everything from the azure command line. Here's the bash script that loops through all directories and deploy them.
#!/bin/bash
az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_SECRET --tenant $AZURE_TENANT
for DIR in $(ls -l | grep '^d' | awk '{print $9}'); do
az deployment group create --resource-group $AZURE_RESOURCE_GROUP --template-file $DIR/template.$DEPLOYMENT_SLOT.json --name $DIR
done
Next is to only deploy those that have changed :)
I need to store the output of an AZ cli commands that fetches my private IPs as a variable.
I am using the following in a bash script:
echo "Fetching Monitoring Server IP"
SERVER_IP=$(az vm show -n ${THIS_VM_NAME} -g ${RSC_GRP_NAME} --query privateIps -o tsv)
echo "$SERVER_IP
It would appear that this isnt working as when I echo the variable, it comes back empty.
+ THIS_VM_NAME=XXXX-XX-XX-XX-XX
+ echo 'Fetching Monitoring Server IP'
Fetching Monitoring Server IP
++ az vm show -n XXXX-XX-XX-XX-XX3 -g XXXX-XX-XX-XX-XX --query privateIps -o tsv
+ SERVER_IP=
+ echo ''
I will appreciate any pointers on this
Edit
The command you post lost a parameter to get the private IPs, you can use the command with the parameter -d or --show-details like this:
az vm show -g resourceGrouName -n vmName -d
But this command just gets all the IPs including the secondary IP.
You can get all the VM primary IPs of each interface through a shell script like this:
count=0
while : ; do
nic=$(az vm nic list -g resourceGroupName --vm-name vmName --query [$count].id -o tsv)
if [[ $nic == '' ]]; then
break
fi
privateIps[$count]=$(az vm nic show -g resourceGroupName --vm-name vmName --nic $nic --query ipConfigurations[0].privateIpAddress -o tsv)
let count++
done
echo ${privateIps[*]}
A solution to my similar problem in Azure Cloud Shell was putting a dollar sign before your SERVER_IP variable if Powershell in chosen.
$SERVER_IP=$(az vm show --name vmname --resource-group rgname --show-details --query [publicIps] --output tsv)
I need to connect from PowerShell to a Linux machine, get some folders that match a name and afterwards delete them (if you're asking why, cleanup for a test environment).
To accomplish this, I'm using the SSH.NET library (details here) and my script looks like this so far:
New-SshSession -ComputerName UbuntuMachine -Username root -Password test1234
Invoke-SshCommand -InvokeOnAll -Command {\
cd /dev;\
shopt -s nullglob;\
backupsToDelete=(Nostalgia*);\
printf "%s\n" "${backupsToDelete[#]}";\
for i in "${backupsToDelete[#]}"\
do\
printf $i\
done}
Everything works fine until I reach the point where I need to loop through my backupsToDelete array. It seems that for some reason, PowerShell is treating the for loop as it's own statement and not a bash one, everything resulting in an error:
At C:\Users\Administrator\Desktop\CleanupLinux.ps1:7 char:6
+ for i in "${backupsToDelete[#]}"\
+ ~
Missing opening '(' after keyword 'for'.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
Is there any way to tell PowerShell to not execute these kind of statements as it's own? Or maybe a different approach?
$command = "#
{\
cd /dev;\
shopt -s nullglob;\
backupsToDelete=(Nostalgia*);\
printf "%s\n" "${backupsToDelete[#]}";\
for i in "${backupsToDelete[#]}"\
do\
printf $i\
done}
#"
New-SshSession -ComputerName UbuntuMachine -Username root -Password test1234
Invoke-SshCommand -InvokeOnAll -Command $command
try like so.