Reference a nested list in yaml in another yaml file - yaml

I have a yaml document that looks like this:
outterkey:
innerkey:
- name: someName
fave: ["Apple"]
- name: someOtherName
fave: ["Mango","Orange"]
How can I reference this in another yaml file like this:
key: ${outterkey.innerkey}
Doing this throw the error where freemarker expected a string but got a sequence.
I did some searching but only found solutions for simple lists using "join".

Related

YAML error : end of the stream or a document separator is expected

I'm getting the following error in a yaml file
Error : end of the stream or a document separator is expected at line 2, column 11:
apiVersion: v1
^
Line : undefined undefined
This is the entire contents of the file
<<EOT
apiVersion: v1
clusters:
- cluster:
server: https://3F46DDD9022BD149B88DA7ED4AFB2B30.gr7.eu-west-1.eks.amazonaws.com
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjR> name: kubernetes
contexts:
- context:
cluster: kubernetes
user: aws
name: aws
current-context: aws
kind: Config
preferences: {}
users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
args:
- "token"
- "-i"
- "terraform-eks-demo"
EOT
I've been googling this for hours but I'm getting nowhere tried . I've tried every YAML linter I can find, can anybody point me in the right direction? I am new to this, if that wasn't obvious
You need to remove <<EOT and EOT. You probably copied them from some code that injected YAML into a command line utility via a heredoc but they should not part of the YAML content.
What happens is that when the YAML processor sees <<EOT (which parses as part of a YAML scalar because < is not a special character) and then a new line. A multiline scalar at root level must be the single root node of a document. When the second line is read, the YAML processor sees a : which is not allowed here. At this point, both lines have been read as multiline scalar, and a multiline scalar cannot be used as mapping key. Therefore, the processor complains that after the single root node of the YAML document, the document must end, but instead you try to start a mapping.
I think you should keep a space before 'v1' at line2.

Get the keyname from a YAML configuration file

I would like to get the name of a key in a YAML configuration file like the following
The YAML file contains a collection of structs in structs, the organisations have a number represented as a symbol which is the key/class name. I need to retrieve this symbol
require 'yaml'
data = YAML.load(DATA)
data.organisations.each do |organisation|
organisation #<struct language="nl", name="myname">
# following line is what I need, I expect it to be :"121"
organisation.class #<Class:0x00000004fd4248>
end
__END__
--- !ruby/struct
organisations: !ruby/struct
:121: !ruby/struct
language: nl
name: organisationname
Can someone help me retrieving the name of the struct ?
I tried around a bit and found this:
data.organisations.members
=> [:"121"]
The way I found this (which is useful in other circumstances, too) is the following:
data.organisations.methods - Object.new.methods
And then I tried out every method that seemed reasonable.

google deployment manager access nested properties with --properties argument

If Im using a jinja template I can override properties via command line like this:
--properties zone:us-central1-a,machineType:n1-standard-1,image:debian-9
But I see no documentation or examples for doing this with nested properties like labels or environmentVariables for example:
resources:
- name: resource-name
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions'
properties:
labels:
testlabel1: testlabel1value
testlabel2: testlabel2value
environmentVariables:
TEST: 'zzzzzzzzz'
How do set properties like these? this does not work: --properties labels:testlabel1:newvalue
The short answer here is that the --properties flag is not meant to pass property values to the template. A template cannot run without a configuration file, the --properties flag is meant to replace the config file. Each parameter you pass is the same as listing them in a config file.
Essentially using --template my-template.py --properties zone:us-central1-f is the equivalent of running --config myConfig.yaml where the YAML is defined as such:
imports:
- path: my-template.py
resources:
- name: some-resource
type: my-temaplte.py
properties:
zone: us-central1-f
The --properties flag is not meant to pass raw data to replace non-variables.
Although this does not directly answer your question, you shouldn't normally need to define nested values in the flag. Your template will generally call on direct variables taken from the object properties.
despite this, I did try some tests, and as far as I can tell, you can't do this.
After some trial and error I managed to pass an object via command line like this:
--properties ^~^labels:{'testlabel1: testlabel1value','testlabel2: testlabel2value'}~environmentVariables:{'TEST: zzzzzzzzz'}
This sequence of symbols ^~^ is how you can change delimiter. You have to put it at the beginning of your properties. More info about escaping you can find here.
I put single apostrophe over single key value pair because we need that space between key and value. Otherwise it's interpereted as key with null value.
If you're using Bash shell you should also escape {,} symbols.

mcollective inventory script

I created mCollective inventory script as below,
def formatting(users_ids)
YAML.load(File.open(users_ids))
end
inventory do
format "%s\t%s\t"
fields { [facts["hostname"], formatting(facts["users_ids"]) ] }
end
Here users_ids facter is in yaml format on the server. So when I do inventory for this facter I need to parse that yaml format to hash. But when I run with this script getting below error,
[root#mco-server]#
The inventory application failed to run, use -v for full error backtrace details: (eval):2:in `initialize': No such file or directory - ---
root: 0
test1: 503
testuser: 2033
[root#mco-server]#
Not sure if am missing something to get the output parsed. The strange thing is its not printing hostname also.
The facter output is below on the server from facts.yaml
users_ids: |-
---
root: 0
test1: 503
testuser: 2033
Any help would be much appreciated.
According to the error message, the argument you are passing to users_ids is not a valid filename.
def formatting(users_ids)
YAML.load(File.open(users_ids))
end
Somehow your code is passing --- as an argument to that method. This is likely due to the combination of your API calls to parse and load the yaml and the yaml file itself. Consider changing the API call to a cleaner:
def formatting(users_ids)
YAML.load_file(users_ids)
end
and I think you really want a hash in your yaml and not an array of key-value pairs with an element of ---, so your yaml should really be:
users_ids:
root: 0
test1: 503
testuser: 2033
which would also remove the --- which typically indicates the beginning of a yaml, and also seems to be what your code is erroring on in the way you are trying to load the yaml.

Retrieve array data from yaml file Ruby

I have the following array in a yaml file:
error_messages:
- 'Error! There is no registration number'
- 'Error! Event is not added'
I am reading data from this file as following:
$common_test_data['error_messages[0]']
I've initialized my yaml file in global var:
$common_test_data = YAML.load_file("#{Dir.pwd}/test_data/common_data.yml")
But data unfortunately is not retrieved from yaml array. Maybe there is specific way to retrieve data from yaml file?
You should retrieve it in the following way:
$common_test_data['error_messages'][0]
$common_test_data is a Hash.
$common_test_data['error_messages'] is an Array.

Resources