Change variables in user data - amazon-web-services

Here is a cloudformation template that works as expected.
https://github.com/shantanuo/cloudformation/blob/master/updated/so2.tpl.txt
But when I change the last line to something like this...
/home/ec2-user/mysecret.txt`'' --valid-ips !Ref MyIpAddress >
It silently ignores the command. Is there any other way to substitute the MyIpAddress variable?

Instead of using 'Fn::Join' you can use 'Fn::Sub'. This will make your template more readable as won't have to break your script inot multiple lines and you can reference MyIpAddress as ${MyIpAddress}.

I do not know how and why does it work. But this is what I was looking for.
- >-
/usr/local/bin/aws-ec2-assign-elastic-ip --access-key ''`cat
/home/ec2-user/myaccesskey.txt`'' --secret-key ''`cat
/home/ec2-user/mysecret.txt`'' --valid-ips '
- !Ref MyIpAddress
- |
'
Thanks to Pat's comment!

Related

drop label from metrics prometheus

I have the metric below and I want to drop the label "exported_namespace="test" and I am using the prometheus relabel_config but I'm not sure if the config will work properly:
"kube_pod_status_ready{condition="false", env="test", exported_namespace="test", instance="10.69.19.17:8080", job="kube-state-metrics", namespace="test", pod="test-1-deploy", uid="1asdadasaas"}
prometheus scrape config
- source_labels = [exported_namesapce]
separator: ,
action: labeldrop
regex: (.*)
replacement: $1
You can do:
writeRelabelConfigs:
- regex: exported_namespace
action: labeldrop
OR
writeRelabelConfigs:
- action: labeldrop
regex: exported_namespace
Please note that you must use metric_relabel_configs instead of relabel_configs if you want apply relabeling on the collected metrics. See this article for details.
If you want to drop a label with a particular value from the collected metric, then use the following relabeling rule at metric_relabel_configs section of the needed scrape_config:
- source_labels: [exported_namespace]
regex: test
target_label: exported_namespace
replacement: ""
This relabeling rule substitutes the exported_namespace="test" label with exported_namespace="" label, which, in turn, is automatically removed by Prometheus, since it contains an empty label value. You can play with this relabeling rule at this page.
If you need just dropping the exported_namespace label with any value, then use the following relabeling rule:
- action: labeldrop
regex: exported_namespace
Note that this rule will drop any value for exported_namespace label. For example, both exported_namespace="test" and exported_namespace="foo" will be dropped.

Use RegEx comparison in Gitlab workflow rules

I'm trying to use a regex comparisons in the workflow:rules section of my gitlab-ci file, but it doesn't seem to be working. Here is a basic version:
stages:
- prep
variables:
VAR1: "no value"
APPURL: "no value"
workflow: #Goal: only run pipeline for push events set some variables based on branch/commit_ref_name
rules:
- if: "CI_PIPELINE_SOURCE == "push"
- if: "CI_COMMIT_REF_NAME =~ /^dev/
variables:
VAR1: "Dev Value"
APPURL: "https://devurl.com"
test_job:
stage: prep
image: runner.image/url
script:
- echo "$VAR1"
- echo "APPURL"
When push a change from a branch named something like "dev1-jirastory", the test job output says "no value" for both variables. So, its not catching the commit_ref_name rule for some reason.
Can someone tell me if you can use regex comparisons in the workflow:rules statements? All the stuff I've found so far refers to job rules. As I want these variables set for multiple jobs, I want to set them for the entire workflow and subsequent jobs, not use the same rules in every single job, which can grow and not be managable.
I did try accomplishing those value determinations in a root "before_script" section, but that gets overwritten if I need to have to do other actions in a before_script for any individual job, so that won't work for me either.
Lastly, if anyone can tell me if I can do any "command" statements for parsing the commit_ref_name, that would be great. I'd love to do something like:
"$CI_COMMIT_REF_NAME" | awk -F "-" '{print $1}')
to pull out the "dev1" portion of the ref name like my sample above for use in jobs as well.
Thanks in advance.
Using variables in workflow works, just a couple of small changes needed for your pipeline:
stages:
- prep
variables:
VAR1: "no value"
APPURL: "no value"
workflow: #Goal: only run pipeline for push events set some variables based on branch/commit_ref_name
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
- if: '$CI_COMMIT_REF_SLUG =~ /^dev/'
variables:
VAR1: "Dev Value"
APPURL: "https://devurl.com"
test_job:
stage: prep
script:
- echo "$VAR1"
- echo "$APPURL"
You where missing the $ before the variables in you rules clauses. And I would recommend to use $CI_COMMIT_REF_SLUG instead of CI_COMMIT_REF_NAME to be independent of the case.

How to pass in JSON type into yaml cloudformation template

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iot1click-placement.html#cfn-iot1click-placement-associateddevices
states that the property AssociatedDevices is a type JSON, but when I am writing the template in YAML
I have read a few responses here and have tried the following:
AssociatedDevices: "--arguments": '{"SecuityButtonTemplate": !Ref TestITPA.DeviceId}'
PlacementName: "TestITPAPlacement"
Attributes: "--arguments": '{"--Location": TestITPALoc}'
(this fails to build)
and this:
AssociatedDevices: '{"SecuityButtonTemplate": !Ref TestITPA.DeviceId}'
PlacementName: "TestITPAPlacement"
Attributes: '{"Location":"TestingLoc"}'
(this also fails to build)
I have even search github for YAML code referencing AssociatedDevices but not finding how people are actually doing this - can anyone help me shed some light ?
I lastly have tried this:
AssociatedDevices: !Sub |
{
SecuityButtonTemplate: !Ref TestITPA.DeviceId
}
PlacementName: "TestITPAPlacement"
Attributes: !Sub |
{
Location: "testingLoc"
}
(this throws what seems to be an IDE erorr - the middle variable of palcementName is not longer red like the others)
You can try the following:
AssociatedDevices: !Sub '{"SecuityButtonTemplate": "${TestITPA.DeviceId}"}'
PlacementName: "TestITPAPlacement"
Attributes: '{"Location":"TestingLoc"}'

Azure Pipeline dynamic parameters to template file from YAML pipeline

I am currently working with Azure Devops Build Pipelines, and am trying to call a template file to do some tasks from my build yaml.
I am facing some difficulties to pass parameters to the template file. Let assume that this is my template file (simplified) which works fine :
parameters:
iterations: []
steps:
- ${{ each i in parameters.iterations }}:
- task: PowerShell#2
displayName: "Get key values ${{i}}"
name: getKeyValues_${{i}}
inputs:
targetType: 'inline'
script: |
$item = "${{i}}"
Write-Host "item : $($item)"
$keyVal = $item -split "_"
Write-Host $keyVal
Write-Host "key: $($keyVal[0]) | value: $($keyVal[1])"
echo "##vso[task.setvariable variable=key;isOutput=true]$($keyVal[0])"
echo "##vso[task.setvariable variable=value;isOutput=true]$($keyVal[1])"
So I want my iterations parameter contain something like this :
iterations: ["1_60", "2_40"]
Inside my Yaml pipeline I have the following code(also simplified) :
Not working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: $(calculateIterations.iterations)
Working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: ["1_60, "2_40"]
As you can see, the problem is that I am unable to use the output variable of my script to pass it as parameter to my template.
When I run the not working scenario I have the following error :
I have found this post, but no solutions yet...
As what 4c74356b41 said, this is the dilemma at present. In another word, the Not working scenario you mentioned does not support to achieve until now.
Now, we must let the template know the clear text at compile time. Because during this compile time, we have difficulty to do 2 or more things in one step at same time, especially contain compiling variable value, passing to corresponding template dynamic arguments and etc.
Expected a sequence or mapping. Actual value
'$(calculateIterations.iterations)'
More detailed, during compile time (after you click Run but before pipeline start truely):
1) Firstly we map the value that come from YAML pipeline, to make sure the - ${{ each i in parameters.iterations }} has clear value to start.
2) After it is done, then parse exact value on name: getKeyValues_${{i}} in script order.
In your scenario, it cannot even satisfy the first step since what you passed is a variable, and we do not has parse value process here. That's why you saw error said Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'.
Another expression of this error message is: we(template) are looking forward to exact value(s) to map our dynamic parameters, but what you give is a unrecognized content $(calculateIterations.iterations). Sorry, we cannot start to run.

How do I use 'include' to apply rules to a capture when writing a Sublime Text syntax definition?

I'm trying to write a syntax definition for Gradle in Sublime Text 3. Many pieces of a Gradle build file are really just Groovy and so I'm trying to take advantage of the current Groovy highlighting support by using include. Thus far this is working fairly well, by I'm stuck on how to apply it to a particular piece.
Here is the Gradle snippet I am trying to highlight:
task copyTask (group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares') {
from 'resources'
into 'target'
include('**/*.txt')
}
And this is the syntax I'm using to match that snippet:
- name: copy.task.source.gradle
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*)\)\s*{'
comment: 'Copy task definition'
beginCaptures:
'1': {name: keyword.task.source.gradle}
'2': {name: entity.name.function}
'3': {name: source.groovy}
end: '}'
contentName: copy.body.source.gradle
patterns:
- include: source.groovy
Most of this appears to work as intended. (Always hard to know with RegEx.) My problem is that the third capture. I want to apply all the rules contained in 'source.groovy' to the text between the parentheses and what I have above is not getting the job done.
To clarify: the text is "captured" and tagged as source.groovy, but that's not actually quite what I want. I don't want it simply tagged as source.groovy, I want the rules from source.groovy to be used when evaluating the text. The last line of my example successfully does this to the "content" section (text in between the braces) but simply putting include does not work.
'3': {include: source.groovy} # This gets an error.
If there is a syntax to apply include directly to a capture I can't find it, and I can't figure out another technique. Maybe something that has nested begin and end tags?
If I am understanding this correctly you would like the third capture group source.groovy to match the group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares' part of your example.
In that case you would just need to alter you expression to capture more of the string like so:
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*?)\)\s*{'