1

I am running a bash command using Ansible.

kubectl version  | head -1 | awk '{print $5}'| cut -d"\"" -f2

The output is supposed to be

   v1.2.0

Playbook is ::

   - name: version of minion before upgrade.
     command: 'kubectl version  | head -1 | awk '{print $5}'| cut -d"\"" -f2'


     register: version

I have also used

     command: "kubectl version  | head -1 | awk '{print $5}'| cut -d"\"" -f2"

But it keeps returning me the ERROR.

# ansible-playbook upgrade_k8s.yaml -i ../env/platform
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/root/ejusnub/ansible/playbook/upgrade_k8s.yaml': line 40, column 50, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

   - name: version of minion before upgrade.
     command: 'kubectl version  | head -1 | awk '{print $5}'| cut -d"\"" -f2'
                                                 ^ here

Ive even tried to escape every symbol but the same error occurs.

2 Answers 2

1

Try this:

- shell: kubectl version | head -1 | awk '{ print $5 }' | cut -d'"' -f2

Use shell module instead of command to work with pipes.

P.S. and carefully read error messages, in every case in your example (with single quotes and with double quotes), you have misplaced quotes.

Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively you can use kubectl version with the --short parameter whose purpose is to get the version number, here it is in a shell task:

 - shell: 'kubectl version --short | cut -d: -f2'

This will remove the need for the calls to head and awk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.