大网数据广告活动
服务器运维问答

Ansible loops over lists and array mixin

· 52CE 我爱测

I'm currently trying to work on a task which will create directories for two differents services but I've trouble solving how am I suppose to do that using ansible loops. Here is the object: obj: metadata: uuid: version: services…

问题原文

原帖链接 · Dr I · CC BY-SA 3.0

I'm currently trying to work on a task which will create directories for two differents services but I've trouble solving how am I suppose to do that using ansible loops.

Here is the object:

obj:
  metadata:
    uuid:
    version:

  services:
    - server:
      archive: binary.tar.gz
      dirs:
        bindir: /path/to/bindir/
        confdir: /path/to/confdir/
        tmpdir: /path/to/tmpdir/

    - client:
      archive: binary.tar.gz
      dirs:
        tmpdir: /path/to/tmpdir

And here is my associated task:

#Creating directories
- name: Creating directories for server and client mode.
  file:
    path: "{{ item.1.dirs['tmpdir'] }}"
    state: directory
    owner: "{{ item.0.metadata.uuid }}"
    group: "{{ item.0.metadata.uuid }}"
    mode: 0750
  with_subelements:
    - "{{ obj }}"
    - services

Now, I'm a little bit disturbed by how loops works on ansible and a little bit stick for now with my current iteration, so, could you help me to find a way to achieve what I'm trying to do?

已采纳解答

原帖链接 · TRW · CC BY-SA 4.0

According to your comments you want to create directories for obj.services.#.dirs.tmpdir items with details from obj.metadata. The solution with width_items: "{{ object.services }}" is nearly working - but you don't have access to obj.metadata. The thing is - there is only one obj.metadata at all and so you don't need to refer to it in the loop. You can use it directly.

- name: Creating directories for server and client mode.
  file:
    path: "{{ item.dirs['tmpdir'] }}"
    state: directory
    owner: "{{ obj.metadata.uuid }}"
    group: "{{ obj.metadata.uuid }}"
    mode: 0750
  with_items:
    - "{{ obj.services }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。