How to make ansible read a json file on remote host?

I following the technique suggested in how to read json file using ansible suggestion in Stackoverflow, and I do this

`

  • set_fact:
    version_file: “{{ lookup(‘file’,‘/opt/gitlab/version-manifest.json’) | from_json }}”
    installed_version: version_file.build_version

`

However I get the error

`
TASK [gitlab-ce : set_fact] ****************************************************
[WARNING]: Unable to find ‘/opt/gitlab/version-manifest.json’ in expected
paths.

fatal: [host.domain.com]: FAILED! => {“failed”: true, “msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a <class ‘ansible.errors.AnsibleError’>, original message: could not locate file in lookup: /opt/gitlab/version-manifest.json”}
to retry, use: --limit @/var/tmp/jenkins/team_SCSCM/gitlab-ce/install-gitlab-ce/ansible-playbooks/gitlab-ce.retry

PLAY RECAP *********************************************************************
rtp-scm-lnx26.cisco.com : ok=5 changed=0 unreachable=0
`

The file is there on the remote machine. What gives?

lookups only execute on the local ansible controller.

You will need to use something such as the slurp module to fetch the contents of the file.

  • slurp:
    path: /opt/gitlab/version-manifest.json
    register: version_file

  • set_fact:
    installed_version: “{{ (version_file.content|b64decode|from_json).build_version }}”

I didn’t know that about lookups. The code you provided worked! Thanks!