Ujjawal Khare
Ujjawal Khare

Reputation: 796

Ansible cant parse kubectl patch command

I am trying to execute below kubectl patch command using ansible but it gives error "We were unable to read either as JSON nor YAML".

I also tried adding escape characters before single and double quotes but no luck.

my task:

- name: Patching cnx-ingress to use loadbalancer provate ip as ingress. 

  shell: kubectl patch svc cnx-ingress-ingress-nginx-controller -n connections -p '{"spec": {"type": "LoadBalancer", "externalIPs":["172.26.0.13"]}}'   
  become_user: "{{ __sudo_user }}"

Error:

TASK [component-pack : Setup Community Ingress Controller] ********************************************************************************
fatal: [master1.internal.dev.net]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.
\n  mapping values are not allowed here\n\nThe error appears to be in '/Users/ujjawalkhare/connections3/deployment-ansible/ansible/roles/hcl/component/tasks/setup_community_ingress.yml': line 207, column 111, but may\nbe elsewhere in the file depending on the exact syntax problem.
\n\nThe offending line appears to be:\n\n- 
name:                      Patching cnx-ingress to use loadbalancer provate ip as ingress\n  
shell:                     kubectl patch svc cnx-ingress-ingress-nginx-controller -n connections -p '{\"spec\": {\"type\": \"LoadBalancer\", \"externalIPs\":[\"172.26.0.13\"]}}'\n 
                                                                                                     ^ here\nWe could be wrong, but this one looks like it might be an issue with\nunbalanced quotes. If starting a value with a quote, make sure the\nline ends with the same set of quotes. For instance this arbitrary\nexample:\n\n    foo: \"bad\" \"wolf\"\n\nCould be written as:\n\n    foo: '\"bad\" \"wolf\"'\n"}

Upvotes: 1

Views: 434

Answers (1)

larsks
larsks

Reputation: 312680

You need to quote the entire argument to shell (because it contains characters that make a YAML parser think you're trying to start a mapping value). Because you're already using both single- and double-quotes in the script, your best bet is to make use of one of YAML's block-quote operators. Perhaps:

    - name: Patching cnx-ingress to use loadbalancer provate ip as ingress.
      shell: >
        kubectl patch svc cnx-ingress-ingress-nginx-controller
        -n connections
        -p '{"spec": {"type": "LoadBalancer", "externalIPs":["172.26.0.13"]}}'
      become_user: "{{ __sudo_user }}"

Also, consider the Ansible k8s module instead of running kubectl patch.

Upvotes: 1

Related Questions