file.managed: context and default

2,322 views
Skip to first unread message

martin f krafft

unread,
Oct 15, 2012, 1:32:31 AM10/15/12
to salt users list
Hey folks,

I am a bit unsure about the interplay between file.managed's context
and default parameters. If I can extend a state anyway and thus
change the default variable values, why would I want to specify the
context separately?

Thanks for any clues,

--
martin | http://madduck.net/ | http://two.sentenc.es/

"mirrors should reflect a little before throwing back images."
-- jean cocteau

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

David Boucha

unread,
Oct 15, 2012, 11:34:13 AM10/15/12
to salt-...@googlegroups.com
Martin,


There is nothing special about the context and defaults.  They are kind of like namespaces for the variables underneath them. They are not required and you can use any non-reserved word for them. For example:

/etc/http/conf/http.conf:
  file.managed:
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - mynewcontext:
        custom_var: "override"
    - newdefaults:
        custom_var: "default value"
        other_var: 123

Also, you can add a variable without such a context like this:

/etc/http/conf/http.conf:
  file.managed:
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - my_awesome_var: 'this is awesome'

Dave

Sean Channel

unread,
Oct 15, 2012, 12:18:30 PM10/15/12
to salt-...@googlegroups.com, David Boucha
Hi Martin.

Please note that the 'extend' declaration is used to append more state
definition to another state file you may already have (e.g. a stub or
generic version). It does not extend the functionality of Salt's modules.

'context' can be used to select different values when rendering a
managed file from a template, which may already have some variables that
you may wish to override.

_S.

martin f krafft

unread,
Oct 15, 2012, 3:27:14 PM10/15/12
to David Boucha, salt-...@googlegroups.com
also sprach David Boucha <da...@saltstack.com> [2012.10.15.1734 +0200]:
Yes.

> There is nothing special about the context and defaults. They are kind of
> like namespaces for the variables underneath them. They are not required
> and you can use any non-reserved word for them.


Thanks for the explanation.

> Also, you can add a variable without such a context like this:


Why then do these "namespaces" exist? What's the use case?
"lessing was a heretics' heretic"
-- walter kaufmann

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

David Boucha

unread,
Oct 15, 2012, 3:33:59 PM10/15/12
to David Boucha, salt-...@googlegroups.com
Why then do these "namespaces" exist? What's the use case?


I'm not 100% sure of their origin. I guess if you want to make sure a variable doesn't get
stomped by a variable of the same name being passed  in to through **kwargs. 

martin f krafft

unread,
Oct 16, 2012, 2:09:28 AM10/16/12
to David Boucha, salt-...@googlegroups.com
also sprach David Boucha <da...@saltstack.com> [2012.10.15.2133 +0200]:
I am still totally confused. Let's take this file.managed stanza
from the docs:

file.managed:
- source: salt://apache/http.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
custom_var: "override"
- defaults:
custom_var: "default value"
other_var: 123
- foobar:
other_var: 456

How does a http.conf template look that accesses all these variables?

Why do the docs mention 'default' and 'context', when I can use any
name I want anyway?
"auch der mutigste von uns hat nur selten den mut zu dem,
was er eigentlich weiß."
- friedrich nietzsche

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

David Boucha

unread,
Oct 16, 2012, 10:54:51 AM10/16/12
to David Boucha, salt-...@googlegroups.com
On Tue, Oct 16, 2012 at 12:09 AM, martin f krafft <mad...@madduck.net> wrote:
also sprach David Boucha <da...@saltstack.com> [2012.10.15.2133 +0200]:
> > Why then do these "namespaces" exist? What's the use case?
>
> I'm not 100% sure of their origin. I guess if you want to make
> sure a variable doesn't get stomped by a variable of the same name
> being passed  in to through **kwargs.

I am still totally confused. Let's take this file.managed stanza
from the docs:

  file.managed:
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
        custom_var: "override"
    - defaults:
        custom_var: "default value"
        other_var: 123
    - foobar:
        other_var: 456

How does a http.conf template look that accesses all these variables?

{{ context.custom_var }} 

{{ defaults.other_var }}

{{ foobar.other_var }}

Why do the docs mention 'default' and 'context', when I can use any
name I want anyway?

I believe the docs there need to be cleaned up and clarified a bit. I'll be doing that soon.
Reply all
Reply to author
Forward
0 new messages