Home > Software engineering >  jq: error: syntax error, unexpected '-', expecting '}' (Unix shell quoting issue
jq: error: syntax error, unexpected '-', expecting '}' (Unix shell quoting issue

Time:12-12

I am trying to run some jq command over ssh-agent in a Jenkins pipeline. But I am getting the following error: Trying to run: `

        stage("common-infra-deployment"){
            steps{
                sshagent (credentials: ['test-private-key']){
                    script{
                       
                        sh '''
                        sudo ssh -o StrictHostKeyChecking=no [email protected] "
                        jq '.outputs | {"kubeconfig-file"}' terraform.tfstate;
                        "
                        '''

                    }
                }
            } 
        }

`

Error: `

jq: error: syntax error, unexpected '-', expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:

`

terraform.tfstate `

{
"outputs": {
    "kubeconfig-file": {
      "value": "/home/chi/jenkins-terraform/config",
      "type": "string"
    },
    "master-node-ip": {
      "value": "x.x.x.x",
      "type": "string"
    },
    "master-node-vm-name": {
      "value": "v1",
      "type": "string"
    },
    "worker-node-ip": {
      "value": "x.x.x.x, x.x.x.x, x.x.x.x",
      "type": "string"
    },
    "worker-node-vm-names": {
      "value": "v2, v3, v4",
      "type": "string"
    }
  }
}

`

I tried \ to escape '-' and '{}' but nothings working out. I want the jq to be executed over ssh session.

CodePudding user response:

You must escape your double quotes inside your double quotes:

sudo ssh -o StrictHostKeyChecking=no [email protected] "
jq '.outputs | {\"kubeconfig-file\"}' terraform.tfstate;
"

Perhaps the backslash needs to be escaped itself:

sudo ssh -o StrictHostKeyChecking=no [email protected] "
jq '.outputs | {\\"kubeconfig-file\\"}' terraform.tfstate;
"
  • Related