2

I am trying to create an amazon EC2 instance using "knife ec2 server create" and passing in JSON to override the default settings by using the -j option where $json is properly formed JSON text and I get the same error if I quote the variable or not:

PS C:\Users\chef> knife ec2 server create --region ... -j $json

It fails with this error:

C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize': A JSON text must
at least contain two octets! (JSON::ParserError)
        from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `new'
        from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
        from C:/Users/chef/AppData/Local/chefdk/gem/ruby/2.1.0/gems/knife-ec2-0.12.0/lib/chef/knife/ec2_server_create.rb
:218:in `block in <class:Ec2ServerCreate>'

If I attempt to place the JSON on the command line I get a different error:

C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse': 757: unexpected token
at '{fusion_**** : *** }' (JSON::ParserError)
        from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
        from C:/Users/chef/AppData/Local/chefdk/gem/ruby/2.1.0/gems/knife-ec2-0.12.0/lib/chef/knife/ec2_server_create.rb
:218:in `block in <class:Ec2ServerCreate>'
        from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/mixlib-cli-1.5.0/lib/mixlib/cli.rb:235:in `call'

I have been unable to find any known issues with knife and PowerShell and while it does work with the minimal JSON of "{}", it does not appear to work with anything else.

Anyone have this working for themselves?

Neil Simon
  • 21
  • 1

2 Answers2

1

The shell may be eating your quotes, since both error message show them to be absent.

{fusion_**** : *** } isn't valid JSON, it definitely needs to be {"fusion_****" : "***"} with both key and value quoted (assuming the value is a string; the literals true, false, and null... and numeric values are not quoted, of course).

I would guess that it needs to be set something like this...

$json = '{"fusion_****" : "***"}'

...where the single quotes cause the double quotes to be interpreted as literals. Or this:

What is the literal escape character in Powershell?

...however, I literally know nothing about powershell, just shells in general. Perhaps posting the code where you're assigning the variable would help, if this doesn't solve it for you.

Michael - sqlbot
  • 21,988
  • 1
  • 57
  • 81
  • I've tried putting the JSON into a variable, various forms of quoting and even tried with the ConvertTo-Json/ConvertFrom-Json and still no luck. I've even tried the '--json-attribute-file' option and it still doesn't work. I suspect that it may be knife ec2 that is the problem. – Neil Simon Jan 19 '16 at 22:23
  • The '--json-attribute-file' option does not currently work in knife ec2. https://github.com/chef/knife-ec2/issues/397 – Neil Simon Jan 21 '16 at 19:05
  • Once you get confirmation of that issue, or some other feedback from the developers, you should consider posting what you've learned as an answer, here. Then I'll remove this one, if it turns out it doesn't apply. – Michael - sqlbot Jan 21 '16 at 21:11
1

What I found was that you need to escape the quotation marks in JSON with a backslash for Ruby.

Since I used substitution in my PowerShell string, I also escaped the quotation marks in the string with accent grave for PowerShell.

For example, to get this in JSON:

    { "fusion": "****" }

you could code the PowerShell either like this, with single quotes (no substitution):

    $json = '{ \"fusion_****\" : \"***\" }'

or like this, with double quotes (substitution enabled, but not used in this example):

    $json = "{ \`"fusion_****\`" : \`"***\`" }"