Skip to content

Problem with multipe pillar environments #4389

Closed

Description

@shintonik

I have defined multiple pillar environment in order to be able to write more generic states. That way important config data of a state can be easily replaced by the config values of another pillar environment.

The problem is that only the values of the last defined enviroment in the pillar top.sls are available indepedent of the passed environemt in state.sls call. Is this a bug or did I get something wrong?

/etc/salt/master

pillar_roots:
  base:
    - /srv/pillar
  staging:
    - /srv/pillar/staging
  dev:
    - /srv/pillar/dev

/srv/pillar/top.sls

base:
  '*':
    - data

staging:
  '*':
    - data

dev:
  '*':
    - data

/srv/pillar/data.sls

git:
  echo_cmd: This is a pillar value

/srv/pillar/staging/data.sls

git:
  echo_cmd: This is a staging pillar value

/srv/pillar/dev/data.sls

git:
  echo_cmd: This is a dev pillar value

/etc/salt/states/test/init.sls

test_output:
  cmd.run:
    - name: echo "{{ salt['pillar.get']('git:echo_cmd', 'default_echo') }}"

Output:

salt * state.sls test env=staging

State: - cmd
Name:      echo "This is a dev pillar value"
Function:  run
    Result:    True
    Comment:   Command "echo "This is a dev pillar value"" run
    Changes:   pid: 2892
               retcode: 0
               stderr: 
               stdout: This is a dev pillar value

Activity

  1. thatch45 commented on Apr 5, 2013

    @thatch45
    Contributor

    Since you have a match of a * for every environment, every system is being matched. This is also because the env value is passed only to state environments and not pillar environments

  2. shintonik commented on Apr 8, 2013

    @shintonik
    ContributorAuthor

    So that means it is intended behaviour? Maybe I misunderstood that whole concept of environments. To be honest I used enviroments more like a kind of vehicle for the solution I am aiming at. Basically, I'm looking for an option to provide Salt a dynamic render context.

    That way I could define a generic state with a default value which can be overwritten with a dynamically injected pillar value. Since there is no option to pass such data dynamically to my state I tried to define those data structures in a static way in the form of an environment.

    My concrete problem: I have already defined several states to setup my technology stack on a server. Now I have to setup that same stack with a slightly different set of configuration data (like username, checkout paths, web root etc.). I could duplicate all states and change the particular data but that is neither DRY nor does it scale if you have to do that more than 2 oder 3 times.

    The following example describes my approach:

    Pillar data

    users:
      user1:
        git:
          user: user1
          pass: user1_pass
          rev: dev
      user2:
        git:
          user: user2
          pass: user2_pass
      user3:
        git:
          user: user3
          pass: user3_pass

    Generic git checkout state

    {% set git_user = salt['pillar.get']('git:user', 'default_user') %}
    
    repo_checkout:
      git.latest:
        - name: https://:{{ git_user }}@domain.com/repo.git
        - target: /var/repos/{{ git_user }}/repo
        - rev: {{ salt['pillar.get']('git:rev', 'master') }}
        - runas: {{ git_user }}
        - require:
          - user: {{ git_user }}

    Salt call with the option to pass the pillar root as an argument

    salt * state.sls git.initial_checkouts -P users:user2
  3. thatch45 commented on Apr 8, 2013

    @thatch45
    Contributor

    Yes, merging the environments is the intended behavior, the top file allows for the environments to be specified on the master. Settign dynamic values to states is done with pillar but I think you are just seeing it from the wrong direction. When setting up environments you need to specify which minions realize which environments:

    base:
      '*base*':
        - data
    dev:
      '*dev*'
        - data
    

    This is assuming that you have divided up environments based on minion id, but grains or other target systems can also be used.

    Finally in your last command in your last comment here I am confused, are you assuming that -P users:user2 only applies the user2 key in users? -P passes in extra data to the pillar, it does not have anything to do with matching existing data

  4. shintonik commented on Apr 9, 2013

    @shintonik
    ContributorAuthor

    Hi Thomas, sorry I was not very clear about my use of environments. I have only used them because Salt is lacking an option to pass in a dynamic render context. That's why I tried to realize that with environments. I thought of pillar environments as separarated "containers" that hold my data. But now I know that was wrong. So environments do not make sense as a solution for this problem.

    In my last post I tried to describe the approach of a dynamic render context (without the use of environments). As I already said in my last post. I would like to write states that are as generic (a.k.a DRY) as possible and use them with different sets of data. In my case I have to setup the same specific technology stack on multiple servers as well as to setup that technology stack multiple times with slightly different configuration data on a single machine (e.g. one development environment per user on one machine).

    At the moment there is only one way for me to do that. Replicate my state tree multiple times (with all disadvantages of such a non-DRY approach). With a dynamic render context I only would have to define another data structure to reuse my states.

    The example of my last post describes exactly that. You define a generic state, a data structure (config data) and with the imaginary option -P (-p would probably be smarter because capitalized options seems to be used only to define an alternative way to target minions) you could pass in the root of the pillar data which is used by Salt as a render context. The state uses the data structure git:user, git:pass and git:rev and the -P option tells which node inside my pillar holds that data structure.

    I hope this time the actual approach got more clear.

  5. thatch45 commented on Apr 10, 2013

    @thatch45
    Contributor

    So you just want to pass in the data that gives you specific groups of values from pillar on the command line? How much have you looking into targets? Basically you should have a grain that matches specific machines and then match specific data via said grain in the pillar, then you get what you are looking for

  6. basepi commented on May 28, 2013

    @basepi
    Contributor

    @shintonik Did you get this resolved using grains as @thatch45 mentioned?

    I'm going to close this one, as the original issue is expected behavior.

  7. shintonik commented on May 29, 2013

    @shintonik
    ContributorAuthor

    @basepi It was never really an issue. But rather a missing feature of the state system. Actually it has nothing to do with targets or grains.

    I further described and discussed the problem/idea in the mailing list. Maybe a future version of Salt will come up with a solution.

  8. basepi commented on May 29, 2013

    @basepi
    Contributor

    If this is still an issue, could you create a new issue that distills the exact problems you're having or features you want? I think we've wandered a bit from the original issue, so it's hard to see at a glance what is needed.

  9. shintonik commented on May 31, 2013

    @shintonik
    ContributorAuthor

    Okay. Currently I'm busy with other tasks. But as soon as I have time to work out the details of the problem and an approach to solve it I will create a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions