Re-using existing ansible group_vars with molecule - molecule

I am working on adding tests to all my existing Ansible roles using Molecule.
My existing Ansible playbooks utilize levels of variables like such:
file: playbook_dir/group_vars/all
contents: global variables, applicable to all everything (hosts, inventories etc.)
file: playbook_dir/inventories/<inventory>/group_vars/all
contents: variables applicable to all hosts in this inventory.
file: playbook_dir/inventories/<inventory>/group_vars/<group>
contents: variables applicable only to this group.
When setting up Molecule for my existing roles, I need access to at the very least the 'global variables' (first in my list), I managed to achieve this by adding links, like such:
provisioner:
name: ansible
inventory:
links:
group_vars: ../../../../group_vars # These are the 'global' group_vars as described above.
So far so good, right. However, there are several roles that require variables to be set which are not part of the 'global' group_vars; i.e. inventory specific ones.
I've found that:
The links only allow a single link to be made, so linking to multiple group_vars does not seem to be an option.
inventory.links.group_vars and inventory.group_vars are mutual exclusive.
My question: How can I re-use my existing 'global variables', but also either a) use existing inventory specific group_vars, or b) define inventory specific group_vars?

You can define inventory group_vars specific to you molecule test(s) directly in molecule.yml. This also works for host_vars
Ref: https://molecule.readthedocs.io/en/stable/configuration.html#id22 (search for 'inventory:' on the page).

Related

DBT dynamic config

I have a generic test and need it to be always saved under a particular name for the given table it is running on, e.g. on table report_revenue the generated generic test name will always be diff_check_report_revenue. Right now the default dbt naming behavior for generic tests is kinda messy (it sets the test name based on the test config, which is a great idea for most cases, but unfortunately not for my particular one).
According to the docs it looks like the [alias]https://docs.getdbt.com/reference/resource-configs/alias is exactly what I need. However, I also need to set the name dynamically based on the table that is tested. I know it can be set in the yml config by setting the field alias, but I hope there might be a more elegant solution.
When I try the following:
{{
config({
"severity": 'warn',
"tags": ["diff_check"],
"alias": 'diff_check_' + {{ model | replace("XXXXXXX") | trim }}
})
}}
It just doesn't work and dbt completely ignores the alias property. model is the relation on which the test is running. It's probably just my own wrong syntax, but I'm stuck and humbly asking for advice. Thanks a lot in advance!
The docs are super confusing on test config, since they group together generic tests and singular tests, and the behavior is different.
You can use a config() block inside the definition for a generic test to configure it, and some keys (e.g., severity) work fine, but alias is not one of them.
I think alias is meant for singular tests only. To give generic tests a unique identifier (only possible since v1.1), you are supposed to use the name property (not config). Docs. Does this make sense? No. Does it make it easy for you to do what you want to do? Also no.
I'll point out that the default naming convention for a generic test includes the name of the test followed by the name of the model, but assuming that isn't good enough, your only option will be to add a name property to every test, where you define the test in the properties (fka schema.yml) file. And it looks like the name property doesn't jinja-template its value (so you can't use jinja to populate the test name). So you'll have to do a lot of this:
models:
- name: my_model
tests:
- diff_check:
name: diff_check_my_model
You could fork dbt-core. The relevant code is here.

Define variables per folder in dbt

I'm trying to have a structure to run a dbt project, where I have multiple entities (bank, names, cars). I'm going to have exactly the same code for them all.
Based on that, I'm trying to have several folder with the same code, where I can define inside the dbt_project.yamlfile. The idea is something like this:
vars:
db_name: 'db_official'
staging:
bank: 'variable_bank'
car: 'variable_car'
name: 'variable_name'
The variable "db_name" works. So, my the two problems I'm having are:
How to have this structure inside the the yaml file?
How to reference this structure inside each file?
(extra) Any other ideas how to handle this?
Thanks!
vars are basically globals. They can be scoped to your whole project, or to a package within your project, but not more specifically than that (they share a flat namespace). See the docs.
I would pull out the common code into a macro, then call that macro from each model file, passing in the unique values as string literals in the model file:
-- models/staging/bank.sql
{{ my_model_template('variable_bank') }}
-- models/staging/car.sql
{{ my_model_template('variable_car') }}

Specify namespace/class in VS2019 Test Explorer while using CTest/CMake

