Anton Kim
Anton Kim

Reputation: 943

ANSIBLE - shell task returns non-zero return code but otherwise works in terminal

I have an ansible task:

  - name: Get vault's binary path
    shell: type -p vault
    register: vault_binary_path

returns

 TASK [update_vault : Get vault's binary path] **********************************************************************************************************************************************************************
fatal: [xxxxx]: FAILED! => {"changed": true, "cmd": "type -p vault", "delta": "0:00:00.003303", "end": "2020-04-08 11:37:19.636528", "msg": "non-zero return code", "rc": 1, "start": "2020-04-08 11:37:19.633225", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

but when I run it in shell it returns just fine

[root@ip-xxxxx]# type -p vault
/usr/local/bin/vault

I run ansible as root with become: true. All previous steps are fine up until this one. Any advice appreciated.

Upvotes: 1

Views: 5381

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295308

Define an update to your PATH in your playbook:

environment:
  PATH: "{{ ansible_env.PATH }}:/usr/local/bin"

...so that /usr/local/bin is guaranteed to be included.

(Also, while when writing bash-specific code type is almost always preferable to which, this isn't such a case, as your shell may be /bin/sh, which isn't guaranteed to support any features that aren't given in the POSIX sh specification. Consider changing to shell: command -v vault, which is guaranteed to work as-intended on all POSIX-compliant shells).

Upvotes: 2

Related Questions