0

I'm trying to use mysql-formula, using it's pillar to set up a database and a user. But, I'd like to use the configured password elsewhere, and I haven't figured out how to do that. Any help appreciated.

Here's the example pillar: https://github.com/saltstack-formulas/mysql-formula/blob/master/pillar.example and assume I'm trying to access frank's password from there.

viq
  • 1
  • 1

1 Answers1

1

Simple answer

To access Frank's password from your states/configuration file :

  {% set franck_pwd = salt['pillar.get']('mysql:user:franck:password','') -%}
  frank:
    user.present:  
      - password: {{ franck_pwd }}
      - host: localhost

Here, we just set a variable and assign to it the data from pillar at the location

 mysql:user:franck:password

It will allow you to get the password stored in pillar after you made this pilar .sls available to the host in the pillar top file.

Advanced

So now you can access your pillar data from your states and configuration files --

don't forget to add to config files definition so Jinja can be interpreted

   my_config_file:   
     file.managed:
       - template: jinja

It's not very flexible to have to set a variable for each user you want to have. Well you can get it done quite simply just by adding user in pillar.

From that pillar file example, you can create users dynamically just based on what's in pillar in your state.

# Loop on users and get all args
{% for user, args in salt['pillar.get']('mysql:user','{}').iteritems() %}

# Use your vars!
{{ user }}:
  user.present:
    - password: args['password']
    - host: args['host']
{% enfor %}

It's more a matter of using jinja correctly than getting data from pillar really.

You can get anything from pillar and use Jinja to refine afterwards. You can already bookmark that link with Jinja templates and examples : http://jinja.pocoo.org/docs/dev/templates/

Val F.
  • 134
  • 4