5

I want to copy a file from salt stack master server to minion servers. I found a script from https://stackoverflow.com/questions/27687886/copy-a-file-from-salt-master-to-minions. But I got an error.

copy_my_files:
  file.recurse:
    - source: salt://srv/salt/nginx.conf
    - target: /etc/nginx
    - makedirs: True

Error:-

  ID: copy_my_files
    Function: file.recurse
      Result: False
     Comment: Specified file copy_my_files is not an absolute path
     Started: 09:46:24.850682
    Duration: 1.473 ms
     Changes:

I have given correct paths for both.

Janith
  • 223
  • 2
  • 4
  • 8
  • Hey Janith - I see you have asked a bunch of questions about saltstack - perhaps you should spend some time with the documentation. – user9517 Dec 26 '18 at 12:58

2 Answers2

4

To simply copy a file, not as part of a state, use salt-cp.

The source can be any file on the master. It does not need to be within the salt fileserver.

salt-cp '*' SOURCE [SOURCE2 SOURCE3 ...] DEST
OrangeDog
  • 569
  • 4
  • 20
  • And for multiple hosts just use the -L option such as -L host1,host2,... – David Ramirez Dec 31 '19 at 18:15
  • 1
    @DavidRamirez the command already targets all minions. Use whatever targeting mechanism you like if that’s not what you want. – OrangeDog Dec 31 '19 at 18:21
  • This is a good answer for a quick ad hoc command. I'd just like to provide an example (copy file to all Windows minions, targeting by grain): `salt-cp -G 'os:windows' script_in_salt_base_on_SaltMstr.ps1 'C:\My_scripts'` – SamAndrew81 Feb 06 '23 at 19:03
2

file.recurse is for copying the content of a directory if I'm correct. Here, what you have to do to copy just one file would be to use file.managed.

For instance reusing your example, this should be working:

copy_my_files:
  file.managed:
    - name: /etc/nginx/nginx.conf
    - source: salt://nginx.conf
    - makedirs: True

Note that the nginx.conf file you want to copy has to be located in /srv/salt on the salt master. Thats the default place were the salt:// is pointing (unless you modified your configuration)

If you want to copy multiple file using the file.recurse it's also quite easy

deploy linter configuration:
  file.recurse:
    - name: "/usr/local/linter"
    - source: salt://devtools/files/linter
    - makedirs: True
    - replace: True
    - clean: True
agm650
  • 36
  • 4