Concatenate strings in file_line resource - regex

I'm trying to concatenate a string in a puppet manifest like so:
file_line {'Append to /etc/hosts':
ensure => present,
line => "${networking['ip'] + '\t'}${networking['fqdn'] + '\t'}${networking['hostname']}",
match => "${'^#?'+ networking['ip'] + '\s+' + networking['fqdn'] + '\s+' + networking['hostname']}",
path => '/etc/hosts'
}
I either get syntax errors or in the case above:
The value '' cannot be converted to Numeric
Which I'm guessing means that it doesn't like the plus operator.
So how do I interpolate the strings in the match and line attributes?

The problem here is that the operator + is restricted to only Numeric types (documentation). It cannot be used with String types. However, the spacing and regular expressions can still be used as normal without attempting a string concatenate. These merely need to be placed outside the variable interpolation. Therefore:
file_line { 'Append to /etc/hosts':
ensure => present,
line => "${networking['ip']}\t${networking['fqdn']}\t${networking['hostname']}",
match => "^#?${networking['ip']}\s+${networking['fqdn']}\s+${networking['hostname']}",
path => '/etc/hosts'
}
should resolve your issues with the type mismatch and the + operator.

i know you already got your answer, but you can make life easier using host to manage /etc/hosts:
host {
"localhost": ip => "127.0.0.1";
"$::puppet_server":
ip => "1.2.3.4",
host_aliases => [ "puppetserver" ],
;
"dns-01.tld":
ip => "1.2.3.5",
host_aliases => [ "dns-01" ],
;
"dns-02.tld":
ip => "1.2.3.6",
host_aliases => [ "dns-02" ],
;
}

Related

Proper way to create index template that is applied to all in ElasticSearch

