简体   繁体   English

Ansible Playbook 将数组列表作为变量传递

[英]Ansible Playbook passing array list as variable

so we currently have a create directories yml and it has 20 or so folders it creates.. I'm attempting to streamline that and pass in the folders with the mode and recurse flag which are all different.所以我们目前有一个创建目录 yml 并且它创建了 20 个左右的文件夹。我正在尝试简化它并传入具有不同模式和递归标志的文件夹。 As a test I created this.作为测试,我创建了这个。

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

Normally we have something like this:通常我们有这样的东西:

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

So my problem is we use AaaS to pass information over and how the parameter is set.. We have a TextBox on a web page.所以我的问题是我们使用 AaaS 传递信息以及如何设置参数。我们在 web 页面上有一个文本框。 I tried the following with no luck.我尝试了以下没有运气。

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

and

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

and

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

The error I'm getting in all cases states 'dest' does not exist.我在所有情况下遇到的错误都表明“dest”不存在。 Is it formatting or is there something else I need to consider?是格式化还是我需要考虑其他一些事情?

I don't have rep to make a comment, but i would think that the yaml you're sending in the first example is a list, but sending yaml via HTTP to an AaaS is difficult.我没有代表发表评论,但我认为您在第一个示例中发送的 yaml 是一个列表,但通过 Z293C9EA246FF9985DC6F62A650F78986 发送 yaml 是困难的。 You might try sending a JSON blob, so you can send:您可以尝试发送 JSON blob,这样您就可以发送:

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

And that would probably make your life easier这可能会让你的生活更轻松

EDIT: removed the single quotes as it's likely that a Text Box would be interpreted as a string already编辑:删除单引号,因为文本框可能已经被解释为字符串

Q: "Is it formatting or is there something else I need to consider?"问: “是格式化还是我需要考虑其他什么?”

A: The code works for me.答:代码对我有用。 (I've only changed the destinations eg dest: '/usr/local/test/path1' ). (我只更改了目的地,例如dest: '/usr/local/test/path1' )。 See below the dir before and after.请参阅下面的目录之前和之后。

shell> tree /usr/local/test
/usr/local/test

0 directories, 0 files

shell> tree /usr/local/test
/usr/local/test
├── path1
└── path2

2 directories, 0 files

shell> ll /usr/local/test
total 16
drwxr-xr-x  4 root root 4096 Jun  1 21:00 ./
drwxr-xr-x 12 root root 4096 Jun  1 21:00 ../
drwxrwxr-x  2 root root 4096 Jun  1 21:00 path1/
drwxrwxrwx  2 root root 4096 Jun  1 21:00 path2/

shell> cat playbook.yml
- hosts: localhost
  become: true
  tasks:
    - name: Create Multiple Directories
      file:
        path: "{{ item.dest }}"
        mode: "{{ item.mode }}"
        recurse: "{{ item.recurse }}"
        state: directory
      with_items:
        - {dest: '/usr/local/test/path1', mode: '0775', recurse: yes}
        - {dest: '/usr/local/test/path2', mode: '0777', recurse: no}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM