Open ghost opened 5 years ago
I think this may not be a bug. According to the documentation https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#salt.states.file.recurse is supposed to receive a single source (not a list of source) . I see some ticket with multiple sources though (e.g, https://github.com/saltstack/salt/issues/9304)
@kyanh-vX7HLAHZLPsUxeHD Multiple sources are supported and the code should pick the first one from the list that exists. If you remove the if statement with the grains does it work as expected and the first one in the list that exists is picked?
@garethgreenaway Yes it works well when I remove the if
statement. The test setup is on my office workstation I will give more details on Monday. Thank you.
If you remove the if statement with the grains does it work as expected and the first one in the list that exists is picked?
Yes, it does, as seen below
# remove the `if` statement
"update /xfiles":
file.recurse:
- replace: True
- source:
- salt://xfiles/{{ grains.id }}/
- salt://xfiles/{{ grains.xfiles }}/
- salt://xfiles/_default/
- name: /salt-test
# Now run highstate command. The result is as expected
root@controller-111:/srv/salt# salt '*' state.highstate
controller-111.internal:
----------
ID: update /xfiles
Function: file.recurse
Name: /salt-test/
Result: True
Comment: Recursively updated /salt-test/
Started: 07:06:59.594404
Duration: 89.9999999965 ms
Changes:
----------
/salt-test/.empty:
----------
diff:
---
+++
@@ -1 +1 @@
-from _default
+from top level
Summary for controller-111.internal
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 90.000 ms
; cat /salt-test/.empty
from top level
I've found that the use of -%}
does matter (whiltespace controlling in Jinjra template). The following code will generate errors
# Using -%}
{%- if salt["grains.get"]("xfiles") | length > 0 -%}
- salt://xfiles/{{ salt["grains.get"]("xfiles") }}/
{%- endif -%}
root@controller-111:/srv/salt# salt '*' state.highstate ; cat /salt-test/.empty
controller-111.internal:
----------
ID: update /xfiles
Function: file.recurse
Name: /salt-test/
Result: False
Comment: Recurse failed: none of the specified sources were found
Started: 09:49:26.318829
Duration: 29.9999999988 ms
Changes:
Now remove them both:
{%- if salt["grains.get"]("xfiles") | length > 0 %}
- salt://xfiles/{{ salt["grains.get"]("xfiles") }}/
{%- endif %}
- salt://xfiles/_default/
This code works perfectly. I think I totally misunderstood how whitespace control works.
Description of Issue
When multiple sources are provided in
file.recurse
, and if one of the sources may be excluded by some grains data,file.recurse
picks up the last source instead of the first source.Setup
bootstrap-salt.sh git develop
; note: this problem also happens with salt-2018 the stable version).minion grains
/srv/salt/base/init.sls
/srv/salt/xfiles/
Steps to Reproduce Issue
Step 1: With dynamic list of sources
Now execute highstate on the minion, and see the results
The result is not as expected
Step 2: Without dynamic list of sources:
Now uncomment the dynamic block
Apply the new state and see the expected resulted file
The resulted file is execpted:
Versions Report