How to read configuration file and store the values of parameters in a variable?

75 views
Skip to first unread message

varun bhatnagar

unread,
Sep 20, 2017, 4:31:00 AM9/20/17
to Salt-users
Hi,

I have a configuration file (config.properties) on windows minions which looks like this:

com.work.dir=G:/work/programs/agents/br_delivery/work
com.type.name=sample_name

I want to read the two parameters in the file and then store the values to a variable using jinja format.
Can anyone please suggest something?

BR,
Varun

varun bhatnagar

unread,
Sep 20, 2017, 6:31:51 AM9/20/17
to Salt-users
I created a sample state file was trying to run it but I get the below error:

>#cat read_data/read_data.sls
{%- set var1 = salt.cmd.run('type C:\salt\conf\minion | findstr alternative.mysql.host') %}
get_data_in_variable:
  module.run:
    - name: test.echo
    - text: "Printing the variable -----> '{{ var1 }}'"


>#salt -t 60 '192.168.133.188' state.apply read_data.read_data
jid: 20170920115532084030
    Data failed to compile:
----------
    Rendering SLS 'base:read_data.read_data' failed: Problem running salt function in Jinja template: Unable to run command '['type', 'C:\\salt\\conf\\minion', '|', 'findstr', 'alternative.mysql.host']' with the context '{'timeout': None, 'with_communicate': True, 'shell': False, 'bg': False, 'stderr': -2, 'env': {'TMP': 'C:\\Windows\\TEMP', 'COMPUTERNAME': 'WIN-D8G7L2C6FAR', 'USERDOMAIN': 'WORKGROUP', 'PSMODULEPATH': 'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 142 Stepping 9, GenuineIntel', 'PROGRAMFILES': 'C:\\Program Files', 'PROCESSOR_REVISION': '8e09', 'SYSTEMROOT': 'C:\\Windows', 'PATH': 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\salt;C:\\Python27;C:\\Python27\\Lib\\site-packages;C:\\Python27\\Lib;C:\\Python27\\DLLs;C:\\Python27\\Lib\\lib-tk;C:\\Python27\\Scripts;C:\\Python27\\Lib\\site-packages;;c:\\salt\\bin\\lib\\site-packages\\pywin32_system32;/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;c:\\salt\\bin\\lib\\site-packages\\pywin32_system32', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'WINDOWS_TRACING_FLAGS': '3', 'TEMP': 'C:\\Windows\\TEMP', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'ALLUSERSPROFILE': 'C:\\ProgramData', 'LOCALAPPDATA': 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local', 'PROGRAMW6432': 'C:\\Program Files', 'USERNAME': 'WIN-D8G7L2C6FAR$', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'PROGRAMDATA': 'C:\\ProgramData', 'PYTHONPATH': 'C:\\Python27;C:\\Python27\\Lib\\site-packages;C:\\Python27\\Lib;C:\\Python27\\DLLs;C:\\Python27\\Lib\\lib-tk;C:\\Python27\\Scripts;C:\\Python27\\Lib\\site-packages', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'FP_NO_HOST_CHECK': 'NO', 'WINDIR': 'C:\\Windows', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log', 'APPDATA': 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming', 'SYSTEMDRIVE': 'C:', 'PYTHONHOME': '', 'NUMBER_OF_PROCESSORS': '1', 'PROCESSOR_LEVEL': '6', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'OS': 'Windows_NT', 'PUBLIC': 'C:\\Users\\Public', 'USERPROFILE': 'C:\\Windows\\system32\\config\\systemprofile'}, 'stdout': -1, 'stdin': None, 'cwd': 'C:\\Windows\\system32\\config\\systemprofile'}', reason: command not found; line 2

---

{%- set var1 = salt.cmd.run('type C:\salt\conf\minion | findstr alternative.mysql.host') %}    <======================
get_data_in_variable:
  module.run:
    - name: test.echo
    - text: "Printing the variable -----> '{{ var1 }}'"
---
ERROR: Minions returned with non-zero exit code

But when I login to the machine and fire the same command from command prompt as admin then the same works fine. Any suggestions how to fix this?

BR,
Varun

Ruggero Marchei

unread,
Sep 20, 2017, 3:49:29 PM9/20/17
to Salt-users
A dirty way to read that file in jinja is:


{% import_text '/tmp/config.properties' as f %}
{% set dir = f|regex_search('com\.work\.dir=(.*)')|first %}
{% set name = f|regex_search('com\.type\.name=(.*)')|first %}
read_properties_file
:
  test
.show_notification:
   
- text: |
        dir
=> {{ dir }}
        name
=> {{ name }}


          ID
: read_properties_file
   
Function: test.show_notification
     
Result: True
     
Comment: dir => G:/work/programs/agents/br_delivery/work
              name
=> sample_name
     
Started: 12:48:43.305768
   
Duration: 0.174 ms
     
Changes:

varun bhatnagar

unread,
Sep 21, 2017, 6:59:58 AM9/21/17
to Salt-users
Hello Ruggero,

Thanks a lot for the reply.

I tried what you have mentioned but it is running into error:

 salt -t 60 'salt' state.apply update_agent.regex_oper

salt:
    Data failed to compile:
----------
    Rendering SLS 'base:update_agent.regex_oper' failed: Jinja syntax error: no filter named 'regex_search'; line 2

---
{% import_text '/tmp/config.properties' as f %}
{% set dir = f|regex_search('com\.work\.dir=(.*)')|first %}    <======================
{% set name = f|regex_search('com\.type\.name=(.*)')|first %}
read_properties_file:
  test.show_notification:
    - text: |
        dir => {{ dir }}
[...]
---
ERROR: Minions returned with non-zero exit code

Is there anything that I missed here?

BR,
Varun

varun bhatnagar

unread,
Sep 21, 2017, 7:27:16 AM9/21/17
to Salt-users
Hi,

Got it!
I had an older version of salt 2016.11 and this got introduced in 2017.0. I have upgraded it to latest and now it works.

BR,
Varun

Jeremy McMillan

unread,
Sep 23, 2017, 11:56:21 PM9/23/17
to Salt-users
This should probably be exposed to salt as grains if it's relatively static, and the properties file is supplied by something completely outside salt.

#put this somewhere in your salt file_roots in a file _grains/br_delivery.py
#  remember to use saltutil.sync_grains etc.

import logging

def br_delivery_config():
   
log = logging.getLogger(__name__)
    grains
={'br_delivery': {'config':{}}}
    config
= grains['br_delivery']['config']
   
try:
       
for line in open('/tmp/config.properties'):
            log
.debug("found /tmp/config.properties")
           
if '=' in line:
                k
,v = line.split('=', 1)
                k
= k.replace('.', '_')
                config
[k] = v
                log
.trace("{}: found '{}' = '{}'".format(__name__, k, v))
       
return grains
   
except Exception as E:
        log
.info("Cannot find/parse /tmp/config.properties")
        log
.debug(E)
       
pass #
 
 
which would be usable like this

{% set config_properties = salt['grains.get']('br_delivery:config') %}
work dir
: {{ config_properties.com_work_dir }}
type name
: {{ config_properties.com_type_name }}
Reply all
Reply to author
Forward
0 new messages