首页 > 解决方案 > Ansible Playbook 将数组列表作为变量传递

问题描述

所以我们目前有一个创建目录 yml 并且它创建了 20 个左右的文件夹。我正在尝试简化它并传入具有不同模式和递归标志的文件夹。作为测试,我创建了这个。

- debug: msg="testing = {{ testing }}"
- name: "loop through list from a variable"
  debug:
    msg: "dest: {{ item.dest }}, mode: {{ item.mode }}, recurse: {{ item.recurse }}"
  with_items: "{{ testing }}"

通常我们有这样的东西:

- name: Create Multiple Directories
  file:
    path: "{{ item.dest }}"
    mode: "{{ item.mode }}"
    recurse: "{{ item.recurse }}"
    state: directory
  with_items:
    - { dest: '/local/path1', mode: '0775', recurse: yes }
    - { dest: '/local/path2', mode: '0777', recurse: no }

所以我的问题是我们使用 AaaS 来传递信息以及如何设置参数。我们在网页上有一个 TextBox。我尝试了以下没有运气。

- { dest: '/local/path1', mode: '0775', recurse: yes }
- { dest: '/local/path2', mode: '0777', recurse: no }

{ dest: '/local/path1', mode: '0775', recurse: yes }
{ dest: '/local/path2', mode: '0777', recurse: no }

dest: '/local/path1', mode: '0775', recurse: yes
dest: '/local/path2', mode: '0777', recurse: no

我在所有情况下遇到的错误都表明“dest”不存在。是格式化还是我需要考虑其他一些事情?

标签: ansible

解决方案


我没有代表发表评论,但我认为您在第一个示例中发送的 yaml 是一个列表,但是通过 HTTP 将 yaml 发送到 AaaS 很困难。您可以尝试发送 JSON Blob,这样您就可以发送:

[{"dest": "/local/path1", "mode": "0775", "recurse": true },
{"dest": "/local/path2", "mode": "0777", "recurse": false }]

这可能会让你的生活更轻松

编辑:删除单引号,因为文本框可能已经被解释为字符串


推荐阅读