so, first. you should never file.managed and file.replace in the same file. either file.managed OR file.replace. this is because they are mutually exclusive and will always end up registering as changing the file. the file.managed should handle the piece that is being replaced through templating.
that being said. yes you can use onlyif or unless. which puts the test into the state instead of makes it it’s own state.
https://docs.saltproject.io/en/latest/ref/states/requisites.html#unless
https://docs.saltproject.io/en/latest/ref/states/requisites.html#onlyif
something like the following
boot-config-file-get:
file.managed:
- name: /boot/config.txt
- source: salt://os/common/boot/config.txt
- template: jinja
- backup: minion
- onlyif:
- fun: file.file_exists
arg:
- /boot/config.txt
- fun: match.grain
tgt: cpuarch:aarch64
https://docs.saltproject.io/en/3000/ref/modules/all/salt.modules.match.html#salt.modules.match.grain
https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.file.html#salt.modules.file.file_exists
--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/dbdcfb0b-5e28-490b-b5a1-3059f39a684en%40googlegroups.com.
so, I never said anything about separate files. and don’t recommend that path either. I’m saying template the file so that you just have the file.managed without having the file.replace later. aka have the file.managed put it together correctly in the first place.
herre is a full example. of me both running the commands as well as all of the files involved. Now this isn’t a complete example of your exact information. but should be enough that you start to get the point.
root@salt00:/srv/salt/tests# salt-call state.apply tests.file
local:
----------
ID: windows file test
Function: file.managed
Name: /tmp/test
Result: True
Comment: onlyif condition is false
Started: 21:37:59.940536
Duration: 2686.292 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 2.686 s
root@salt00:/srv/salt/tests# touch /tmp/test
root@salt00:/srv/salt/tests# salt-call state.apply tests.file
local:
----------
ID: windows file test
Function: file.managed
Name: /tmp/test
Result: True
Comment: File /tmp/test updated
Started: 21:38:58.632514
Duration: 2725.411 ms
Changes:
----------
diff:
---
+++
@@ -0,0 +1,3 @@
+
+
+arm_64bit=0
Summary for local
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 2.725 s
root@salt00:/srv/salt/tests# salt-call state.apply tests.file
local:
----------
ID: windows file test
Function: file.managed
Name: /tmp/test
Result: True
Comment: File /tmp/test is in the correct state
Started: 21:39:33.669573
Duration: 3475.972 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 3.476 s
root@salt00:/srv/salt/tests# cat /tmp/test
arm_64bit=0
root@salt00:/srv/salt/tests# cat file.sls
windows file test:
file.managed:
- name: /tmp/test
- source: salt://tests/files/testfile.txt
- template: jinja
- onlyif:
- fun: file.file_exists
path: /tmp/test
root@salt00:/srv/salt/tests# cat files/testfile.txt
{% set arm_64bit=0 %}
{% if salt["match.grain"]('cpuarch:aarch64') %}
{% set arm_64bit=1 %}
{% endif %}
arm_64bit={{arm_64bit}}
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/0e698821-0a31-4586-9ea4-1965b812120cn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/0e9f0c62-6eff-48a8-8d7f-53350b15774cn%40googlegroups.com.