how to read json file using ansible

后端 未结 3 1584
失恋的感觉
失恋的感觉 2021-02-18 18:34

I have a json file in the same directory where my ansible script is. Following is the content of json file:

{ \"resources\":[
           {\"name\":\"package1\",          


        
相关标签:
3条回答
  • 2021-02-18 19:16

    In case if you need to read a JSON formatted text and store it as a variable, it can be also handled by include_vars .

    - hosts: localhost
      tasks:
        - include_vars:
            file: variable-file.json
            name: variable
    
        - debug: var=variable
    
    0 讨论(0)
  • 2021-02-18 19:18

    for future visitors , if you are looking for a remote json file read. this won't work as ansible lookups are executed in the local

    you should use a module like Slurp

    0 讨论(0)
  • 2021-02-18 19:24

    You have to add a from_json jinja2 filter after the lookup:

    version_file: "{{ lookup('file','/home/shasha/devOps/tests/packageFile.json') | from_json }}"
    
    0 讨论(0)
提交回复
热议问题