Hot answers tagged

11

Check out the slsutil.renderer module. This should do what you want salt my-minion-id slsutil.renderer /srv/salt/network/init.sls 'jinja' This module just calls the compile_template function directly for you. Edit: /srv/salt/network/init.sls is the path on the minion, if you are not targeting the master as your minion, you will probably need to do the ...


9

Given how much time I spent weeks ago struggling with a closely-related issue, I wish I'd figured this out sooner. The solution appears to be to use salt.modules.cp.get_template to have the Salt minion retrieve the file, render it through the templating engine and place it in a readable place: # salt my-minion-id cp.get_template salt://network/init.sls /...


5

Yes, that is possible according to this post. When a second jinja template was created and this was included in the base jinja it was called. {% include "checks/subdue.j2" %}


4

This templated SLS file works splendidly: {% if 'components' in salt.pillar.items() %} include: {% for component in salt.pillar.get('components').keys() %} - {{ component }} {% endfor %} {% endif %} However, it requires changing my pillar key structure. From the original question, you see the structure as: my-minion-id: ---------- components: ...


3

There is currently one safe way to do this. You can use {{opts.id}} as an explicit way to grab the minion ID. opts is technically an implementation detail; the opts dictionary is a Salt internal structure that doesn't appear to be formally specified. Its candidacy for the purpose was pointed out to me in a bug comment. In the next version of Salt (so, ...


3

You can also do it like: {% if pillar['components'] is defined %} include: {% for component in pillar['components'] %} - {{ component }} {% endfor %} {% endif %} {% if pillar['components'] is defined %} components: require: {% for component in pillar['components'] %} - {{ component }} {% endfor %} {% endif %}


2

The syntax failures are caused by the presence of the {{...}} expression blocks (normally used for filling the template output with the corresponding content) inside the {%...%} statement blocks. I only used standalone jinja2 templates, so I'm not 100% certain if this applies to Ansible jinja templates as well, but I suspect so. In the Jinja2 {%...%} ...


2

Yes, this can be done. The following should do the trick: "{%- for ip, az in seed.iteritems() %} {%- if 'us-east-1a' in az %} {%- if firstloop is not defined %} {%- set firstloop = 1 %} {{- ip }} {%- else %} {{- " " + ip}} {%- endif %} {%- endif %} {%- endfor%}" To test it with https://cryptic-cliffs-32040.herokuapp.com/, you can ...


2

I have found the answer to my own question after some more investigation. The reason the state was skipping the contents of my if statement was because the hostname of the server was in lower-case (god knows why). Therefore the in-built salt grains.item host which is called upon didn't recognise the server I was specifying.


2

You can have more than if statement per state. The issue is that your conditional doesn't seem actually be checking the hostname, it's just passing everything. Try this: {% if grains['host'] in ['dev-server2', 'test-server2'] %}


1

Since Ansible was installed via Homebrew on macOS, the workaround is to install Ansible using Pip. $ brew remove ansible $ pip3 install ansible $ pip3 list | grep -e ansible -e Jinja2 ansible 2.8.0 Jinja2 2.8 $ ansible-playbook check_jinja.yaml -v TASK [jinja_version] ok: [localhost] => {"changed": false, "msg": "2.8"}


1

You use pretty old ansible, maybe worth to try a new one. Although tests "succeeded" and "success" should be in 2.2 too. But main problem I think it's failed_when: no in your task. It makes test when: aptitude_installed is success completely useless, it will be always true because failed_when: no marks all tasks as successful. If you don't want to stop on ...


1

The magic you need is magic variables, in particular the groups variable, which is a dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it. Then you can use template filters to format the list: use map with regex_replace to add the quotes to all list elements and then join them into the final result. {{ ...


1

The "saltey" way of doing this is typically in a map.jinja file. This one for example... In that, you would: {%- load_yaml as serverlist %} #Note that a default list could be provided here {%- endload %} {%- set serverlist2 = salt['grains.filter_by'](serverlist, merge=salt['pillar.get']('pillarname:server_list_de')) %} {%- set servers = salt['grains....


1

Perhaps a macro might be what you are looking for. Understanding Jinja:Macros In the lib.sls example in the link (pasted below), it sets a value contingent on the value of the if statement. I think a similar approach could work for you. {% macro pythonpkg(pkg) -%} {%- if grains['os'] == 'FreeBSD' -%} py27-{{ pkg }} {%- elif grains['os'] == '...


1

In lack of a better idea I chose this rather long approach: myid.j2 {% for host in groups['zookeeper'] %} {% if host == inventory_hostname %} {{ loop.index }} {% endif %} {% endfor %} The idea is to iterate over all hosts and check if the current host being processed matches the host in the list of hosts. If it matches I write the iteration counter to the ...


Only top voted, non community-wiki answers of a minimum length are eligible