小编Sha*_*a99的帖子

如何使用ansible读取json文件

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

{ "resources":[
           {"name":"package1", "downloadURL":"path-to-file1" },
           {"name":"package2", "downloadURL": "path-to-file2"}
   ]
}
Run Code Online (Sandbox Code Playgroud)

我试图使用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
Run Code Online (Sandbox Code Playgroud)

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

[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.
Run Code Online (Sandbox Code Playgroud)

ansible ansible-playbook ansible-2.x

16
推荐指数
3
解决办法
2万
查看次数

Running script with arguments through ansible

[Ansible version == 2.1.0]

In order to run a script which is present locally on the target server, we can use Ansible's "command" module. Following can be done easily:

- name: Executing getpkgs.sh to download packages.
  command: sh "/path/to /dir/scriptName.sh" arg1 arg2 arg3 arg4
Run Code Online (Sandbox Code Playgroud)

I have my script names and the arguments stored in ansible variables. For example, the following variable contains all the script names and the arguments to be passed to those scripts:

scripts_to_execute:
  - { filename: "/path/to/file/file1.sh", …
Run Code Online (Sandbox Code Playgroud)

ansible ansible-playbook ansible-2.x

0
推荐指数
1
解决办法
7273
查看次数

标签 统计

ansible ×2

ansible-2.x ×2

ansible-playbook ×2