saving an ansible fact as a local variable - variables

I'm trying to create a local variable (local as inside a task) using a variable gathered from a fact. Basically the opposite of sect_fact....
something like:
- name: create a variable from a fact
create_var:
name: myprivatevar
value: {{ ansible_eth0.ipv4.address }}
in order to be able to use that var later, in relation to another host.
I know I can use magic variables an hostvars in order to access other hosts' facts but I dont know the names of the hosts I want to refer to when I play the book...

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') }}

Using variables in Serverless resource name

In my serverless.yml I have:
public-function:
handler: function.Handler
I want to parametrise the public-function line but I'm not sure how to do this. I know I can add a name: attribute to the function and use the ${} syntax documented at https://www.serverless.com/framework/docs/providers/aws/guide/variables/ but this only half solves my problem as public-function is used by Serverless as a basis for the names of other resources which I also want to have parametrised names.
Is there some way of using variables in the resource name?
The declared function key (i.e. public-function) is only a reference in the stack. There is no reason to change it at build time as the name is arbitrary.
If you want to customise the details of the deployed function, change its configuration.
For example, change the name of the function to something else using environment variables:
functions:
main:
handler: function.Handler
name: ${self:provider.stage}-${env:FUNCTION_NAME}
If you are exporting the resource to another stack, you can define the export name like so:
resources:
Outputs:
MainFunction:
Value:
Fn::GetAtt: [MainLambdaFunction, Arn]
Export:
Name: ${self:provider.stage}-${env:CUSTOM_EXPORT_NAME}
The stage scoping to both of these examples is recommended but not required; you can name your functions, resources, and outputs anything you want.

Re-using existing ansible group_vars with 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).

Using Environment variables in ElastAlert

I am trying to implement alerts on my data present in elasticsearch using ElastAlert. I would like to know if there is a way to use environment variables or properties file or by exporting the values for changing the fields present in rule types in ElastAlert instead of going and changing the values manually in the rule files to reduce the possibility of an error.
For example, my spike rule configuration looks like this:
name: Event spike
type: spike
index: alerting-logs-*
threshold_cur: 300
timeframe: minutes: 2
spike_height: 2
spike_type: "up"
query_key: HostName
filter:
- query:
query_string: {query: 'smcfsloglevel:ERROR'}
alert:
- "email"
email:
- "someuser#email.com"
Now if I want to change the value of threshold_cur from 300 to, say, 500, can I somehow do it without going to the spike rule file like by exporting like threshold_cur: ${thr_cur}
Does anyone have an idea to achieve this?
Have you tried defining threshold_cur in your main elastalert.yml file?
current_threshold:300
And then in your rule file, reference the threshold_cur value:
threshold_cur: {{current_threshold}}
I am doing something like this in an alert file that sends an email, and it works.