I'm still new to the ElasticSearch technology and right now struggle with creating index template that will be applied to all new indices but can't create proper one.
Currently running ElasticSearch 7.9.2 and it's documentation index templates informs that index_patterns field is required and should be array of wildcards (* matches any string).
Naturally I have tried using ["*"] as pattern via Kibana's console for request:
PUT _index_template/template_1
{
"index_patterns": ["*"],
"priority": 0
}
I've got:
{
"error" : {
"root_cause" : [
{
"type" : "null_pointer_exception",
"reason" : null
}
],
"type" : "null_pointer_exception",
"reason" : null
},
"status" : 500
}
while for request that added empty settings:
PUT _index_template/template_1
{
"index_patterns": ["*"],
"template": {
"settings": {
}
},
"priority": 0
}
I've received:
#! Deprecation: index template [template_1] has index patterns [*] matching patterns from existing older templates [.monitoring-es,.triggered_watches,.management-beats,.transform-internal-005,.logstash-management,.monitoring-kibana,.kibana-event-log-7.9.2-template,.ml-config,.watch-history-11,.ml-meta,ilm-history,.monitoring-logstash,.ml-state,.slm-history,.ml-inference-000002,.monitoring-beats,.monitoring-alerts-7,.ml-anomalies-,.watches,.ml-notifications-000001,.transform-notifications-000002,.ml-stats] with patterns (.monitoring-es => [.monitoring-es-7-*],.triggered_watches => [.triggered_watches*],.management-beats => [.management-beats],.transform-internal-005 => [.transform-internal-005],.logstash-management => [.logstash],.monitoring-kibana => [.monitoring-kibana-7-*],.kibana-event-log-7.9.2-template => [.kibana-event-log-7.9.2-*],.ml-config => [.ml-config],.watch-history-11 => [.watcher-history-11*],.ml-meta => [.ml-meta],ilm-history => [ilm-history-2*],.monitoring-logstash => [.monitoring-logstash-7-*],.ml-state => [.ml-state*],.slm-history => [.slm-history-2*],.ml-inference-000002 => [.ml-inference-000002],.monitoring-beats => [.monitoring-beats-7-*],.monitoring-alerts-7 => [.monitoring-alerts-7],.ml-anomalies- => [.ml-anomalies-*],.watches => [.watches*],.ml-notifications-000001 => [.ml-notifications-000001],.transform-notifications-000002 => [.transform-notifications-*],.ml-stats => [.ml-stats-*]); this template [template_1] will take precedence during new index creation
{
"acknowledged" : true
}
Response depends only on existence of empty template.settings it seems like it might be slightly bugged.
Nevertheless latter approach seems to work however deprecation warning sounds dangerous and discouraging (I've tried setting priority to 0 but to no avail). However Documentation of 6.8 version sports example of "*". So such functionality existed not long ago.
What is the proper way, if there is one, to construct "match all" index template?
Currently to match all indices you really need to use the * as the index pattern and the warning is there because it will match anything, including the internal systems indices.
Depending on what is in your template this can lead to things not working right or break your system.
There is an open issue on github about it, mostly regarding to the .security index, which is also affected when you use an match all index pattern, and there is another open issue that also deals with this problem.

Laravel Regex Validattion - No ending delimiter '/' found

I don't know why am receiving a:
No ending delimiter '/' found`
Here is a live sample
Here is my regex validations
$validation = Validator::make($request->all(), [
'cpf' => 'required|regex:/[0-9]{11}/',
'identidade' => 'required|regex:/[0-9]{6,11}/'
]);
This one regex:/[0-9]{11}/ is working fine;
But when I put a min delimiter regex:/[0-9]{6,11}/ I get the error.
Anyone know why?
Which version of Laravel are you using?
I'm using 5.8 and it is working fine.
But you can also try to use an array for the rules:
$validation = Validator::make($request->all(), [
'cpf' => ['required', 'regex:/[0-9]{11}/'],
'identidade' => ['required', 'regex:/[0-9]{6,11}/']
]);

Logstash can not handle multiple heterogeneous inputs

Let's say you have 2 very different types of logs such as FORTINET and NetASQ logs and you want:
grok FORTINET using a regex, ang grok NETASQ using an other regex.
I know that with "type"in the input file and "condition" in the filter we can resolve this problem.
So I used this confing file to do it :
input {
file {
type => "FORTINET"
path => "/fortinet/*.log"
sincedb_path=>"/logstash-autre_version/var/.sincedb"
start_position => 'beginning'
}
file {
type => "NETASQ"
path => "/home/netasq/*.log"
}
}
filter {
if [type] == "FORTINET" {
grok {
patterns_dir => "/logstash-autre_version/patterns"
match => [
"message" , "%{FORTINET}"
]
tag_on_failure => [ "failure_grok_exemple" ]
break_on_match => false
}
}
if [type] == "NETASQ" {
# .......
}
}
output {
elasticsearch {
cluster => "logstash"
}
}
And i'm getting this error :
Got error to send bulk of actions: no method 'type' for arguments(org.jruby.RubyArray) on Java::OrgElasticsearchActionIndex::IndexRequest {:level=>:error}
But if don't use "type" and i grok only FORTINET logs it wroks.
What should i do ?
I'm not sure about this but maybe it helps:
I have the same error and I think that it is caused by the use of these if statements:
if [type] == "FORTINET"
your type field is compared to "FORTINET" but this is maybe not possible because "FORTINET" is a string and type isn't. Some times by setting a type to an input, if there is already a type, the type isn't replaced, but the new type is added to a list with the old type. You should have a look to your data in kibana (or wherever) and try to find something like this:
\"type\":[\"FORTINET\",\"some-other-type\"]
maybe also without all those \" .
If you find something like this try not to set the type of your input explicitly and compare the type in your if-statement to the some-other-type you have found.
Hope this works (I'm working with more complex inputs/forwarders and for me it doesn't, but it is worth a try)

augeas & puppet & whitespace

I try to edit the file limits.com with puppet. I need to add lines this files. I copy the example from:
http://docs.puppetlabs.com/guides/augeas.html
# /etc/puppet/modules/limits/manifests/conf.pp
define limits::conf (
$domain = "root",
$type = "soft",
$item = "nofile",
$value = "10000"
) {
# guid of this entry
$key = "$domain/$type/$item"
# augtool> match /files/etc/security/limits.conf/domain[.="root"][./type="hard" and ./item="nofile" and ./value="10000"]
$context = "/files/etc/security/limits.conf"
$path_list = "domain[.=\"$domain\"][./type=\"$type\" and ./item=\"$item\"]"
$path_exact = "domain[.=\"$domain\"][./type=\"$type\" and ./item=\"$item\" and ./value=\"$value\"]"
augeas { "limits_conf/$key":
context => "$context",
onlyif => "match $path_exact size != 1",
changes => [
# remove all matching to the $domain, $type, $item, for any $value
"rm $path_list",
# insert new node at the end of tree
"set domain[last()+1] $domain",
# assign values to the new node
"set domain[last()]/type $type",
"set domain[last()]/item $item",
"set domain[last()]/value $value",
],
}
and
use case:
limits::conf {
# maximum number of open files/sockets for root
"root-soft": domain => root, type => soft, item => nofile, value => 9999;
"root-hard": domain => root, type => hard, item => nofile, value => 9999;
}
The result inside of limits.conf is:
##student - maxlogins 4
root hard nofile 9999
root soft nofile 9999
How I can put whitespace or tabs bettwen the values "root hard nofile & value"
I try put the whitespace inside "", try with regex but doesn't works.
Thanks
You can't. Augeas manages spaces automatically, and there is no way to control them manually.
Elaborating on Raphink's correct answer: While augeas works with files in a semantic fashion (it knows what the content represents), there are alternatives if you care for the appearance more.
In your case, it might make more sense to manage your file through a template and take fine grained control of the result.

Concatenating variable and regexp expression in Puppet

Is it possible at all? My use case is setting wildcard domain's docroot. I have a variable
$docroot = "/var/www"
and, inside apache::vhost {} I'm trying to set
virtual_docroot' => "{$docroot}/%-2+",
That however throws an error saying:
Error: Could not parse for environment production: Syntax error at ' => "$docroot/%-2+",
docroot => $docroot,
serveraliases => ['; expected '}' at /tmp/vagrant-puppet-1/manifests/init.pp:22 on node localhost
My guess is that the whole expression is treated as a regexp, and $ treated as a part of the whole expression. Hence my doubts whether that's possible at all.
The answer turns out to be an orphaned apostrophe, misplaced after virtual_docroot, and copied from puppetlabs-apache documentation, where the faulty code is:
apache::vhost { 'subdomain.loc':
vhost_name => '*',
port => '80',
virtual_docroot' => '/var/www/%-2+',
docroot => '/var/www',
serveraliases => ['*.loc',],
}
Removing the apostrophe fixed the problem. Answer to my question is to set that particular configuration line as:
virtual_docroot => "$docroot/%-2+",
Notice the omitted {} characters.