-1

Team, in my playbook am invoking yq in shell module but its complaining of path. can anyone help please? I have already tested manually yq and jq are available.

cat $HOME/.kube/config/cluster-user.kubeconfig | yq .contexts[0].name```
"site.test.com"
"site.test.com"
      - name: "GET API server current-context name with YQ..  "
        shell: "cat $HOME/.kube/config/cluster-user.kubeconfig | yq .[\"current-context\"]"
        register: string1
        ignore_errors: true

      - debug:
          var: string1.stdout_lines
        when: string1.stdout != ''


      - name: "GET API server contexts name with YQ..  "
        shell: "cat $HOME/.kube/config/cluster-user.kubeconfig | yq .contexts[0].name"
        register: string2
        ignore_errors: true

      - debug:
          var: string2.stdout_lines
        when: string2.stdout != ''


      - fail:
          msg: "Validate if both string1 and string2 are same, if yes proceed..."
        when: string2.stdout != string1.stdout

anyhint how to make ansible use binaries ?

output:

TASK [GET API server contexts name with YQ..] ******************************************************************************************
fatal: [target1]: FAILED! => {"changed": true, "cmd": "cat $HOME/.kube/config/cluster-user.kubeconfig | yq .contexts[0].name", "delta": "0:00:00.006670", "end": "2019-09-30 18:15:13.829021", "msg": "non-zero return code", "rc": 127, "start": "2019-09-30 18:15:13.822351", "stderr": "/bin/sh: yq: command not found", "stderr_lines": ["/bin/sh: yq: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • The message isn't `yq`, it's that `yq` can't find **`jq`** – mdaniel Oct 01 '19 at 00:33
  • I adjusted my question now. – AhmFM Oct 01 '19 at 01:16
  • 1
    Well, previously you had `/usr/local/bin/yq` fully qualified, and it found `yq` but didn't find `jq`; if both `yq` and `jq` are in `/usr/local/bin`, then the answer is to either modify the `PATH` variable of the user ansible is connecting via, or add `{"environment": {"PATH": "/usr/local/bin:{{ ansible_env.PATH }}" } }` to your `shell:` invocation – mdaniel Oct 01 '19 at 06:10
  • I already have these set in $PATH but still get that error for any binary i am trying to call from ansible. can you please clarify more how exactly should i add that on my MAC? echo $PATH /opt/local/bin:/opt/local/sbin:/usr/local/go/bin:/usr/local/bin: – AhmFM Oct 01 '19 at 23:21
  • which file will it be on mac to do this? {"environment": {"PATH": "/usr/local/bin:{{ ansible_env.PATH }}" } } – AhmFM Oct 01 '19 at 23:47
  • Well, the `$PATH` for your user may very well be that, but apparently the user that ansible is using to connect to that machine does not have that `$PATH`; and I was just using the json syntax for [the `environment:` Task keyword](https://docs.ansible.com/ansible/2.8/reference_appendices/playbooks_keywords.html#task) because the SO comments are so terrible for whitespace – mdaniel Oct 02 '19 at 05:06

2 Answers2

0

Added a path in environments in playbook

environment: PATH: “{{ ansible_env.PATH }}:/usr/local/bin”

Now, my binaries were being used as expected. However, I still have problem using yq coz it asks for jq.

AhmFM
  • 1,552
  • 3
  • 23
  • 53
0

What I did was in my playbook I added a task to install yq via pip:

- name: Install yq
  pip:
    name: ['yq']
kernal119
  • 175
  • 1
  • 3
  • 14