saltstack pattern match failing - salt-stack

I am trying to replace a line in httpd.conf in packer using masterless salt. I always check the pattern using grep command and if it works then the same will work in saltstack too but this time it fails. As I am not good in regular expression please help on this.
I want to replace the below line
CustomLog "|'/apps/CA/secure-proxy/bizportalsps/httpd/bin/rotatelogs' '/apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log' 10M" common
with
CustomLog /apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log common
So I used
Log-file-update:
file.replace:
- name: /apps/CA/secure-proxy/bizportalsps/httpd/conf/httpd.conf
- pattern: 'CustomLog \"\|'/apps/CA/secure-proxy/bizportalsps/httpd/bin/rotatelogs' '/apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log' 10M\" common'
- repl: 'CustomLog /apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log common'
- append_if_not_found: False
But its failing. Any suggestions will be very helpful.
The below is the error.
<pre> <code>
amazon-ebs: [CRITICAL] Rendering SLS 'base:install' failed: while parsing a block mapping
amazon-ebs: in "<unicode string>", line 46, column 7:
amazon-ebs: - pattern: 'CustomLog \"\|'/apps/C ...
amazon-ebs: ^
amazon-ebs: expected <block end>, but found '<scalar>'
amazon-ebs: in "<unicode string>", line 46, column 32:
amazon-ebs: - pattern: 'CustomLog \"\|'/apps/CA/secure-proxy/bizportals ...
amazon-ebs: ^

This is because of the first ' in your pattern.
This should work,
Log-file-update:
file.replace:
- name: /etc/pki/tls/certs/test
- pattern: CustomLog \"|\'/apps/CA/secure-proxy/bizportalsps/httpd/bin/rotatelogs\' \'/apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log\' 10M\" common
- repl: CustomLog /apps/CA/secure-proxy/bizportalsps/httpd/logs/access_log common
- append_if_not_found: False

Related

How to add nested dictionary to dynamic host in Ansible

I have application details in respective vars like below. For example, myapp1 in "QA" environment would look like the below:
cat myapp1_QA.yml
---
APP_HOSTS:
- myapphost7:
- logs:
- /tmp/web/apphost7_access
- /tmp/web/apphost7_error
- myapphost9:
- logs:
- /tmp/web/apphost9_access
- /tmp/web/apphost9_error
- /tmp/web/apphost9_logs
WEB_HOSTS:
- mywebhost7:
- logs:
- /tmp/webserver/webhost7.pid
In this example I wish to create a dynamic host containing the 3 hosts
myapphost7
myapphost9
mywebhost7
and each host has variable log which can be looped to get the file paths:
Below is my ansible play:
---
- hosts: localhost
tasks:
- include_vars:
file: "{{ playbook_dir }}/{{ appname }}_{{ myenv }}.yml"
- name: Dsiplay dictionary data
debug:
msg: "{{ item[logs] }}"
loop: "{{ APP_HOSTS }}"
I get the below error:
ansible-playbook read.yml -e appname=myapp1 -e myenv=QA
TASK [Dsiplay dictionary data] *********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'logs' is undefined\n\nThe error appears to be in '/root/read.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Dsiplay dictionary data\n ^ here\n"}
My requirement is to store "myapphost7", "myapphost9", "mywebhost7" in group using add_hosts: hosts: while a variable logs: having the list of log files.
Note: if no hosts mywebhost7 is defined under WEB_HOSTS: or APP_HOSTS: then nothing should be added to the dynamic host.
Can you please suggest?

how to check 777 permission in multiple directory by ansible

For a single directory my script is running fine, but how to check the same for multiple directories?
Code for a single directory:
---
- name: checking directory permission
hosts: test
become: true
tasks:
- name: Getting permission to registered var 'p'
stat:
path: /var/SP/Shared/
register: p
- debug:
msg: "permission is 777 for /var/SP/Shared/
when: p.stat.mode == "0777" or p.stat.mode == "2777" or p.stat.mode == "4777"
Reading stat_module shows that there is no parameter for recursion. Testing with_fileglob: did not gave the expected result.
So it seems you would need to loop over the directories in a way like
- name: Get directory permissions
stat:
path: "{{ item }}"
register: result
with_items:
- "/tmp/example"
- "/tmp/test"
tags: CIS
- name: result
debug:
msg:
- "{{ result }}"
tags: CIS
but I am sure there can be still more advanced solutions found.

Symfony framework.yaml invalid YAML - but it's not?

I've started getting this error...not sure why or how?
Here is the framework.yaml:
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
ad_ldap:
ldap:
service: Symfony\Component\Ldap\Ldap
base_dn: dc=mydomain,dc=local
search_dn: 'appuser'
search_password: "xxx"
default_roles: ROLE_USER
uid_key: 'sAMAccountName'
filter: '({uid_key}={username})'
Giving me this error:
In FileLoader.php line 180:
The file "/vagrant/project/config/packages/security.yaml" does not
contain valid YAML: Unexpected characters near "rname})' " at
line 12 (near "filter: '({uid_key}={us ername})' ") in
/vagrant/project/config/packages/security.yaml (which is loaded in
resource "/vagrant/project/config/packages/security.yaml").
In YamlFileLoader.php line 693:
The file "/vagrant/project/config/packages/security.yaml" does not
contain valid YAML: Unexpected characters near "rname})' " at
line 12 (near "filter: '({uid_key}={us ername})' ").
In Parser.php line 760:
Unexpected characters near "rname})' " at line 12 (near
"filter: '({uid_key}={username})' ").
The problem is a vendor issue with symfony and is corrected already but not released as of this writing.
https://github.com/symfony/symfony/issues/39229
See: https://symfony.com/doc/current/components/yaml/yaml_format.html#strings
the part:
Strings containing any of the following characters must be quoted. Although you can use double quotes, for these characters it is more convenient to use single quotes, which avoids having to escape any backslash :
:, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, #, `
Can you try quoting all strings there? (using single quotes)
Also, which Symfony version?

