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

6,482 views
Skip to first unread message

ZillaYT

unread,
Jan 22, 2018, 2:32:20 PM1/22/18
to Ansible Project
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?

Matt Martz

unread,
Jan 22, 2018, 3:21:45 PM1/22/18
to ansible...@googlegroups.com
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 }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f4f129f1-bbaa-4b5e-856a-afb5f1a6e86c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

ZillaYT

unread,
Jan 22, 2018, 4:07:30 PM1/22/18
to Ansible Project
I didn't know that about lookups. The code you provided worked! Thanks!


On Monday, January 22, 2018 at 3:21:45 PM UTC-5, Matt Martz wrote:
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 }}"
On Mon, Jan 22, 2018 at 1:32 PM, ZillaYT <zil...@gmail.com> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages