salt icon indicating copy to clipboard operation
salt copied to clipboard

Manage multiple files in one state declaration.

Open benhosmer opened this issue 13 years ago • 21 comments

I believe this used to work:

config-files:
  file:
    - managed:
      - name: /tmp/file1.conf
      - source: salt://files/file1.conf
  file:
    - managed:
      - name: /tmp/file2.conf
      - source: salt://files/file2.conf

But now, I only get one file copied over.

Has something changed or am I completely crazy and remembering something wrong?

benhosmer avatar Apr 16 '13 14:04 benhosmer

You can only do one file.managed underneath a single id declaration On Apr 16, 2013 8:34 AM, "Ben Hosmer" [email protected] wrote:

I believe this used to work:

config-files: file: - managed: - name: /tmp/file1.conf - source: salt://files/file1.conf file: - managed: - name: /tmp/file2.conf - source: salt://files/file2.conf

But now, I only get one file copied over.

Has something changed or am I completely crazy and remembering something wrong?

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/4508 .

UtahDave avatar Apr 16 '13 15:04 UtahDave

I guess I'm crazy then! Thanks for setting me straight.

benhosmer avatar Apr 16 '13 16:04 benhosmer

I tried an ID declaration with a file.managed and a file.exists, and only the file.managed function worked. The documentation is not clear how function declarations under the same ID get processed if they come from the same state.

I'm still uncertain when my function declarations will be silently ignored? Is it when they both come from the same state?

markisus avatar Jul 10 '13 20:07 markisus

you can't have have two functions from the same state module under the same ID declaration.

You'll have to split those out under 2 ID declarations.

On Wed, Jul 10, 2013 at 2:37 PM, Mark [email protected] wrote:

I tried an ID declaration with a file.managed and a file.exists, and only the file.managed function worked. The documentation is not clear how function declarations under the same ID get processed if they come from the same state.

I'm still uncertain when my function declarations will be silently ignored? Is it when they both come from the same state?

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/4508#issuecomment-20771262 .

Dave Boucha | Sr. Engineer

5272 South College Drive, Suite 301 | Murray, UT 84123 office 801-305-3563 [email protected] | www.saltstack.com http://saltstack.com/

UtahDave avatar Jul 10 '13 20:07 UtahDave

No error was thrown because of the multiple declarations of the same state?

Pedro Algarvio @ phone