Not able to execute lifecycle operation using script plugin

I'm trying to learn how to use script plugin. I'm following script plugin docs here but not able to make it work.
I've tried to use the plugin in two ways. The first, when cloudify.interface.lifecycle.start operation is mapped directly to a script:
tosca_definitions_version: cloudify_dsl_1_3
imports:
- 'http://www.getcloudify.org/spec/cloudify/4.5.5/types.yaml'
node_templates:
Import_Project:
type: cloudify.nodes.WebServer
capabilities:
scalable:
properties:
default_instances: 1
interfaces:
cloudify.interfaces.lifecycle:
start:
implementation: scripts/create_project.sh
inputs: {}
The second with a direct mapping:
tosca_definitions_version: cloudify_dsl_1_3
imports:
- 'http://www.getcloudify.org/spec/cloudify/4.5.5/types.yaml'
node_templates:
Import_Project:
type: cloudify.nodes.WebServer
capabilities:
scalable:
properties:
default_instances: 1
interfaces:
cloudify.interfaces.lifecycle:
start:
implementation: script.script_runner.tasks.run
inputs:
script_path: scripts/create_project.sh
I've created a directory named scripts and placed the below create_project.sh script in this directory:
#! /bin/bash -e
ctx logger info "Hello to this world"
hostname
I'm getting errors while validating the blueprint.
Error when operation is mapped directly to a script:
[2019-04-13 13:29:40.594] [DEBUG] DslParserExecClient - got output from dsl parser Could not extract plugin from operation mapping 'scripts/create_project.sh', which is declared for operation 'start'. In interface 'cloudify.interfaces.lifecycle' in node 'Import_Project' of type 'cloudify.nodes.WebServer'
in: /opt/cloudify-composer/backend/dev/workspace/2/tmp-27O0e1t813N6as
in line: 3, column: 2
path: node_templates.Import_Project
value: {'interfaces': {'cloudify.interfaces.lifecycle': {'start': {'implementation': 'scripts/create_project.sh', 'inputs': {}}}}, 'type': 'cloudify.nodes.WebServer', 'capabilities': {'scalable': {'properties': {'default_instances': 1}}}}
Error when using a direct mapping:
[2019-04-13 13:25:21.015] [DEBUG] DslParserExecClient - got output from dsl parser node 'Import_Project' has no relationship which makes it contained within a host and it has a plugin 'script' with 'host_agent' as an executor. These types of plugins must be installed on a host
in: /opt/cloudify-composer/backend/dev/workspace/2/tmp-279QCz2CV3Y81L
in line: 2, column: 0
path: node_templates
value: {'Import_Project': {'interfaces': {'cloudify.interfaces.lifecycle': {'start': {'implementation': 'script.script_runner.tasks.run', 'inputs': {'script_path': 'scripts/create_project.sh'}}}}, 'type': 'cloudify.nodes.WebServer', 'capabilities': {'scalable': {'properties': {'default_instances': 1}}}}}
What is missing to make this work?
I also found the Cloudify Script Plugin examples from their documentation do not work: https://docs.cloudify.co/4.6/working_with/official_plugins/configuration/script/
However, I found I can make the examples work by adding an executor line in parallel with the implementation line to override the host_agent executor as follows:
tosca_definitions_version: cloudify_dsl_1_3
imports:
- 'http://www.getcloudify.org/spec/cloudify/4.5.5/types.yaml'
node_templates:
Import_Project:
type: cloudify.nodes.WebServer
capabilities:
scalable:
properties:
default_instances: 1
interfaces:
cloudify.interfaces.lifecycle:
start:
implementation: scripts/create_project.sh
executor: central_deployment_agent
inputs: {}

Salt: text file to variable and use the same variable in state file to find&replace

I've run into an issue I havent been able to solve:
I have a file(/etc/osci) that I use on all of my servers as an name for our monitoring(zabbix)
I've created a state file that pushes a template configuration file to the server and and reads the content of /etc/osci to a variable. The next step would be to use that same variable with 'file.replace' function to search for a string and replace it with the variable.
uusnimi=$(cat /etc/osci):
cmd.run
/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644
change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname=$uusnimi'
Now when executing the state file echoing the variable in the target server it provides me the right output:
echo $uusnimi
Server1
but for the life of me I can't figure out how to escape the last line of the above code so it would insert the value not the '$uusnimi' string
Use uusnimi as a jinja variable.
{% set uusnimi = salt['cmd.shell']('cat /etc/osci') %}
/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644
change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname={{ uusnimi }}'

Resources