Retrieve array data from yaml file Ruby - 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.

Related

Reference a nested list in yaml in another yaml file

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".

How do I get the file metadata from an AWS S3 file with Ruby?

I'm trying to simply retrieve the meta data from a file uploaded to S3. Specifically I need to the content type.
I know the file has metadata, because I can see it in S3 console. But I'm unable to get it programmatically. I must have some syntax error.
See the code below, the file.key returns the file name correctly. But the file.metadata doesn't seem to return an array with data.
s3 = Aws::S3::Resource.new(region: ENV['REGION'])
file = s3.bucket(sourceS3Bucket).object(sourceS3Key)
puts file.key # this works!
puts file.metadata # this returns an empty array {}
puts file.metadata['content-type'] # empty
As Aleksei Matiushkin suggested file.data[:content_type] will give the file type.

Fetch variable from yaml in puppet manifest

I'm doing one project for puppet, however currently stuck in one logic.
Thus, want to know can we fetch variable from .yaml, .json or plain text file in puppet manifest file.
For example,
My puppet manifest want to create user but the variable exist in the .yaml or any configuration file, hence need to fetch the varibale from the outside file. The puppet manifest also can do looping if it exist multiple users in .yaml file.
I read about hiera but let say we are not using hiera is there any possible way.
There are a number of ways you can do this using a combination of built-in and stdlib functions, at least for YAML and JSON.
Using the built-in file function and the parseyaml or parsejson stdlib functions:
Create a file at mymodule/files/myfile.yaml:
▶ cat files/myfile.yaml
---
foo: bar
Then in your manifests read it into a string and parse it:
$myhash = parseyaml(file('mymodule/myfile.yaml'))
notice($myhash)
That will output:
Notice: Scope(Class[mymodule]): {foo => bar}
Or, using the loadyaml or loadjson stdlib functions:
$myhash = loadyaml('/etc/puppet/data/myfile.yaml')
notice($myhash)
The problem with that approach is that you need to know the path to file on the Puppet master. Or, you could use a Puppet 6 deferred function and read the data from a file on the agent node.
(Whether or not you should do this is another matter entirely - hint: answer is you almost certainly should be using Hiera - but that isn't the question you asked.)

ruby/hash:Chef::Node::ImmutableMash included in file when converting Chef attributes to yaml

I am trying to write a .yml file from a subset of attributes. Here's the resource definition I'm using:
file '/home/user/file.yml' do
owner 'user'
group 'user'
mode '0755'
content node['default']['properties'].to_yaml
end
When I run this, the file.yml ends up looking like this though:
--- !ruby/hash:Chef::Node::ImmutableMash
config: !ruby/hash:Chef::Node::ImmutableMash
example: value
another: value
How do I get clean yaml output without all the !ruby/hash:Chef::Node::ImmutableMash output?
Turns out all you need to do is convert the attributes to a hash explicitly before converting to yaml. Here is the working code:
file '/home/user/file.yml' do
owner 'user'
group 'user'
mode '0755'
content node['default']['properties'].to_hash.to_yaml
end
Note: you'll need to be using chef-client 11.10.0 or later, because
there used to be a bug with the to_hash method. More info here:
https://stackoverflow.com/a/14782389/1688034

JSON file parse error from delete_user method

I have a program that saves to a JSON file, this is my first attempt with a JSON file so I apologize if this is a very simple question.
I have a method inside of my program that parses the JSON file and deletes the information that is given by the user, called delete_user. While parsing the file from the delete_user method I get the error:
C:/Ruby22/lib/ruby/2.2.0/json/common.rb:155:in `parse': 776: unexpected token at
'users.json' (JSON::ParserError)
from C:/Ruby22/lib/ruby/2.2.0/json/common.rb:155:in `parse'
from usertool.rb:35:in `delete_user'
from usertool.rb:15:in `menu'
from usertool.rb:66:in `<main>'
I understand that the error is telling me my JSON file isn't parsing, but what I don't understand is why..
The delete_user method that the error is coming from:
def delete_user
hash = JSON.parse('users.json')
delete_data = prompt("Enter username:")
delete_data[:username] = "#{username}"
delete_data.delete[:email_address, :member_status]
new_json = delete_data.to_json
delete_user if restart
end
Is there a issue with how I'm parsing the file, am I doing it wrong? Or is there another issue that's going way over my head?
And here's an example of the JSON file:
{"username":"TEST1","email_address":"TEST1","member_status":"TEST1"}
TR;DL:
This will work:
JSON.parse(File.read('file-name-to-be-read.json'))
Why?
json.parse [string] parses plain JSON, but doesn't load a file. Instead, pass it a file object, and it will read the JSON inside of it.
JSON Is A String
JSON is a way of encoding objects to a string - it is based on how javascript objects are defined (JavaScript-Option-Notation). It's a string - not a file saver, which means you can use it in more applications, like API's, along with saving data to files.

Resources