# Calculate in salt state

**URL:** <https://forums.suse.com/t/calculate-in-salt-state/37258>\
**Category:** SUSE Multi Linux Manager\
**Created:** [March 14, 2022, 2:06pm UTC](https://forums.suse.com/t/calculate-in-salt-state/37258 "2022-03-14T14:06:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![schuenejoe](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@schuenejoe](https://forums.suse.com/u/schuenejoe)\
**Post date:** [March 14, 2022, 2:06pm UTC](https://forums.suse.com/t/calculate-in-salt-state/37258/1 "2022-03-14T14:06:29Z")

</div>

Hi.

I try to find a way to calculate some ports via an offset, that I definied in a state: The code is like this:

{% set domain = [

```
    { "server": "server1", "dom": "domain1", "port": 7000, "version": "2.12.0" },
    { "server": "server2", "dom": "domain2", "port": 8000, "version": "1.12.0" },
]

```

%}

{% set offset\_admin = 1 %}  
{% set offset\_managed = 3 %}  
{% set offset\_nm = 99 %}

{% for dom in domain %}  
{% if grains[‘host’] == dom.server %}

{% set serverport = salt [‘cmd.shell’](‘echo $(({{ dom.port }}+{{ offset\_managed }}))’) %}  
/path\_to\_file/{{ dom.dom }}/filename-mod2:  
file.replace:  
- name: /path\_to\_file/{{ dom.dom }}/filename.ini  
- pattern: “@PORT@”  
- repl: {{ serverport }}  
- onlyif:  
- grep ‘@PORT@’ /path\_to\_file/{{ dom.dom }}/filename.ini

{% endif %}  
{% endfor %}

Unfortunately, the value of the variables seems not to be filled before executing the bash command:

[ERROR] Command ‘echo’ failed with return code: 1  
[ERROR] stdout: /bin/bash: {{ dom.port }}+{{ offset\_managed }}: syntax error: operand expected (error token is “{{ dom.port }}+{{ offset\_managed }}”)  
[ERROR] retcode: 1  
[ERROR] Command ‘echo’ failed with return code: 1

I tried some code like this (with fixed values instead of variable “offset\_managed” ) too:

{% set serverport=eval({{dom.port+3}}) %}  
or  
{% set serverport = {{ dom.port }} + 3 %}

Any ideas? Is there a way to calculate directly in the state?

Kind regards  
Jörg