----- Reply message ----- De: "David Boucha" [email protected] Para: "saltstack/salt" [email protected] Assunto: [salt] Manage multiple files in one state declaration. (#4508) Data: qua, Jul 10, 2013 21:46 you can't have have two functions from the same state module under the same

ID declaration.

You'll have to split those out under 2 ID declarations.

On Wed, Jul 10, 2013 at 2:37 PM, Mark [email protected] wrote:

I tried an ID declaration with a file.managed and a file.exists, and only

the file.managed function worked. The documentation is not clear how

function declarations under the same ID get processed if they come from the

same state.

I'm still uncertain when my function declarations will be silently

ignored? Is it when they both come from the same state?

—

Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/4508#issuecomment-20771262

.

Dave Boucha | Sr. Engineer

5272 South College Drive, Suite 301 | Murray, UT 84123

office 801-305-3563

[email protected] | www.saltstack.com http://saltstack.com/

— Reply to this email directly or view it on GitHub.

s0undt3ch avatar Jul 11 '13 18:07 s0undt3ch

The fact that there was no error surprises me. We might want to look into that.

As an aside, file.managed will verify that a file exists. You don't need both, afaik.

basepi avatar Jul 11 '13 19:07 basepi

Since it is yaml, you can't have multiple keys with the same name -- I did get a warning (v 0.16.0):

[WARNING ] Duplicate Key: "pkg" found in salt://postgresql environment=base

It would be nice to be able to do something like:

postgresql: pkg: - installed - name: postgresql92-server pkg: - installed - name: postgresql92-contrib

Since we could add a service state ... so the limitation of only one kind of state per id seems to be a limitation of yaml more than of salt? (or not, I tried making the second one pgk.installed, and it complained.)

rrauenza avatar Aug 06 '13 23:08 rrauenza

@rrauenza you can list multiple pkg states like so..

collectd-install:
  pkg.installed:
    - pkgs:
      - collectd-utils
      - collectd

For future reference that is documented salt.states.pkg with the following example provided.

mypkgs:
  pkg.installed:
    - pkgs:
      - foo
      - bar: 1.2.3-4
      - baz
    - pkg_verify:
      - ignore_types: [config,doc]

jimmystewpot avatar Apr 08 '15 10:04 jimmystewpot

But shouldn't it be possible to manage multiple files in one 'file.manage' as the OP was asking:

config-files:
  file.managed:
    - arg1: value1
    - arg2: value2
    - arg3: value3
    - names:
      - /path/to/some/file1:
        - source: salt://path/to/source/file1
      - /path/to/some/file2:
        - source: salt://path/to/source/file2
      - /path/to/some/file3:
        - source: salt://path/to/source/file3

shorawitz avatar Feb 12 '16 16:02 shorawitz

Do we have any method to use like @shorawitz mentioned?

nrsekhar avatar Aug 24 '16 06:08 nrsekhar

As @nrsekhar asked is there any method like what @shorawitz mentions? Running into this trying to setup the nginx site files to throw in /etc/nginx/conf.d/ and trying to avoid having to do a full stanza for each file I would need to be there.

slothlair avatar May 01 '17 18:05 slothlair

Same here, looking for a way to do this to rename multiple files on a target system all in the same state declaration.

ndobbs avatar May 08 '17 14:05 ndobbs

For reference, you may want to have a look at file.recurse to manage a full directory. However, the ability to manage multiple files under a single ID as suggested by @shorawitz would benefit to me aswell

calve avatar May 11 '17 16:05 calve

I'd also see a benefit in being able to manage multiple files and their ownership/mode in a single id.

dynek avatar Dec 01 '17 15:12 dynek

Meanwhile, although a little ugly you can loop over all the files you want to manage.

Simple:

{% for file in 
  '/etc/ssl/private/' + fqdn + '.key', 
  '/etc/ssl/private/' + fqdn + '.crt'
%}
{{ file }}:
  file.managed:
  - source: "salt://application1/{{ file }}"
  - user: root
  - group: ssl-cert
  - mode: 0740
{% endfor %}

Complex:

{% for file in [
  { 'src': '/etc/ssl/private/' + fqdn + '.key', 'dst': '/etc/ssl/private/' + fqdn + '.key', 'group': 'ssl-cert'},
  { 'src': '/etc/ssl/private/' + fqdn + '.crt', 'dst': '/etc/ssl/private/' + fqdn + '.crt', 'group': 'ssl-cert'},
  { 'src': '/etc/nginx/sites-available/application2.conf.j2', 'dst': '/etc/nginx/sites-available/' + fqdn + '.conf', 'group': 'www-data' }
  ]
%}
{{ file.dst }}:
  file.managed:
  - source: "salt://application2/{{ file.src }}"
  - user: root
  - group: "{{ file.group }}"
  - mode: 0740
{% endfor %}

eb4x avatar Jan 12 '18 12:01 eb4x

Every six months a Google search leads me here then I just get exasperated. Anyway, Is this still a plan so we don't have to template it out? Or is another renderer more practical or have people just resorted to packaging like rpm/deb/nix/help?

config-files:
  file:
    - managed:
      - name: /tmp/file1.conf
      - source: salt://files/file1.conf
  file:
    - managed:
      - name: /tmp/file2.conf
      - source: salt://files/file2.conf

daodennis-zz avatar Jan 17 '18 22:01 daodennis-zz

I did something like this, it worked. 1 extra line per file.

config-files1:
  file.managed:
    - name: /tmp/file1.conf
    - source: salt://files/file1.conf
config-files2:
  file.managed:
    - name: /tmp/file2.conf
    - source: salt://files/file2.conf

gmcfrear avatar Jan 25 '18 16:01 gmcfrear

Can someone from the team reopen this issue please?

jleroy avatar Mar 17 '18 17:03 jleroy

Meanwhile, although a little ugly you can loop over all the files you want to manage.

Simple:

{% for file in 
  '/etc/ssl/private/' + fqdn + '.key', 
  '/etc/ssl/private/' + fqdn + '.crt'
%}
{{ file }}:
  file.managed:
  - source: "salt://application1/{{ file }}"
  - user: root
  - group: ssl-cert
  - mode: 0740
{% endfor %}

Complex:

{% for file in [
  { 'src': '/etc/ssl/private/' + fqdn + '.key', 'dst': '/etc/ssl/private/' + fqdn + '.key', 'group': 'ssl-cert'},
  { 'src': '/etc/ssl/private/' + fqdn + '.crt', 'dst': '/etc/ssl/private/' + fqdn + '.crt', 'group': 'ssl-cert'},
  { 'src': '/etc/nginx/sites-available/application2.conf.j2', 'dst': '/etc/nginx/sites-available/' + fqdn + '.conf', 'group': 'www-data' }
  ]
%}
{{ file.dst }}:
  file.managed:
  - source: "salt://application2/{{ file.src }}"
  - user: root
  - group: "{{ file.group }}"
  - mode: 0740
{% endfor %}

is not working for me

State 'copy nginx config file' in SLS 'nginx_install.nginx' is not formed as a list

State 'copy nginx config file' in SLS 'nginx_install.nginx' is not formed as a list

siitao avatar Sep 09 '19 09:09 siitao

State 'copy nginx config file' in SLS 'nginx_install.nginx' is not formed as a list

You're probably missing some brackets '[' and ']' to make it a list in the for loop. Show your sls

eb4x avatar Sep 11 '19 10:09 eb4x