Can I add more than one variable to an existing variable group in Azure DevOps? - bash

I have a starter pipeline with one task which is using the Azure CLI: az pipelines variable-group variable create. My whole script looks like this:
steps:
- bash: |
az pipelines variable-group variable create \
--group-id 113 \
--name envName \
--value ${{parameters.envName}} \
--org $(System.CollectionUri) \
--project $(System.TeamProject)
displayName: 'Add variables to group'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
After running this command, the variable now exists in my variable group. However, I was wondering if there was a way to add more than one variable at a time? For example:
steps:
- bash: |
az pipelines variable-group variable create \
--group-id 113 \
--name location \ # First variable
--value ${{parameters.location}} \
--name envName \ # Second variable
--value ${{parameters.envName}} \
--org $(System.CollectionUri) \
--project $(System.TeamProject)
displayName: 'Add variables to group'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
When I run this script with the additional variable, the first variable is overwritten by the next variable. I'm looking into creating a loop that will iterate over all of my parameters to then be passed into my script. Something on the lines of:
steps:
${{each parameter in parameters}}:
- bash: |
az pipelines variable-group variable create \
--group-id 113 \
--name ${{parameter.Key}} \
--value ${{parameters.Value}} \
--org $(System.CollectionUri) \
--project $(System.TeamProject)
displayName: 'Add variables to group'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
This last script gives me A mapping was not expected.
Is there a way to use the script to add more than one variable to an already existing variable group?

Your each is fine, I just fixed small syntax and it works:
parameters:
- name: test1
displayName: test1
type: string
default: "Test-1"
- name: test2
displayName: test2
type: string
default: "Test-2"
jobs:
- job: VG
steps:
- ${{ each parameter in parameters }}:
- bash: |
az pipelines variable-group variable create \
--group-id 113 \
--name ${{parameter.Key}} \
--value ${{parameter.Value}} \
--org $(System.CollectionUri) \
--project $(System.TeamProject)
displayName: 'Add variable ${{parameter.Key}} to group'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
Results:

Related

Bash Script not runnuning properly in openstack server creation

I have an openstack server in which i want to create an instance with user data file for example
openstack server create --flavor 2 --image 34bf1632-86ed-46ca-909e-c6ace830f91f --nic net-id=d444145e-3ccb-4685-88ee --security-group default --key-name Adeel --user-data ./adeel/script.sh m3
script.sh contain
#cloud-config
password: mypasswd
chpasswd: { expire: False }
ssh_pwauth: True
#!/bin/sh
wget https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-7.17.7-linux-x86_64.tar.gz && tar -xzf elastic-agent-7.17.7-linux-x86_64.tar.gz cd
elastic-agent-7.17.7-linux-x86_64 sudo ./elastic-agent install \
--fleet-server-es=http://localhost:9200 \
--fleet-server-service-token=AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuLTE2Njc0MDM1 \
--fleet-server-policy=499b5aa7-d214-5b5d \
--fleet-server-insecure-http
when i add this script nothing executed. i want run above script when my instance boot first time.

azure devops, bash : 'az pipelines variable-group variable update'

az pipelines variable-group variable update \
--group-id X \
--name ${{parameter.Key}} \
--value ${{parameter.Value}} \
--org $(System.CollectionUri) \
--project $(System.TeamProject)
Is there an option to reach variable-group by name and not by group-id?
have option to reach variable group by name and not by group-id?
The answer is no.
According to the document az pipelines variable-group variable update:
az pipelines variable-group variable update --group-id
--name
[--detect {false, true}]
[--new-name]
[--org]
[--project]
[--prompt-value {false, true}]
[--secret {false, true}]
[--value]
Required Parameters
--group-id --id
Id of the variable group.
--name
Name of the variable.
The group-id is the Required Parameter.

Setting Docker environment variables in Azure DevOps using bash task

I am having a problem setting up an environment variable as part of Releasing my container. in Azure DevOps, I have a bash task in which I am trying to set an environment variable (CUSER)
export CUSER=$(CUSER) && \
echo $(MYPASS) | sudo -S docker run \
-e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &
The container runs but the environment variable is not set. But it is set when I execute the script directly on the host like export CUSER="Dev9" && docker run -e CUSER --name demo1 -p 80:80 myimage:256
I suspect there is a problem with the way my command is formatted but I am not sure where or what.
Resolved it by changing how I authenticate sudo.
sudo -S <<< $(MYPASS) echo export CUSER=$(CUSER) && \
docker run -e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &

How to create EC2 instance by docker-machine with exists key pair?

Use this way to create an instance on aws:
docker-machine create \
-d amazonec2 \
--amazonec2-region ap-northeast-1 \
--amazonec2-zone a \
--amazonec2-ami ami-XXXXXX \
--amazonec2-keypair-name my_key_pair \
--amazonec2-ssh-keypath ~/.ssh/id_rsa \
my_instance
Can't connect to it by ssh.
The my_key_pare is a name that exist on aws. The ~/.ssh/id_rsa is local ssh private key. How to set the right value?
I have read the document but didn't find an example of using both --amazonec2-keypair-name and --amazonec2-ssh-keypath.
Download the file from "Key Pairs" in AWS Console and place it in ~/.ssh.
Then run
docker-machine create \
-d amazonec2 \
--amazonec2-region ap-northeast-1 \
--amazonec2-zone a \
--amazonec2-ami ami-XXXXXX \
--amazonec2-ssh-keypath ~/.ssh/keypairfile \
my_instance
In gitlab-runner using Docker+machine, you have to provide both "amazonec2-keypair-name=XXX",
"amazonec2-ssh-keypath=XXX".
the Keypath should be like /home/gitlab-runner/.ssh/id_rsa and the path should also have id_rsa.pub file with id_rsa. These two file should not be your local generated key, should be the id_rsa, id_rsa.pub of Pem created
The following commands will do the trick,
cat faregate-test.pem > /home/gitlab-runner/.ssh/id_rsa
ssh-keygen -y -f faregate-test.pem > /home/gitlab-runner/.ssh/id_rsa.pub
And This will allow you to connect from runner manager instance to the runner provisioned by using your AWS existing keypair...

How does "knife ec2 server create"'s expansion of --run-list work?

I'm unable to bootstrap my server because "knife ec2 server create" keeps expanding my runlist to "roles".
knife ec2 server create \
-V \
--run-list 'role[pgs]' \
--environment $1 \
--image $AMI \
--region $REGION \
--flavor $PGS_INSTANCE_TYPE \
--identity-file $SSH_KEY \
--security-group-ids $PGS_SECURITY_GROUP \
--subnet $PRIVATE_SUBNET \
--ssh-user ubuntu \
--server-connect-attribute private_ip_address \
--availability-zone $AZ \
--node-name pgs \
--tags VPC=$VPC
Consistently fails because 'roles[pgs]' is expanded to 'roles'. Why is this? Is there some escaping or alternative method I can use?
I'm currently working around this by bootstrapping with an empty run-list and then overriding the runlist by running chef-client once the node is registered.
This is a feature of bash. [] is a wildcard expander. You should can escape the brackets using "\".

Resources