mattray / spiceweasel

Generates Chef knife commands from a simple JSON or YAML file.
Apache License 2.0
284 stars 65 forks source link

Please add variable substitution #45

Open wesparish opened 11 years ago

wesparish commented 11 years ago

It would be nice for Spiceweasel to have the ability to dynamically replace variables at runtime.

For example, it would be nice to be able to set a Spiceweasel variable "password" to "mysecret123" and use a replacement string throughout the rest of the manifest file. Furthermore, it would also be nice to have the ability to not only define key/value pairs for variables, but also to define key/special_value variables that allow prompting the user at runtime (eg: prompting for a sensitive password that cannot be stored in plaintext).

As a possible path to take (assuming 2 special strings, prompt_for_password (no echo) and prompt_for_text (echo)):

variables:
- root_password:
    value: "prompt_for_password"
- admin_user_name:
    value: "prompt_for_text"
- mray_pem_file:
    value: "~/.ssh/mray.pem"

nodes:
- serverA:
    run_list: role[base]
    options: -i <%= variables[:mray_pem_file] %> -x <%= variables[:admin_user_name] %> --sudo -P <%= variables[:root_password] %>
rberger commented 11 years ago

I just tested, that if you use the ruby style of spiceweasel configurations, you can do ruby stuff which includes effectively the things you are discussing (I didn't try prompting but I presume it would work)

For instance I wanted to set attributes inside of the spiceweasel config file and not in roles but I didn't want to embed json directly in the cluster option lines. So I did:

pod_attributes = {
  :pod => "default",
  :haproxy => {
    :balance_algorithm => "hdr(X-Real-IP)",
    :defaults_options => [
                          "httplog",
                          "dontlognull",
                          "forwardfor", 
                          "redispatch",
                          "httpchk GET /ui/"
                         ]
  }
}

and then in the related cluster section I used ruby string interpolation #{pod_attributes.to_json} to expand that out in the options . Note that the spiceweasel {{n}} mechanism for embedding the index of the instance still works too:

  "clusters"=>
  [
   {
     "qa"=>
     [
      {
        "ec2 1"=>
        {
          "run_list"=>"role[rm-pod] recipe[rm-pod] recipe[logstash::awesant]",
          "options"=>
          "-d chef-full  -S mykey -G qa -I ami-eb3c0183 -f m1.medium -j \'#{pod_attributes.to_json}\' -N qplb{{n}}.app
        }
      },
...

The main problem is if there is any syntax errors you get a rather meaningless error. I may get around to adding some stuff and do a pull request but probably not soon since I'm going to be away for a couple of weeks.