Search code examples
ansibleansible-facts

How can I lookup properties in a slurped file?


According to the ansible documentation, I can use slurp to read a remote file.

I have a java properties file on a remote host that I want to slurp so I did:

- name slurp xyz properties
  slurp:
    src: /some/path/on/the/remote/my.properties
  register: myprops

- debug:
    msg: "{{ myprops['content'] | b64decode }}"

If I do that I get the content.

Now I want to use that conent in ansible. E.g. through a lookup. Something like this:

{{lookup('somePropertyInPropertiesFile', myprops['content'])}}

But this doesn't work since the lookup module only allows lookup in files.

How can I pass the slurped file to the lookup?

I'm using ansible 2.9.9


Solution

  • Given ansible lookups work on the control host, you can also get a file from remote to local using fetch module. Then use ini lookup to read a specific property from the properties file.

    I couldn't test the code but something like below should work.

    - name: Fetch my properties
      fetch:
        src: /some/path/on/the/remote/my.properties
        dest: /tmp/
        flat: yes
    
    - debug: 
        msg: "content is {{ lookup('ini', 'content type=properties file=/tmp/my.properties') }}"
    

    flat: yes will copy the file under /tmp without creating a dir with hostname in the given destination dir which is the default behavior. This might be useful if you have a single host or do not care if the file gets overwritten.