Object unchanged when running knife client edit

2019-09-08 15:25发布

问题:

I'm running knife client edit command to edit a node in Chef to set admin permissions. I would like to do this programmatically so my intention is to add this command to a shell script if I can make it work.

The previous state of the node is (UPDATE):

$ knife client show -Fj my_node
{
  "name": "my_node",
  "public_key": "-----BEGIN PUBLIC KEY-----\n...key...\n-----END PUBLIC KEY-----\n",
  "validator": false,
  "admin": false,
  "json_class": "Chef::ApiClient",
  "chef_type": "client"
}

When running the command it says that the object is unchanged and after checking the node with knife client I can verify that still has the admin attribute set to false:

$ knife client edit -d my_node < my_node.json
Object unchanged, not saving

The content of my_node.json file is:

{
  "name": "my_node",
  "public_key": "-----BEGIN PUBLIC KEY-----\n...key...\n-----END PUBLIC KEY-----\n",
  "validator": false,
  "admin": true,
  "json_class": "Chef::ApiClient",
  "chef_type": "client"
}

I don't know how to make this piece work. Any help on this will be very much appreciated.

NOTE: $ knife --version Chef: 12.2.1

UPDATE: I couldn't find an accepted answer in this thread which is quite similar.

UPDATE 2: I tried these other ways as well with no luck at all (using same json file):

$ cat my_node.json | knife client edit my_node -d
Object unchanged, not saving

$ knife client edit my_node -d < my_node.json
Object unchanged, not saving

回答1:

If you already got the file with the content you need, there is a trick to use it.

knife client edit -e "cp my_node.json"

Explanation on how interactive editing works on an app (knife in this case):

  1. The app starts on foreground the command on EDITOR environment variable passing a temporary file as argument
  2. When the command ends, the app opens the temporary file and uses it

So if all you want is changing a word, you can use sed instead:

knife client edit -e "sed -i  's/\"admin\":\s*false/\"admin\": true/'"


回答2:

There is no difference between your my_node.json and your my_node settings. Change a value and try again.