chef / chef-apply

The ad-hoc execution tool for the Chef ecosystem.
https://www.chef.sh/
Apache License 2.0
14 stars 14 forks source link

Add Attributes Option for `chef-run` #160

Open jonsmorrow opened 4 years ago

jonsmorrow commented 4 years ago

As a chef-run user,

I would like to be able to pass in attributes via the CLI during a chef-run,

So that I can parameterize my ad-hoc tasks in a way where I can key off of said attributes, and do action/assignment therein ..

Example use case:

I want to deploy webapp-v1.23 to a target set of nodes using remote_file .. This means I would need to update my recipe and execute chef-run to make it work as expected .. What I would prefer is to be able to pass in an attribute via the CLI that I can then refer to in my recipe .. For example:

remote_file '/opt/tomcat/webapps/user.war' do source "http://nexus.example.com/user-#{node['user_webapp_version']}" owner "tomcat" group "tomcat" mode "0755" action :createend

And the way I would invoke it would be to do something like this:

chef-run webapp-user[1:10]-dev recipes/deploy.rb --node-attr 'user_webapp_version=1.23'

This will enable me to use chef-run as my tool of choice via my automation platform (Jenkins, etc.) to perform ad-hoc tasks against target nodes, where I could create an automation "job", pass it parameters, trigger said job, git clone my automation code, and let chef-run :dothething: ..

So a possible rebuttal: "Greg, that info should be in code, not as some parameter that is passed in via the CLI, this is not the Chef way" .. I agree !! But since there is no way to generically assign attributes to targets, there is a way where this same feature could be used to key off of a CLI parameter to refer to code .. Here is an example:

# attributes/default.rbdefault['user_webapp_version'] = { 'dev' => 1.23, 'stage' => 1.22, 'prod' => 1.21}
# recipes/deploy.rbnode_enviro = node['node_enviro']remote_file '/opt/tomcat/webapps/user.war' do source "http://nexus.example.com/user-#{node['user_webapp_version'][node_enviro]}" owner "tomcat" group "tomcat" mode "0755" action :createend

And the way I would invoke it would be to do something like this:

chef-run webapp-user[1:10]-dev recipes/deploy.rb --node-attr 'node_enviro="dev"'

Addendum: I want to be able to use this --node-attr option multiple times via the same execution .. For example:

chef-run webapp-user[1:10]-dev recipes/deploy.rb --node-attr 'user_webapp_version="1.23"' --node-attr 'restart_webapp=false'

Aha! Link: https://chef.aha.io/epics/WS-E-22