-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(map.jinja
): use pillar/config .get
according to __cli
option
#102
fix(map.jinja
): use pillar/config .get
according to __cli
option
#102
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK salt-ssh
works with config.get
. No need to distinguish between the two.
(defaults.merge
is the one which does/did not work with salt-ssh
, IIRC.)
My suggested change is to keep the code as DRY as possible and abstain from defaults.merge
.
Edit:
Just understood from #95 (comment) that config.get
lacks merge=True
.
I'm thinking of solving this with grains.filter_by
.
Edit 2:
Docs say that config.get
hast merge=recurse
and merge=override
.
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.config.html#salt.modules.config.get
|
||
{#- Merge the template config (e.g. from pillar) #} | ||
{%- set template = salt['config.get']('template', default=defaults, merge=True) %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIR the key in "salt
vs. salt-ssh
" was that defaults.merge
does/did not work on salt-ssh
. I suppose config.get
works just fine, but I'd have to test that.
So if we could use config.get
and stick to grains.filter_by
(instead of defaults.merge
) we'd just need one code path without distinguishing between salt
and salt-ssh
.
From my tests salt-ssh
seems to be able to use config.get
:
% salt-ssh host config.get apache:configdir
host:
/etc/apache2
% salt-ssh host pillar.get apache:configdir
host:
/etc/apache2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.get
wasn't ever the issue itself (for salt-ssh
) but the problem faced has been merging back the return from config.get
into the rest of the map. defaults.merge
is one way of reliably achieving this but if grains.filter_by
can be coerced do it instead, that's even better.
template/map.jinja
Outdated
merge=salt['grains.filter_by'](osmap, grain='os', | ||
merge=salt['grains.filter_by'](osfingermap, grain='osfinger', | ||
merge=salt['config.get']('template:lookup', default={}) | ||
{%- if salt['config.get']('__cli') == 'salt-call' %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may run into problems because salt-call
and salt-ssh
are two different ways of using Salt.
But maybe defaults.merge
is broken for salt-call
too, so this approach would be fine.
# salt-call grains.get saltpath
local:
/usr/local/lib/python3.6/site-packages/salt
# salt-call config.get __cli
local:
salt-call
#% salt-ssh host grains.get saltpath
host:
/var/tmp/.root_99b0b6_salt/pyall/salt
#% salt-ssh host config.get __cli
host:
salt-call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good point, which has helped establish that config.get __cli
is not good enough by itself to identify salt-ssh
. Just tried salt-call
on a standard minion and it worked fine with defaults.merge
, so it shouldn't lose out by being batched alongside salt-ssh
(if we do have to use defaults.merge
as the solution).
I've got some config.items
diffs between all of the types, so I'm sure we can find a reliable solution (if required).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(my 2 cents) salt-call
can also be used to run salt commands from a minion and not a master, but with the same results I think.
@myii did you try the following? ( {#- Merge the template pillar/grains/etc. #}
{%- set template = salt['config.get']('template', default=defaults, merge='recurse') %}
#salt|~ % salt-ssh host1 config.get empty merge=recurse "default={'zzzz': 'asdf'}"
#[ERROR ] TypeError encountered executing config.get: get() got an unexpected keyword argument 'merge'
host1:
TypeError encountered executing config.get: get() got an unexpected keyword argument 'merge'
#salt|~ % salt 'host2' config.get empty merge=recurse "default={'zzzz': 'asdf'}"
host2:
----------
zzzz:
asdf Now that's a bummer! |
@alxwr I'm assuming you meant |
Assuming that -salt-call --local config.items
+salt-ssh 'minion' config.items
cachedir:
- /var/cache/salt/minion
+ /var/tmp/.root_08c4d3_salt/running_data/var/cache/salt/minion
caller_floscript:
- /usr/lib/python2.7/dist-packages/salt/daemons/flo/caller.flo
+ /var/tmp/.root_08c4d3_salt/pyall/salt/daemons/flo/caller.flo
conf_file:
- /etc/salt/minion
+ /var/tmp/.root_08c4d3_salt/minion
config_dir:
- /etc/salt
+ /var/tmp/.root_08c4d3_salt
extension_modules:
- /var/cache/salt/minion/extmods
+ /var/tmp/.root_08c4d3_salt/running_data/var/cache/salt/minion/extmods
+ fileserver_list_cache_time:
+ 3
fun:
pythonexecutable:
- /usr/bin/python
+ /usr/bin/python2.7
pythonpath:
- - /usr/bin
+ - /var/tmp/.root_08c4d3_salt/py2
+ - /var/tmp/.root_08c4d3_salt/pyall
+ - /var/tmp/.root_08c4d3_salt
- /usr/lib/python2.7
- /usr/lib/python2.7/plat-x86_64-linux-gnu
- /usr/lib/python2.7/lib-tk
- 6
- final
- 0
saltpath:
- /usr/lib/python2.7/dist-packages/salt
+ /var/tmp/.root_08c4d3_salt/pyall/salt
grains_cache:
- False
+ True
log_file:
- /var/log/salt/minion
+ /var/tmp/.root_08c4d3_salt/running_data/salt-call.log
log_level:
- warning
+ quiet
minion_floscript:
- /usr/lib/python2.7/dist-packages/salt/daemons/flo/minion.flo
+ /var/tmp/.root_08c4d3_salt/pyall/salt/daemons/flo/minion.flo
+ output:
+ json
pidfile:
- /var/run/salt-minion.pid
+ /var/tmp/.root_08c4d3_salt/running_data/var/run/salt-minion.pid
pki_dir:
- /etc/salt/pki/minion
+ /var/tmp/.root_08c4d3_salt/running_data/etc/salt/pki/minion
print_metadata:
- False
+ True
retcode_passthrough:
- False
+ True
root_dir:
- /
+ /var/tmp/.root_08c4d3_salt/running_data
sock_dir:
- /var/run/salt/minion
+ /var/tmp/.root_08c4d3_salt/running_data This seconds some suggestions made by @alxwr in #21 (comment), regarding paths such as |
Looking at the above diff, |
Yes, indeed. Sry, it was late. And my test was bogus.
Now I understand. :-) Thanks! Well, I guess the right solution would be to implement the I came up with a solution supporting both {%- set _lookup = salt['config.get']('template:lookup', default={}) %}
{%- set _config = salt['config.get']('template', default={}) %}
{%- set template = salt['grains.filter_by'](default_settings, default='template',
merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
merge=salt['grains.filter_by'](osmap, grain='os',
merge=salt['grains.filter_by'](osfingermap, grain='osfinger',
merge=salt['grains.filter_by']({'lookup': _lookup}, default='lookup',
merge=salt['grains.filter_by']({'config': _config}, default='config')
)
)
)
)
) %} Patch: alxwr@a127f91 |
@myii I was able to merge mapped values and # cat /tmp/template-formula.conf
########################################################################
# File managed by Salt at <salt://template/files/default/example.tmpl.jinja>.
# Your changes will be overwritten.
########################################################################
This is another example file from SaltStack template-formula.
{'config': '/tmp/template-formula.conf', 'tofs': {'source_files': {'template-config-file-file-managed': ['example.tmpl.jinja']}}, 'pkg': 'zsh', 'service': {'name': 'template'}}
|
@alxwr That looks like an excellent development! One thing I'd like to discuss further is adding |
@myii Take your time, I'm busy too. I should be working on another project since two hours, but this issue here is much more exciting. :-) So, question for later: |
@alxwr Another - Rendering SLS 'base:template.test' failed: Jinja error: get() got an unexpected keyword argument 'merge'
/var/tmp/.root_08c4d3_salt/running_data/var/cache/salt/minion/files/base/template/map.jinja(11):
---
[...]
{#- Start imports as #}
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %}
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %}
{%- set _lookup = salt['config.get']('template:lookup', default={}, merge='recurse') %} <======================
{%- set _config = salt['config.get']('template', default={}, merge='recurse') %}
{%- set template = salt['grains.filter_by'](default_settings,
default='template',
merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
[...]
---
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 393, in render_jinja_tmpl
output = template.render(**decoded_context)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 7, in top-level template code
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 1013, in make_module
return TemplateModule(self, self.new_context(vars, shared, locals))
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 1070, in __init__
self._body_stream = list(template.root_render_func(context))
File "/var/tmp/.root_08c4d3_salt/running_data/var/cache/salt/minion/files/base/template/map.jinja", line 11, in top-level template code
{%- set _lookup = salt['config.get']('template:lookup', default={}, merge='recurse') %}
TypeError: get() got an unexpected keyword argument 'merge' |
@alxwr The benefit of using
The drawback is that it's surely slower than stopping at the first match found. Note: the breakdown above may not be exact, perhaps someone else can correct any issues. |
@myii we could make the |
@alxwr That's interesting and it's given me an idea of how to finally make |
map.jinja
): use pillar/config .get
according to __cli
optionmap.jinja
): use pillar/config .get
according to __cli
option
@alxwr Unfortunately, the new solution isn't working for me (according to the method shown above). Using an empty pillar: template: {} But setting the following values in the minion's config file: template:
lookup:
minion: template-minion Both --- `salt-ssh`
+++ `salt-minion`
@@ -1,9 +1,5 @@
{
"config": "/etc/template.d/custom-ubuntu.conf",
- "lookup": {
- "minion": "template-minion"
- },
- "minion": "template-minion",
"pkg": "template-ubuntu",
"service": {
"name": "template" So the |
My point was that the formula's defaults ( |
@myii: I sketched a configurable |
@alxwr No, actually |
@alxwr The problem with |
@myii We should be collecting known What if we implement the behaviour of {# determine is_salt_ssh here #}
{%- if is_salt_ssh %}
{%- set _lookup = salt['pillar.get']('template:lookup', default={}) %}
{%- set _config = salt['pillar.get']('template', default={}) %}
{#- including the possibility of merging minion data etc. into the dict 'the salt-ssh way' #}
{%- else %}
{%- set _merge = salt['config.get']('template:config_get_merge', default='recurse') %}
{%- set _lookup = salt['config.get']('template:lookup', default={}, merge=_merge) %}
{%- set _config = salt['config.get']('template', default={}, merge=_merge) %}
{%- endif %} |
@alxwr Yes, that's what's being done by this original PR but in a convoluted way! I'm won't be around for a while but we can keep working on finding a simplified solution. How do you merge back into the map with the method you've outlined in the comment above? |
@myii I use |
@myii Take your time. :-) |
This ticket is reopened now. |
Thanks for chasing that up @noelmcloughlin. |
d88c9bc
to
976fe11
Compare
@alxwr @vutny OK, we're almost there thanks to you both for your input. Please check over my last changes in 976fe11, in order to detect the type of call that has been made (i.e. to differentiate execution for |
map.jinja
): use pillar/config .get
according to __cli
optionmap.jinja
): use pillar/config .get
according to __cli
option
c6a9726
to
3b0a7b5
Compare
@myii I've made some minor remarks, but overall: LGTM |
@myii: Maybe our testing was flawed! (Nevertheless I took the time to review your patch, because in I came across this because I need to set a minion setting for salt.modules.tls. Based on your comment #102 (comment) I did the following:
If I add the minion options to # roster
host:
minion_opts:
template:
lookup:
minion: template-minion % salt-ssh host config.get 'template'
host:
----------
lookup:
----------
minion:
template-minion Grains work too and can be overwritten via % salt-ssh host config.get 'kernel'
host:
FreeBSD
% salt-ssh host config.get 'kernelrelease'
host:
fancy So in If we just picked the wrong place to put our config, then So we could change the {%- if cli == 'min' or cli == 'cll' %}
{%- set default_merge_strategy = "recurse" %}
{%- else %}
{%- set default_merge_strategy = None %}
{%- endif %}
{%- set merge_strategy = salt['config.get']('template:config_get_merge_strategy', default=default_merge_strategy) %}
{# Don't set merge= if merge_strategy is None #} Please correct me if I'm wrong. (It's been a few days since my last comments.) |
* `merge` not available via. `salt-ssh` * Additionally, fix 5dc0b86 in saltstack-formulas#95 - No option `merge=True` for `config.get`
3b0a7b5
to
00e474c
Compare
After a lot of work together with @alxwr in our Slack/IRC/Matrix room, we've finally concluded that the most reliable option is to simply remove the For reference, all of the discussions start here: https://freenode.logbot.info/saltstack-formulas/20190515#c2178767. |
@myii LGTM |
@alxwr Not at all, like yourself I wanted to reach the best available solution and thanks to your help, I believe we've got there. I feel confident that this change will open up many possibilities with the rest of our formulas, so it was well worth the effort and time. |
🎉 This PR is included in version 2.1.4 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
* To distinguish between: - `salt-minion` - `salt-call` - `salt-ssh` * Invoked like `map.jinja`: - `{%- from tplroot ~ "/libsaltcli.jinja" import cli with context %}` * Based upon work done in PRs: saltstack-formulas#102, saltstack-formulas#114 & saltstack-formulas#115
* To distinguish between: - `salt-minion` - `salt-call` - `salt-ssh` * Invoked like `map.jinja`: - `{%- from tplroot ~ "/libsaltcli.jinja" import cli with context %}` * Based upon work done in PRs: saltstack-formulas#102, saltstack-formulas#114 & saltstack-formulas#115 * Finalised from saltstack-formulas/libvirt-formula#71 * Required by saltstack-formulas#186
config.get
instead ofpillar.get
#95merge=True
forconfig.get
Usepillar.get
forsalt-call
(i.e.salt-ssh
)Useconfig.get
via.defaults.merge
otherwiseReintroduce based on 775a930 in update formula with map.jinja and style guide references, improve REA… #20Update: The whole method of fixing this changed completely by the end of this PR.
Apologies for seeking multiple reviews again but
map.jinja
is the core of our formulas and this is a significant change, merging the map based on the type of minion. In #95 (comment), I mentioned that it appeared to be either one or the other:But in #21 (comment), @alxwr mentioned that there was a way around this:
Asked on Slack:
Resulting in this PR, which constructs the map conditionally:
salt-call
(i.e.salt-ssh
): Use the recentmap.jinja
method of usingpillar.get
.salt-minion
): Useconfig.get
via.defaults.merge
.