2016-02-15 150 views
5

我有一個json文件在我的腳本是相同的目錄。以下是json文件的內容:如何閱讀json文件使用ansible

{ "resources":[ 
      {"name":"package1", "downloadURL":"path-to-file1" }, 
      {"name":"package2", "downloadURL": "path-to-file2"} 
    ] 
} 

我試圖使用get_url下載這些軟件包。以下是方法:

--- 
- hosts: localhost 
    vars: 
    package_dir: "/var/opt/" 
    version_file: "{{lookup('file','/home/shasha/devOps/tests/packageFile.json')}}" 

    tasks: 
    - name: Printing the file. 
     debug: msg="{{version_file}}" 

    - name: Downloading the packages. 
     get_url: url="{{item.downloadURL}}" dest="{{package_dir}}" mode=0777 
     with_items: version_file.resources 

的首要任務是正確打印文件的內容,但在第二個任務,我收到以下錯誤:

[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this 
will be a fatal error.. This feature will be removed in a future release. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 
+0

它適用於我在2.0.0.2。你使用什麼合理的版本? – graphite

+0

版本是2.1.0 – Shasha99

+0

看起來json文件中存在一些問題。對於少數文件而言,它正在工作,很少有文件不能。 – Shasha99

回答

10

您必須添加一個from_json的Jinja2篩選後查找:

version_file: "{{ lookup('file','/home/shasha/devOps/tests/packageFile.json') | from_json }}"