I'm using CMake + VS2019. In my test definition I have something like:
add_test(NAME "common/base64" COMMAND my_unit_test "common/base64")
VS2019 displays this like:
In project I have hundreds of tests and it is very inconvenient to search through unclear randomly generated names, expanding each items. So my questions:
How can I specify recognizable test name (instead of EBF.Tests.52488745200006951440) ?
How to specify namespace / class?
Also I can see Vs2019 Test Explorer can group by "Traits" - may be I can specify it over this feature?
It seems that the "EBF.Tests" prefix is the "project name".
You can disable the test prefix or set a new one by altering the TEST_PREFIX setting in your test autodiscovery method or the test naming itself.
For CTest, by altering the test naming:
# use [namespace].[class].[testname] naming
add_test("lib.namespace.Tests.SomeTest" test_exe)
For Catch2, by altering the test name prefixing by changing TEST_PREFIX in the auto-discovery method:
catch_discover_tests(
TagTestsTarget
TEST_SPEC "[tag]" # select tests by tag name
TEST_PREFIX "lib.namespace.Tests." # last dot required !
)
Fast Solution:
Set the Group By option in the Test Explorer to visualize by Class.
Then name each test in your CMake files as: mytestname.mytestname
As an example: add_test(NAME mytest.mytest COMMAND mytest)
(Not a perfect solution but it works well enough.)
More details:
When naming tests for the Team Explorer, if there is one dot between the name, will be interpreted as class name then function name. If there are two dots it will be interpreted as namespace, class, function. Any more dots are appended in the function string.
You could also display on the Team Explorer by namespace and then use these naming features to group your tests in at most two levels of hierarchy.
As example you could name all the tests related to a given feature with the same class or namespace and they will display nicely in the same set in the Team Explorer window.

Salt and managing .ssh/authorized_keys

Salt has a state module to manage .ssh/authorized_keys
https://docs.saltstack.com/en/develop/ref/states/all/salt.states.ssh_auth.html
I am not happy with it, since it combines code and data.
The state file is for me some kind of source code.
The ssh-key is for me data.
I don't want to combine both in one file.
Is there an other solution which separates code and data?
you don't have to put them together in one file:
as per documentation: https://docs.saltstack.com/en/latest/ref/states/all/salt.states.ssh_auth.html
you can use this method:
thatch:
ssh_auth.present:
- user: root
- source: salt://ssh_keys/thatch.id_rsa.pub
- config: /%h/.ssh/authorized_keys
(contrary to the example in the documentation, i get an error if the config: value starts with a '%')
this keeps your keys in their appropriate files and only links them from your code by their filenames.
Please have a look at the OpenSSH Formula. openssh/auth.sls contains the code for a state that pulls all data from a pillar. In the root folder of the formula you find pillar.example that shows how to structure the data for a pillar.
Maybe this formula is a starting point for you.

What is the best practice of storing single use user settings in rails 3?

I am struggling to find a pattern for storing single use user settings (VAT percentage, tag line. Things that are 1 off by nature) in Rails 3. I need to set up global site settings, which have single instances.
Ideally, I want the answer to be a design pattern, rather than a gem or plugin (unless someone knows a gem or plugin that integrates will with Active Admin)
What do you mean by single use settings? Do you mean things like API keys and environment variables?
If so, then a good practice is to use the ENV hash, and set up ENV variables in the environments file (explained below).
Create a .rb file for each individual gem (or arbitrary entity) that needs settings in your config/initializers/ directory. For example, when using stripe, I created config/initializers/stripe.rb shown below:
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
:secret_key => ENV['STRIPE_SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
This sets up initial settings within my stripe gem, and pulls the variable values from the ENV hash.
To set variables in the ENV hash, you can do so within the config/environments directory. In that directory, you will have three different files: config/environments/test.rb, config/environments/development.rb, config/environments/production.rb. Setting variables in the ENV hash(as shown below).
AppName::Application.configure do
# Set Stripe API Key
ENV['STRIPE_SECRET_KEY'] = "sk_test_key"
ENV['STRIPE_PUBLISHABLE_KEY'] = "pk_test_key"
...
end
How about a class for storing your key-value pairs?
class Settings < ActiveRecord::Base
attr_accessible :lookup, :value
def self.VAT
return self.find_by_lookup('VAT')
end
end
Then you can do #vat = Settings.VAT.value or similar. The lookup is your internally defined key. Of course the value column will have to be all the same datatype, but you can handle any necessary transformation in the getter methods (or through subclasses).