I have snippet like this in the init.sls:
{% for server, args in pillar.get('servers', {}).items() %}
software-server#{{ server }}
service.running:
- enable: true
- require:
- pkg: software_pkgs
- watch:
- file: software_config
/etc/software/{{server}}.json:
file.managed:
- source: salt://software/files/config.json.j2
- template: jinja
{% endfor %}
config.json.j2:
{
listen: {{server}}:{{listen_addr}}
}
and in the pillar:
software.servers:
server1:
listen_addr:10.0.0.1
server2:
listen_addr:127.0.01
in each of the {{server}}.json the listen_addr is different. I don't know if saltstack has something like a scope for current loop, or is there a workaround for this.
You probably need to use context or defaults options in file.managed:
file.managed
In your example it would like like :
/etc/software/{{server}}.json:
file.managed:
- source: salt://software/files/config.json.j2
- template: jinja
- context:
server: {{ server }}
listen_addr: {{ server['listen_addr'] }}
Related
{% set value = salt['mine.get']('{{ server }}', 'xml.set_value')('/opt/suite/version.xml', './/Version']) %}
Get value:
cmd.run:
- name: echo {{ value }}
Need help in writing the set value.
I am trying to override PriceHelper class by adding getOriginalPrice function but since the service not registered, on service.yml, I put:
services:
AppBundle\Helper\PriceHelper\:
class: AppBundle\Helper\PriceHelper
arguments:
- "#sylius.calculator.product_variant_price"
tags:
- { name: templating.helper, event: sylius.templating.helper.price, method: getOriginalPrice, alias: sylius_calculate_original_price }
On twig, I added:
{%- macro calculateOriginalPrice(variant) -%}
{% from _self import convertAndFormat %}
{{- convertAndFormat(variant|sylius_calculate_original_price({'channel': sylius.channel})) }}
{%- endmacro -%}
and replaced:
{{ money.calculatePrice(product|sylius_resolve_variant) }}
to
{{ money.calculateOriginalPrice(product|sylius_resolve_variant) }}
Error:
Unknown "sylius_calculate_original_price" filter. Did you mean
"sylius_calculate_price"?
Any idea?
Problem solved.
The services.yml is actually fine. Just need to add below in config.yml:
twig:
globals:
sylius_calculate_original_price: "#app.templating.helper.price"
and in twig:
{{ sylius_calculate_original_price.getOriginalPrice(variant,{'channel': sylius.channel}) }}
services.yml can be shorten to:
app.templating.helper.price:
decorates: sylius.templating.helper.price
class: AppBundle\Helper\PriceHelper
arguments:
- "#sylius.calculator.product_variant_price"
The PriceHelper service is configured in this file: https://github.com/Sylius/Sylius/blob/f7d42d2ce64288407372775e0ed421debcd50cd3/src/Sylius/Bundle/CoreBundle/Resources/config/services/templating.xml
But instead of replacing the service like you did, you should decorate it. Extend the PriceHelper class with a new class and add the functionality that you need, then add configuration for your new service like described in the following link, to decorate the PriceHelper service: http://symfony.com/doc/current/service_container/service_decoration.html
In your specific case, you have to use this configuration:
AppBundle\Helper\PriceHelper:
decorates: '#sylius.templating.helper.price'
arguments:
$productVariantPriceCalculator: '#sylius.calculator.product_variant_price'
I've just installed a vanilla SaltStack master and minions. I'm trying to follow the instructions on https://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html to create a very basic PHP state.
/srv/salt/php.sls:
php_ini:
file.managed:
- name: /etc/php.ini
- source: salt://php.ini.tmpl
- template: jinja
- context:
php_ini_settings: {{ salt.pillar.get('php_ini', {}) | json() }}
/srv/pillar/php.sls:
php_ini:
PHP:
engine: 'On'
short_open_tag: 'Off'
error_reporting: 'E_ALL & ~E_DEPRECATED & ~E_STRICT'
/srv/salt/php.ini.tmpl:
{% macro php_ini_serializer(data) %}
{% for section_name, name_val_pairs in data.items() %}
[{{ section_name }}]
{% for name, val in name_val_pairs.items() -%}
{{ name }} = "{{ val }}"
{% endfor %}
{% endfor %}
{% endmacro %}
; File managed by Salt at <{{ source }}>.
; Your changes will be overwritten.
{{ php_ini_serializer(php_ini_settings) }}
The output file looks like this:
VVV File starts on the next line
; File managed by Salt at <salt://php.ini.tmpl>.
; Your changes will be overwritten.
^^^ File ends on the previous line
I've added extra lines so that Stack Overflow will display the blank lines in the output file correctly.
I would expect it to look something link this:
VVV File starts on the next line
; File managed by Salt at <salt://php.ini.tmpl>.
; Your changes will be overwritten.
[PHP]
engine = "On"
short_open_tag = "Off"
error_reporting = "E_ALL & ~E_DEPRECATED & ~E_STRICT"
^^^ File ends on the previous line
It appears that Salt isn't reading the pillar file at all. What have I done wrong?
It looks like php_ini_settings: {{ salt.pillar.get('php_ini', {}) | json() }} does not add any data to the context of your jinja template salt://php.ini.tmpl.
To debug check if the pillar data is available use the pillar module
salt 'minionid' pilar.ls # to check the existence of keys
salt 'minionid' pilar.items # to explore whole the pillar data of your minion
Pillar data needs to be applied to minions like states using a top.sls file. My guess is that you did not apply your pillar data to your minion. Does /srv/pillar/top.sls includes somthing like that?
base:
'minionid':
- php
I use saltstack to deploy my application on AWS. The formulas fetch the jar from artifactory and run the application as a service.
It works fine for production(release version ex: 1.1.3) but it fails on dev environment with snapshot version (ex: 1.1.4-SNAPSHOT).
My formula :
artifactory.downloaded:
- artifact:
artifactory_url: {{ artifactory_url }}
repository: {{ repository }}
artifact_id: {{ artifact_id }}
group_id: {{ group_id }}
packaging: {{ packaging }}
classifier: {{ classifier }}
version: '{{ version }}'
- target_dir: {{ folder }}
The error: 'NoneType' object is not iterable
I think I figure it out.
The state artifactory.downloaded use the module artifactory.get_snapshot for snapshot and artifactory.get_release for release.
The get_snapshot module needs a snapshot_version properties and version properties (I think it's an issue) but you can't pass snapshot_version properties from artifactory.downloaded state.
So to resolve this issue, I don't longer use artifactory.downloaded state but artifactory.get_snapshot / artifactory.get_release module :
artifact_download:
module.run:
- name: artifactory.get_snapshot
- artifactory_url: {{ artifactory_url }}
- repository: {{ repository }}
- artifact_id: {{artifact_id }}
- group_id: {{ group_id }}
- packaging: {{ packaging }}
- classifier: {{ classifier }}
- version: '{{ version }}'
- snapshot_version: '{{ version }}'
- target_dir: {{ folder }}
⚠️ - snapshot_version and version properties are both required.
I'm trying to learn SaltStack and now I'm facing a problem.
I have a property file (propertyfile.properties) with values being populated by salt pillar. Now this property file exists in multiple directories. The issue that I have is that I want the values of the property file be populated/rendered by salt pillar.get function base on what directory currently it is into. To give you more idea, let's consider this example:
propertyfile.property (in directory 1)
name={{ salt['pillar.get']['dir1.name'] }}
propertyfile.property (in directory 2)
name={{ salt['pillar.get']['dir2.name'] }}
#pillar
dir1.name=dir1
dir2.name=dir2
note that the property file is only one and is generated by salt to multiple directories via loop like this:
{% for dir in 'dir1', 'dir2' %}
propertyfile_properties_{{ dir }}:
file.managed:
- name: /home/devuser/{{ dir }}/propertyfile.properties
- source: {{ propertyfile_source }}
- source_hash: {{ propertyfile_source }}.MD5
- template: jinja
- show_diff: True
- makedirs: True
{% endfor %}
Any ideas? Your help is very much appreciated. Thanks
You can do this by passing each directory to the template as context, as it's rendered:
# In .sls
{% for dir in salt['pillar.get']("directories") %}
propertyfile_properties_{{ dir }}:
file.managed:
- name: /home/devuser/{{ dir }}/propertyfile.properties
- source: salt://path/to/template.jinja
- template: jinja
- context:
dir: {{ dir }}
{% endfor %}
# In template:
dirname={{ dir }}
fullpath=/home/devuser/{{ dir }}/propertyfile.properties
# In pillar:
directories:
- dir1
- dir2
- ...and so on
Note the extra indentation of the contents of the context dictionary. There's an explanation of why that's sometimes necessary here.