首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible:循环遍历dict和filetree

Ansible:循环遍历dict和filetree
EN

Stack Overflow用户
提问于 2019-05-04 23:04:41
回答 1查看 1.3K关注 0票数 1

如何循环遍历dict和filetree?我想递归模板文件与.j2后缀(关键字)到目标位置(值),也基名应该被重命名(删除.j2后缀)。这是一个完美的用例。不幸的是,ansible并不擅长复杂的数据结构。

输入:

代码语言:javascript
复制
vars:
  applications:
    application1:
      svcpaths:
        localfolder/bardir1: remotefolder/bardir1
        localfolder/bardir2: remotefolder/bardir2
        localfolder/bardir3: remotefolder/bardir3
    application2:
      svcpaths:
        localfolder/bardir5: remotefolder/bardir5
        localfolder/bardir6: remotefolder/bardir6

我的尝试:

代码语言:javascript
复制
    - name: Files to template
      template:
        src: "{{ item.src }}"
        dest: "{{ item.destination }}/{{ item.name | regex_replace('.j2','') }}"
      loop: |
        [
        {% for c in applications %}
        {% if applications[c]['svcpaths'] is defined and applications[c]['svcpaths'] |list|length >0  %}
        {% for o,m in applications[c]['svcpaths'].items() %}
        {% for i in lookup('filetree', o ) %}
        {% if i.state == 'file' and i.path | regex_search('.\.j2') %}
        {
        "name": "{{ i.path }}",
        "src": "{{ i.src }}",
        "destination": "{{ m }}"
        }, 
        {% endif %}
        {% endfor %}
        {% endfor %}
        {% endif %}
        {% endfor %}
        ]

我知道在游戏中使用jinja是不好的,如果可能的话,我想避免它。另外,输入数据结构也不应该改变。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-05-05 12:39:05

如果我理解你想做什么,我想有一个相当简单的解决方案。如果您像这样编写一个名为template_files.yml的任务文件

代码语言:javascript
复制
---
- name: render templates to dest_dir
  loop: "{{ query('filetree', src_dir) }}"

  # we need this to avoid conflicts with the "item" variable in
  # the calling playbook.
  loop_control:
    loop_var: template
  when: template.src.endswith('.j2')
  template:
    src: "{{ template.src }}"
    dest: "{{ dest_dir }}/{{ (template.src|basename)[:-3] }}"

然后你可以像这样写一本剧本:

代码语言:javascript
复制
---
- hosts: localhost
  gather_facts: false
  vars:
    applications:
      application1:
        svcpaths:
          localfolder/bardir1: /tmp/remotefolder/bardir1
          localfolder/bardir2: /tmp/remotefolder/bardir2
          localfolder/bardir3: /tmp/remotefolder/bardir3
      application2:
        svcpaths:
          localfolder/bardir5: /tmp/remotefolder/bardir5
          localfolder/bardir6: /tmp/remotefolder/bardir6

  tasks:
    # generate a list of {key: src, value: destination} 
    # dictionaries from your data structure.
    - set_fact:
        templates: "{{ templates|default([]) + item|dict2items }}"
      loop: "{{ applications|json_query('*.svcpaths')}}"

    # show what the generated variable looks like
    - debug:
        var: templates

    # template all the things
    - include_tasks: template_files.yml
      loop: "{{ templates }}"
      vars:
        src_dir: "{{ item.key }}"
        dest_dir: "{{ item.value }}"

假设我有一组本地文件,如下所示:

代码语言:javascript
复制
localfolder/bardir1/example.txt.j2
localfolder/bardir2/example.txt.j2
localfolder/bardir3/example.txt.j2
localfolder/bardir5/example.txt.j2
localfolder/bardir6/example.txt.j2

运行攻略会产生以下结果:

代码语言:javascript
复制
/tmp/remotefolder/bardir6/example.txt
/tmp/remotefolder/bardir5/example.txt
/tmp/remotefolder/bardir3/example.txt
/tmp/remotefolder/bardir2/example.txt
/tmp/remotefolder/bardir1/example.txt

我认为这可能比你正在使用的基于Jinja模板的解决方案更容易阅读和理解。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55983831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档