How to provide attribute value in the template .erb file - ruby

How to provide attribute value in a template .erb file
qradar_logs/
├── Berksfile
├── LICENSE
├── README.md
├── attributes
│   └── default.rb
├── chefignore
├── files
│   └── default
│   └── default
├── metadata.rb
├── recipes
│   └── default.rb
├── spec
│   ├── spec_helper.rb
│   └── unit
│   └── recipes
│   └── default_spec.rb
├── templates
│   └── rsyslog.conf.erb
└── test
└── integration
└── default
└── default_test.rb
11 directories, 12 files
In attribute.rb file, I have the following contents:
default['logs']['hostname'] = "169.67.89.72:514"
In my recipe, I provided the following:
hostname = node['logs']['hostname']
In my templates rsyslog.conf file, I would like to use this value based on the value changed in attributes file.
I tried giving:
<%= "#{hostname}" %>
It errors out as:
FATAL: Chef::Mixin::Template::TemplateError: undefined local variable or method `hostname'
How can I access the attributes defined in attribute file in template.erb file?
Thank you

So a few things. First you should use the variables property in your template resource to expose the data to the template:
template '/whatever' do
# ...
variables hostname: node['logs']['hostname']
end
Then you need to use Erb formatting, which you kind of did but not really:
<%= #hostname %>
The variable name matches the key you used with variables.

Related

Correct directory structure for Puppet RSpec testing

I'm having some issues creating unit tests for my Puppet control repository.
I mostly work with roles and profiles with the following directory structure:
[root#puppet]# tree site
site
├── profile
│   ├── files
│   │   └── demo-website
│   │   └── index.html
│   └── manifests
│   ├── base.pp
│   ├── ci_runner.pp
│   ├── docker.pp
│   ├── gitlab.pp
│   ├── logrotate.pp
│   └── website.pp
├── role
│   └── manifests
│   ├── gitlab_server.pp
│   └── nginx_webserver.pp
Where do I need to place my spec files and what are the correct filenames?
I tried placing them here:
[root#puppet]# cat spec/classes/profile_ci_runner_spec.rb
require 'spec_helper'
describe 'profile::ci_runner' do
...
But I get an error:
Could not find class ::profile::ci_runner
The conventional place for a module's spec tests is in the module, with the spec/ directory in the module root. So site/profile/spec/classes/ci_runner_spec.rb, for example.
You could consider installing PDK, which can help you set up the structure and run tests, among other things.

Ansible Molecule how to use multiple group_vars

I have a folder structure like this in Ansible where global variables are at the root group_vars and then environment specific variables are in inventories/dev/group_vars/all etc.
.
├── ansible.cfg
├── group_vars
│   └── all
├── inventories
│   ├── dev
│   │   ├── group_vars
│   │   │   └── all
│   │   └── hosts
│   └── prod
│   ├── group_vars
│   │   └── all
│   └── hosts
└── playbook.yml
I want to use to be able reuse the existing variables in both var files in Molecule but unable to do so as it cannot find the variable. Something similar to the below works but I need both group_vars/all and inventories/dev/group_vars/all
extract of my molecule.yml
provisioner:
name: ansible
inventory:
links:
group_vars: ../../../group_vars
I tried comma separated and that doesn't work because afterall it's just a symlink to the file.

Ansible how to get access to the main level roles dir ? so i can include generic tasks from any playbook?

i have role which i try to get using includ_role
now i have this file structure
.
├── foo_A
│   └── roles
│   ├── foo_deploy
├── foo_B
│   └── roles
│   ├── db_foo
│   │   └── tasks
├── foo_C
│   └── roles
│   ├── package_deploy
│   │   ├── defaults
│   │   ├── files
│   │   └── tasks
│   │   └── main.yml  
├── group_vars
└── roles
└── utilities
├── defaults
├── files
├── handlers
├── meta
├── tasks
└── dpackage.yml 
├── templates
└── vars
I'm calling the include_role with the utilities name from main.yml
but I'm getting an error that that main level role is not units search paths
ERROR! the role 'utilities' was not found in /home/ec2-user/ansible/foo_C/roles:/home/ec2-user/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/ec2-user/ansible/foo_C
The error appears to be in '/home/ec2-user/ansible/foo_C/roles/package_deploy/tasks/main.yml': line 78, column 11, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
include_role:
name: utilities
^ here
how can i get access to the main roles dir under : /home/ec2-user/ansible

Ansible dynamic inventory: unable to use group_vars

Here is my directory structure,
├── README.md
├── internal-api.retry
├── internal-api.yaml
├── ec2.py
├── environments
│   ├── alpha
│   │   ├── group_vars
│   │   │   ├── alpha.yaml
│   │   │   ├── internal-api.yaml
│   │   ├── host_vars
│   │   ├── internal_ec2.ini
│   ├── prod
│   │   ├── group_vars
│   | │   ├── prod.yaml
│   │   │   ├── internal-api.yaml
│   │   │   ├── tag_Name_prod-internal-api-3.yml
│   │   ├── host_vars
│   │   ├── internal_ec2.ini
│   └── stage
│   ├── group_vars
│   │   ├── internal-api.yaml
│   │   ├── stage.yaml
│   ├── host_vars
│   │   ├── internal_ec2.ini
├── roles
│   ├── internal-api
├── roles.yaml
I am using separate config for an ec2 instance with tag Name = prod-internal-api-3, so I have defined a separate file, tag_Name_prod-internal-api-3.yaml in environments/prod/group_vars/ folder.
Here is my tag_Name_prod-internal-api-3.yaml,
---
internal_api_gunicorn_worker_type: gevent
Here is my main playbook, internal-api.yaml
- hosts: all
any_errors_fatal: true
vars_files:
- "environments/{{env}}/group_vars/{{env}}.yaml" # this has the ssh key,users config according to environments
- "environments/{{env}}/group_vars/internal-api.yaml"
become: yes
roles:
- internal-api
For prod deployemnts, I do export EC2_INI_PATH=environment/prod/internal_ec2.ini, likewise for stage and alpha. In environment/prod/internal_ec2.ini I have added instance filter, instance_filters = tag:Name=prod-internal-api-3
When I run my playbook,
I get this error,
fatal: [xx.xx.xx.xx]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'internal_api_gunicorn_worker_type' is undefined"}
It means that it is not able to pick variable from the file tag_Name_prod-internal-api-3.yaml. Why is it happening? Do I need to manually add it in include_vars(I don't think that should be the case)?
Okay, so it is really weird, like really really weird. I don't know whether it has been documented or not(please provide link if it is).
If your tag Name is like prod-my-api-1, then the file name tag_Name_prod-my-api-1 will not work.
Your filename has to be tag_Name_prod_my_api_1. Yeah, thanks ansible for making me cry for 2 days.

Gradle plugin packaging - Why is plugin unexpectedly applied?

So I have:
buildSrc/
├── build.gradle
└── src
├── main
│   ├── groovy
│   │   └── build
│   │   ├── ExamplePlugin.groovy
│   │   └── ExampleTask.groovy
│   └── resources
│   └── META-INF
│   └── gradle-plugins
│   └── build.ExamplePlugin.properties
└── test
└── groovy
└── build
├── ExamplePluginTest.groovy
└── ExampleTaskTest.groovy
Question:
It seems like build.ExamplePlugin.properties maps directly to the build.ExamplePlugin.groovy. Is this the case? Seems terribly inefficient to have only one property in the file. Does it have to be fully qualified, i.e. does the name have to exactly match the full qualification of the class?
Now in the example, I see:
project.pluginManager.apply 'build.ExamplePlugin'
...however if I have that in my test, I get an error to the effect that the simple task the plugin defines, is already defined.
Why bother with test examples that require 'apply' when that is inappropriate for packaging?

Resources