Passing attributes during runtime

Hi Guys,

I had a question, which I couldn’t seem to find an answer for in earlier posts.
I might have been searching the wrong keywords, but here it is. Is it possible
to run the chef-client command and pass in custom attributes inline. I need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for every
different name and post number that I want to pass in or update the same json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

In general this isn't how Chef is designed to run, it expects to know the whole universe so you would put all this information either in node data on the server or in roles et al (the latter is preferable). You could, however, use environment variables to pass runtime information into Chef if you reeeeeally wanted to.

--Noah

On Feb 4, 2014, at 10:19 AM, Ameya Ameya Savale ameya@simplyhired.com wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier posts.
I might have been searching the wrong keywords, but here it is. Is it possible
to run the chef-client command and pass in custom attributes inline. I need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for every
different name and post number that I want to pass in or update the same json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

Thanks, I will look into the different approaches you stated.

-Ameya

On Tue, Feb 4, 2014 at 10:24 AM, Noah Kantrowitz noah@coderanger.netwrote:

In general this isn't how Chef is designed to run, it expects to know the
whole universe so you would put all this information either in node data on
the server or in roles et al (the latter is preferable). You could,
however, use environment variables to pass runtime information into Chef if
you reeeeeally wanted to.

--Noah

On Feb 4, 2014, at 10:19 AM, Ameya Ameya Savale ameya@simplyhired.com
wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier
posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates
certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the same
json
file before running the recipe with the new name and port, this seems
very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

you can invoke chef-client with -j option to pass custom attributes
(including the run list). This is the exact mechanism using which chef
bootstraps (install chef and other stuff on a virgin server). If you dont
want the converged node state to be saved back to the chef server, you can
use run list override (-o) option.
I have used same workflows to setup development and other type of instances
where I want to reuse my chef scripts but dont want the target chef server
to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale
ameya@simplyhired.comwrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier
posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the same
json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

Hi Ranjib,

So you were able to pass in attributes using the -j option like so:

chef-client -j {"key":"value"}

  • Thanks
    Ameya

On Tue, Feb 4, 2014 at 10:40 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can invoke chef-client with -j option to pass custom attributes
(including the run list). This is the exact mechanism using which chef
bootstraps (install chef and other stuff on a virgin server). If you dont
want the converged node state to be saved back to the chef server, you can
use run list override (-o) option.
I have used same workflows to setup development and other type of
instances where I want to reuse my chef scripts but dont want the target
chef server to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale <ameya@simplyhired.com

wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier
posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the same
json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

-j expects a json file or a url pointing the json file. For an example of
how to specify different attribute, you can get a node back as json

knife node show foo -Fj > foo.json

read foo.json, and see how attributes, environments, run list etc are
specified. -j expects a file with similar structure.

On Tue, Feb 4, 2014 at 11:00 AM, Ameya Savale ameya@simplyhired.com wrote:

Hi Ranjib,

So you were able to pass in attributes using the -j option like so:

chef-client -j {"key":"value"}

  • Thanks
    Ameya

On Tue, Feb 4, 2014 at 10:40 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can invoke chef-client with -j option to pass custom attributes
(including the run list). This is the exact mechanism using which chef
bootstraps (install chef and other stuff on a virgin server). If you dont
want the converged node state to be saved back to the chef server, you can
use run list override (-o) option.
I have used same workflows to setup development and other type of
instances where I want to reuse my chef scripts but dont want the target
chef server to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale <
ameya@simplyhired.com> wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier
posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates
certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the same
json
file before running the recipe with the new name and port, this seems
very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

No, the argument to -j is a file containing JSON.

--Noah

On Feb 4, 2014, at 11:00 AM, Ameya Savale ameya@simplyhired.com wrote:

Hi Ranjib,

So you were able to pass in attributes using the -j option like so:

chef-client -j {"key":"value"}

  • Thanks
    Ameya

On Tue, Feb 4, 2014 at 10:40 AM, Ranjib Dey dey.ranjib@gmail.com wrote:
you can invoke chef-client with -j option to pass custom attributes (including the run list). This is the exact mechanism using which chef bootstraps (install chef and other stuff on a virgin server). If you dont want the converged node state to be saved back to the chef server, you can use run list override (-o) option.
I have used same workflows to setup development and other type of instances where I want to reuse my chef scripts but dont want the target chef server to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale ameya@simplyhired.com wrote:
Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier posts.
I might have been searching the wrong keywords, but here it is. Is it possible
to run the chef-client command and pass in custom attributes inline. I need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for every
different name and post number that I want to pass in or update the same json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

Hi Ranjib,

On Tue, Feb 4, 2014 at 11:22 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

-j expects a json file or a url pointing the json file. For an example of
how to specify different attribute, you can get a node back as json

knife node show foo -Fj > foo.json

read foo.json, and see how attributes, environments, run list etc are
specified. -j expects a file with similar structure.

On Tue, Feb 4, 2014 at 11:00 AM, Ameya Savale ameya@simplyhired.comwrote:

Hi Ranjib,

So you were able to pass in attributes using the -j option like so:

chef-client -j {"key":"value"}

  • Thanks
    Ameya

On Tue, Feb 4, 2014 at 10:40 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can invoke chef-client with -j option to pass custom attributes
(including the run list). This is the exact mechanism using which chef
bootstraps (install chef and other stuff on a virgin server). If you dont
want the converged node state to be saved back to the chef server, you can
use run list override (-o) option.
I have used same workflows to setup development and other type of
instances where I want to reuse my chef scripts but dont want the target
chef server to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale <
ameya@simplyhired.com> wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in
earlier posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates
certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read
in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the
same json
file before running the recipe with the new name and port, this seems
very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

Hi Ranjib

I get it now, thanks for information.

-Ameya

On Tue, Feb 4, 2014 at 11:22 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

-j expects a json file or a url pointing the json file. For an example of
how to specify different attribute, you can get a node back as json

knife node show foo -Fj > foo.json

read foo.json, and see how attributes, environments, run list etc are
specified. -j expects a file with similar structure.

On Tue, Feb 4, 2014 at 11:00 AM, Ameya Savale ameya@simplyhired.comwrote:

Hi Ranjib,

So you were able to pass in attributes using the -j option like so:

chef-client -j {"key":"value"}

  • Thanks
    Ameya

On Tue, Feb 4, 2014 at 10:40 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can invoke chef-client with -j option to pass custom attributes
(including the run list). This is the exact mechanism using which chef
bootstraps (install chef and other stuff on a virgin server). If you dont
want the converged node state to be saved back to the chef server, you can
use run list override (-o) option.
I have used same workflows to setup development and other type of
instances where I want to reuse my chef scripts but dont want the target
chef server to be under chef server, or actively managed (one time setup).

cheers
ranjib

On Tue, Feb 4, 2014 at 10:19 AM, Ameya Ameya Savale <
ameya@simplyhired.com> wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in
earlier posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates
certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read
in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the
same json
file before running the recipe with the new name and port, this seems
very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

This is a great use case for a definition0. A definition, quite
simply, is a "recipe macro." It looks like a resource when used in a
recipe, but it contains other resources that you want to repeat with
slightly different parameters. Here's a totally made up example.

define :host_porter, :port => 4000, :hostname => nil do
params[:hostname] ||= params[:name]

directory "/etc/#{params[:hostname]}" do
recursive true
end

file "/etc/#{params[:hostname]}/#{params[:port]}" do
content "some content"
end
end

in a recipe you write something like this:

host_porter node['hostname'] do
port 4000
end

host_porter "www1" do
port 4001
end

There are more definition examples in our apache2 and chef-splunk cookbooks[1].

Cheers,
Joshua

[1]: http://ckbk.it/apache2 http://ckbk.it/chef-splunk

On Tue, Feb 4, 2014 at 11:19 AM, Ameya Ameya Savale
ameya@simplyhired.com wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier posts.
I might have been searching the wrong keywords, but here it is. Is it possible
to run the chef-client command and pass in custom attributes inline. I need to
write a recipe which takes in a name and a port number and creates certain
directories and files and setups up configuration on the host using the name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for every
different name and post number that I want to pass in or update the same json
file before running the recipe with the new name and port, this seems very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

--
Joshua Timberman, Chef.

Thank you Joshua for this information, I will definitely try to use this
approach in order to address my problem :slight_smile:

-Ameya

On Wed, Feb 5, 2014 at 7:58 PM, Joshua Timberman joshua@getchef.com wrote:

This is a great use case for a definition0. A definition, quite
simply, is a "recipe macro." It looks like a resource when used in a
recipe, but it contains other resources that you want to repeat with
slightly different parameters. Here's a totally made up example.

define :host_porter, :port => 4000, :hostname => nil do
params[:hostname] ||= params[:name]

directory "/etc/#{params[:hostname]}" do
recursive true
end

file "/etc/#{params[:hostname]}/#{params[:port]}" do
content "some content"
end
end

in a recipe you write something like this:

host_porter node['hostname'] do
port 4000
end

host_porter "www1" do
port 4001
end

There are more definition examples in our apache2 and chef-splunk
cookbooks[1].

Cheers,
Joshua

[1]: http://ckbk.it/apache2 http://ckbk.it/chef-splunk

On Tue, Feb 4, 2014 at 11:19 AM, Ameya Ameya Savale
ameya@simplyhired.com wrote:

Hi Guys,

I had a question, which I couldn't seem to find an answer for in earlier
posts.
I might have been searching the wrong keywords, but here it is. Is it
possible
to run the chef-client command and pass in custom attributes inline. I
need to
write a recipe which takes in a name and a port number and creates
certain
directories and files and setups up configuration on the host using the
name
and the port number.

I recently started using chef so everything is quite new to me. I read in
multiple places that you can do it using a json file. If I understood it
correctly then it seems I would need to create a separate json file for
every
different name and post number that I want to pass in or update the same
json
file before running the recipe with the new name and port, this seems
very
tedious.

What would be the correct way of approaching this problem?

  • Thanks
    Ameya

--
Joshua Timberman, Chef.