Edit json file using ansible - ansible

I am trying to edit a particular JSON file using Ansible. The file is present in remote servers which have to be logged in using LDAP authentication.
The file path is /etc/sensu/conf.d/client.json
and the line that I want to edit is as follows:
"subscriptions": ["Nginx", "Primus", "B2B", "Docker_Process_Check", "EBS", "base"],
I want to add one more value ("filebeat") in that line which after that should look like
"subscriptions": ["Nginx", "Primus", "B2B", "Docker_Process_Check", "EBS", "base","filebeat"],

AFAIK there's still no builtin module for JSON manipulation.
Either use 3rd-party module (like ghetto-json),
or make a complex regular expression with replace module (i.e. search for "subscriptions": [<...>] and place ,"filebeat" just before closing ]. You can use regex101.com to craft and test required expression.
Please also inspect this answer for additional info.

Related

How do nested variables within the .env file work in CodeIgniter 4

Under the "Nesting Variables" section in Codeigniter4 site:
"To save on typing, you can reuse variables that you’ve already specified in the file by wrapping the variable name within ${...}"
link to CI nesting Variables section
example in the documentation:
BASE_DIR="/var/webroot/project-root"
CACHE_DIR="${BASE_DIR}/cache"
TMP_DIR="${BASE_DIR}/tmp"
I was trying to use the following
app.baseURL = 'http://localhost:8080/'
google.redirect = ${app.baseURL}Google
However, it's assigning it as a literal when print_r($_ENV)
[google.redirect] => ${app.baseURL}Google
I've tried using non-namespaced keys including BASE_DIR (per the example) and it keeps printing as a literal.
What's strange - When I use the following:
CI_ENVIRONMENT = development
google.redirect = ${CI_ENVIRONMENT}Google
The result when print_r is:
[CI_ENVIRONMENT] => development
[google.redirect] => developmentGoogle
My question is - What am I doing incorrectly and/or how should these be set/used correctly?
According to the documentation, I should be able to use any key within the .env file that was already assigned using
${somekeyinthisfile}
After a bit of looking, there is a more recent file up at
https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Config/DotEnv.php
with all the "other" changes...
This was a Bug Fix. So get that file and you will be good to go.
I am pretty sure that the intention wasn't to allow app.xxx settings to be used as variables as the documentation clearly shows, by not
showing them being used. ( yes its 6am now ...)
BUT it is your code to do with as you please...So if you want to use app.xxx as variables...
The Only Thing missing is the DOT (.) in the regex
If you look on Line 272 - system/Config/DotEnv.php inside method resolveNestedVariables() and add a . (dot) into the regex, that will make all your app.things work.
$value = preg_replace_callback(
'/\${([a-zA-Z0-9_.]+)}/',
function ($matchedPatterns) use ($loader) {
I have added a dot (.) at the end of the [a-zA-Z0-9_
So
'/\${([a-zA-Z0-9_]+)}/',
becomes
'/\${([a-zA-Z0-9_.]+)}/',

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

Replacing variable value in ruby while setting the value using "set" command

I have a .properties files as below:
user:abcd
pwd:xyz
system:test
Next, I have a ruby script with Watir for browser automation. In this script, I have statements like
browser.text_field(:id => 'identifierId').set "#{user}:variable to be replaced by its value from .properties file".
Similarly, other values need to be replaced for "pwd" and "system".
I tried the solution per below posts:
Replace properties in one file from those in another in Ruby
However, "set" command is setting whatever has been paased as arguments to it instead of replacing the variable with its value.
Please help.
You have to read the information out of the file.
Most Watir users leverage yaml files for this.
config/properties.yml:
user: abcd
pwd: xyz
system: test
Then read the yaml file & parse your data:
properties = YAML.safe_load(IO.read('config/properties.yml'))
text_field = browser.text_field(id: 'identifierId')
text_field.set properties['user']
Alternately you can take a look at Cheezy's Fig Newton gem, which is designed to work with his Page Object gem

Properties in JMeter for GUI and CMD

I have a JMeter script, where I have some user defined Variables like FILE_SAVE_PATH. This script should be started on a command line with parameter -J. So in the GUI, I changed the value for the variable FILE_SAVE_PATH to ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}, because the test should save there a file, but only on my machine. On the machine, where the script will be started from command line, it should save the file into another path.
My problem is now this: When I test this JMeter script on my machine in the GUI, I get an output of this:
About to replace in property of type: class org.apache.jmeter.testelement.property.StringProperty: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
2017/04/04 17:09:38 DEBUG - jmeter.testelement.property.AbstractProperty: Not running version, return raw function string
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
But I think, the last line should be something like this:
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: "C:\svn\trunk\dir"
So, how to change the test to get the result I want?
Escape every backslash with another one - C:\\svn\\trunk\\dir, or use unix slash, JVM's gonna handle it right: C:/svn/trunk/dir
And remove the doublequotes, they're not needed.
P.S. I presumed you're not using that notation in the Beanshell/JSR223 context. If you do - stop there and use the legit way to access properties.

chef recipe FileEdit insert_line_after_match and insert_line_if_no_match

I want to edit file using chef cookbook recipe.
The file appears now as,
[attribute1]
foo=bar
[attribute2]
....
I want to change it like:
[attribute1]
foo=bar
newfoo=newbar
[attribute2]
....
So basically, I want to add a line if it does not exist in the file and I want to add it after a particular line in that file.
I found 2 options under Class: Chef::Util::FileEdit which could be useful here insert_line_after_match and insert_line_if_no_match. But I want an option which can perform both of the actions. If I use insert_line_after_match, it works for first run but for next run it just keep adding lines even if line is already in the file. And insert_line_if_no_match adds line at the end of file if line does not exist in file but I want to add line after particular line in that file.
I am bit new to chef recipes. Is there any solution to solve above problem?
I would suggest not editing files, but rather overwriting them. You should create a template or a file inside the cookbook and then using template or cookbook_file resource overwrite the file on the machine with the one from cookbook.
Your config file looks similar to toml, so you can also use toml-rb gem to generate this file from json (data bag) or attributes like that:
chef_gem 'toml-rb' do
compile_time false
end
file '/path/to/file.conf' do
content( lazy do
require 'toml'
"# This file is managed by Chef\n" +
TOML.dump( my_json )
end )
end
Pretty please don't use FileEdit. It is an internal API and not intended for public use. What you want is the line cookbook, specifically the replace_or_add custom resource. Make sure you craft your regexp very carefully.
In general we do not recommend this kind of management style as it is very brittle and easily broken by unrelated changed. A better option is to use a template resource or similar to manage the whole file in a convergent manner.

Resources