如何使用ansible读取json文件 [英] how to read json file using ansible

查看:35
本文介绍了如何使用ansible读取json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ansible 脚本所在的同一目录中有一个 json 文件.以下是json文件的内容:

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

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

我正在尝试使用 get_url 下载这些软件包.以下是方法:

I am trying to to download these packages using get_url. Following is the approach:

---
- 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

第一个任务是正确打印文件的内容,但在第二个任务中,我收到以下错误:

The first task is printing the content of the file correctly but in the second task, I am getting the following error:

[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.

推荐答案

查找后必须添加一个 from_json jinja2 过滤器:

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

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

这篇关于如何使用ansible读取json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