Very Simple Template Substitutions Using Python Perl
Very Simple Template Substitutions Using Python Perl
2020-03-15T20:50:05Z
Introduction
It is quite common to treat text files as templates which contains place holders that will be replaced by some real values. Python jinja or Ruby erb template engines are there to resolve for these kind of issues. They are quite feature rich. Following is a very simple Template substitution program in Python and Perl that does similar kind of job. Its very basic but works for simple scenarios.
Using Python
Consider following text file test_file.txt with place holders in it.
This is a %TEST1% . Another %TEST1% %TEST1%
Now this is a %TEST2%.
Following is the file var.py containing values of above place holder.
TEST1="test_string1"TEST2="test_string2"
Following is the python program that will replace above values in test_file.txt from var.py file.
#! /usr/bin/env pythonimport reimport fileinputimport sysfrom var import*def print_data_vars(matchObj):returnglobals()[matchObj.group(1)]for line in fileinput.input('test_file', inplace=True): line = re.sub(r'%(.+?)%', print_data_vars, line) sys.stdout.write(line)
when above program is run , it will replace placeholders with values in existing test_file.txt file.
Using Shell and Perl
Assuming var.py and test_file.txt contents are same as above, same effect can also be achieved using shell and perl combination as follows.
Saltstack: Call runner from minion (jinja conditionals)
Saltstack: Call runner from minion (jinja conditionals)
2020-03-15T21:05:23Z
Introduction
Sometimes a minion may need to execute something which is central to whole system but based on some condition. e.g. Minion may need to update an external common Database whose write access is given only to master. Therefore minion may need to ask salt-master to execute on its behalf. This can be done by
Minion calling runner on salt-master
runner executing a custom execution module that provides access to DB
As a custom execution module needs to be executed on salt-master, therefore salt-minion also needs to be installed on salt-master. And configure salt-minion to accept the commands from salt-master. This will be configured as a normal salt-minion. Nothing special needs to be done. Thus salt-master machine would be running both salt-master and salt-minion.
Once above DB operation is done, something else might need to be done. This can be achieved by using jinja in state files.
Pre-Requisites
salt-master installed and configured on a machine. I used fedora-24 VM.
salt-minion installed and configured to accept command from salt-master.
Note: In /etc/salt/master file, log_level is set to debug. Normally this will be info. Also take a note of peer_run config. This is must. At the moment this is very open and you may like to narrow it down.
Note1: In above state file, if the execution of execution module my_module is True then tcpdump will be installed otherwise git will be installed.
Execution
Run following command to sync all modules with all minions.
root@salt-master# salt '*' saltutil.sync_allfedora2.vagrant.box:----------beacons:engines:grains:log_handlers:modules:- modules.custom_module- modules.my_moduleoutput:proxymodules:renderers:returners:sdb:states:utils:fedora.vagrant.box:----------beacons:engines:grains:log_handlers:modules:- modules.custom_module- modules.my_moduleoutput:proxymodules:renderers:returners:sdb:states:utils:
Now execute above state file on remote minion by running following command.
root@salt-master# salt 'fedora2.vagrant.box' state.sls jinja_state_file_calling_runner_on_masterfedora2.vagrant.box:----------ID: install tcpdumpFunction: pkg.installedName: tcpdumpResult: TrueComment: Package tcpdump is already installedStarted: 20:05:03.065341Duration: 420.049 msChanges:Summary for fedora2.vagrant.box------------Succeeded: 1Failed: 0------------Total states run: 1Total run time: 420.049 ms
Sometimes a minion may need to execute something which is central to whole system. e.g. Minion may need to update an external common Database whose write access is given only to master. Therefore minion may need to ask salt-master to execute on its behalf. This can be done by
Minion calling runner on salt-master
runner executing a custom execution module that provides access to DB
As a custom execution module needs to be executed on salt-master, therefore salt-minion also needs to be installed on salt-master. And configure salt-minion to accept the commands from salt-master. This will be configured as a normal salt-minion. Nothing special needs to be done. Thus salt-master machine would be running both salt-master and salt-minion.
Pre-Requisites
salt-master installed and configured on a machine. I used fedora-24 VM.
salt-minion installed and configured to accept command from salt-master.
Note: In /etc/salt/master file, log_level is set to debug. Normally this will be info. Also take a note of peer_run config. This is must. At the moment this is very open and you may like to narrow it down.
Now we will ask minion to execute runner on master.
Create a state file /srv/salt/state_file_calling_runner_on_master.sls as following:
Below_is_state_that_calls_runner_from_minion:module.run:-name: publish.runner-m_fun: my_runner.my_runner_function-arg:-'a_string'-'b_string'# Following will not work. Because in following case, publish.runner is a state module which does not exists in minions.# In above case, publish.runner is actually an execution module.# Below_is_state_that_calls_runner_from_minion:# publish.runner:# - name: my_runner.my_runner_function##
Execution
Run following command to sync all modules with all minions.
root@salt-master# salt '*' saltutil.sync_allfedora2.vagrant.box:----------beacons:engines:grains:log_handlers:modules:- modules.custom_module- modules.my_moduleoutput:proxymodules:renderers:returners:sdb:states:utils:fedora.vagrant.box:----------beacons:engines:grains:log_handlers:modules:- modules.custom_module- modules.my_moduleoutput:proxymodules:renderers:returners:sdb:states:utils:
Now execute above state file on remote minion by running following command.
root@salt-master# salt 'fedora2.vagrant.box' state.sls state_file_calling_runner_on_masterfedora2.vagrant.box:----------ID: Below_is_state_that_calls_runner_from_minionFunction: module.runName: publish.runnerResult: TrueComment: Module function publish.runner executedStarted: 19:36:53.706742Duration: 462.667 msChanges:----------ret:- a_string_modified- b_string_modifiedSummary for fedora2.vagrant.box------------Succeeded: 1 (changed=1)Failed: 0------------Total states run: 1Total run time: 462.667 ms