Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible lookup a single line in a json file

I need to read an ip line from a dynamic generated json file and add it to a configuration file on the server.

At Ansible home page i found two modules which would help:
- lookup module
- fileinline module

The lookup examples however show looking up for the whole contents of a file using this phrase "{{ lookup('file', '/etc/foo.txt') }}"
How could i filter the result into reading a single line?

Does anybody know a good way to achieve this ?

like image 855
alixander Avatar asked Jun 17 '16 11:06

alixander


1 Answers

You probably do want a special key from a JSON dict I guess? If it's just a random line which can not be accessed inside the JSON struct it will be hard. You would need to grep out the line in a separate task.

But let's assume you want a special value from a dict, then you can convert the JSON to an object with the from_json filter:

{{ lookup('file', '/etc/foo.txt') | from_json }}

Now if you want the value of bar from the contained data structure, something like this should work:

{{ (lookup('file', '/etc/foo.txt') | from_json).get('bar') }}
like image 56
udondan Avatar answered Oct 18 '22 03:10

udondan