A data bag is a global variable that is stored as JSON data and is accessible from a Chef server. A data bag is indexed for searching and can be loaded by a recipe or accessed during a search.
A data bag item may be encrypted using shared secret encryption. This allows each data bag item to store confidential information (such as a database password) or to be managed in a source control system (without plain-text data appearing in revision history). Each data bag item may be encrypted individually; if a data bag contains multiple encrypted data bag items, these data bag items are not required to share the same encryption keys.
The knife data bag subcommand is used to manage arbitrary stores of globally available JSON data.
Note
Review the list of common options available to this (and all) Knife subcommands and plugins.
The create argument is used to add a data bag to the Chef server.
This argument has the following syntax:
$ knife data bag create DATA_BAG_NAME [DATA_BAG_ITEM] (options)
This argument has the following options:
Note
For encrypted data bag items, use either --secret or --secret-file, not both.
The following examples show how to use this Knife subcommand:
Create a data bag
To create a data bag named “admins”, enter:
$ knife data bag create admins
to return:
Created data_bag[admins]
The delete argument is used to delete a data bag or a data bag item from a Chef server.
This argument has the following syntax:
$ knife data bag delete DATA_BAG_NAME [DATA_BAG_ITEM] (options)
This argument has the following options:
The following examples show how to use this Knife subcommand:
Delete a data bag
$ knife data bag delete data_bag_name
Delete a data bag item
To delete an item named “charlie”, enter:
$ knife data bag delete admins charlie
Type Y to confirm a deletion.
The edit argument is used to edit the data contained in a data bag. If encryption is being used, the data bag will be decrypted, the data will be made available in the $EDITOR, and then encrypted again before saving it to the Chef server.
This argument has the following syntax:
$ knife data bag edit DATA_BAG_NAME [DATA_BAG_ITEM] (options)
This argument has the following options:
Note
For encrypted data bag items, use either --secret or --secret-file, not both.
The following examples show how to use this Knife subcommand:
Edit a data bag
To edit the contents of a data bag, enter:
$ knife data bag edit admins
Edit a data bag item
To edit an item named “charlie” that is contained in a data bag named “admins”, enter:
$ knife data bag edit admins charlie
to open the $EDITOR. Once opened, you can update the data before saving it to the Chef server. For example, by changing:
{
"id": "charlie"
}
to:
{
"id": "charlie",
"uid": 1005,
"gid": "ops",
"shell": "/bin/zsh",
"comment": "Crazy Charlie"
}
The from file argument is used to create a data bag on the Chef server from a file. The path to the data bag file must specify one of the following:
If the name of a data bag is specified, Knife will search for the data bag in ./data_bags/bag_name/file. Once opened, the JSON file should be a hash that contains at least an ID key which represents the name of the data bag item.
Warning
A chef-client must be version 11.6 (or higher) when using the knife data bag from file argument with the Enterprise Chef or Open Source Chef version 11 servers.
This argument has the following options:
Note
For encrypted data bag items, use either --secret or --secret-file, not both.
The following examples show how to use this Knife subcommand:
Create a data bag from a file
To create a data bag on the Chef server from a file:
$ knife data bag from file "path to JSON file"
Create an encrypted data bag from a file
To create a data bag named “devops_data” that contains encrypted data, enter:
$ knife data bag from file devops_data --secret-file "path to decryption file"
The list argument is used to view a list of data bags that are currently available on the Chef server.
This argument has the following options:
The following examples show how to use this Knife subcommand:
View a list of data bags
$ knife data bag list
The show argument is used to view the contents of a data bag.
This argument has the following options:
Note
For encrypted data bag items, use either --secret or --secret-file, not both.
The following examples show how to use this Knife subcommand:
Show a data bag
$ knife data bag show admins
to return something like:
charlie
Show a data bag item
To show the contents of a specific item within data bag, enter:
$ knife data bag show admins charlie
to return:
comment: Crazy Charlie
gid: ops
id: charlie
shell: /bin/zsh
uid: 1005
Show a data bag, encrypted
To show the contents of a data bag named “passwords” with an item that contains encrypted data named “mysql”, enter:
$ knife data bag show passwords mysql
to return:
## sample:
{
"id": "mysql",
"pass": "trywgFA6R70NO28PNhMpGhEvKBZuxouemnbnAUQsUyo=\n",
"user": "e/p+8WJYVHY9fHcEgAAReg==\n"
}
Show a data bag, decrypted
To show the decrypted contents of the same data bag, enter:
$ knife data bag show --secret-file /path/to/decryption/file passwords mysql
to return:
## sample:
{
"id": "mysql",
"pass": "thesecret123",
"user": "fred"
}
Show a data bag as JSON
To view information in JSON format, use the -F common option as part of the command like this:
$ knife data bag show admins -F json
Other formats available include text, yaml, and pp.